私は2つのアクティビティが同じであるcustom adapter
を使用したいと考えているNewContact.java
とViewContact.java
という2つのアクティビティを持っています。java.lang.ClassCastException、同じカスタムアダプタを使用して2つのアクティビティを持つようにしよう
しかし、私は次のようなエラーと私のアプリがクラッシュを取得しています:私は、被験者AndroidRuntime: FATAL EXCEPTION:
、java.lang.ClassCastException
とここでかなりの数の記事を読んだが、私は合うように答えを調整する方法がわからない
AndroidRuntime: FATAL EXCEPTION: main
java.lang.ClassCastException: com.example.chris.tutorialspoint.ViewContact cannot be cast to com.example.chris.tutorialspoint.NewContact
私のニーズ。
私のアダプターのコードはgetView
です。私はこの問題は線である知っている:私は、残りの活動のために、その後custom adapter
作品を行のいずれかを削除した場合
viewHolder.check.setOnCheckedChangeListener((NewContact) _c);
viewHolder.check.setOnCheckedChangeListener((ViewContact) _c);
が、私はそれが両方の活動、NewContact
とViewContact
のために働いていたいです。
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
System.out.println("getView number is :" + i + "convertView is : " + convertView);
//we're naming our convertView as view
// View view = convertView;
ViewHolder viewHolder = null;
if (convertView == null) {
//if there is nothing there (if it's null) inflate the view with the layout
LayoutInflater li = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.phone_inflate_listview, null);
viewHolder = new ViewHolder();
// So, for example, title is cast to the name id, in phone_inflate_listview,
// phone is cast to the id called no etc
viewHolder.title = (TextView) convertView.findViewById(R.id.name);
viewHolder.phone = (TextView) convertView.findViewById(R.id.no);
viewHolder.invite = (Button) convertView.findViewById(R.id.btnInvite);
viewHolder.check = (CheckBox) convertView.findViewById(R.id.checkBoxContact);
// viewHolder.check.setVisibility(View.GONE);
//remember the state of the checkbox
viewHolder.check.setOnCheckedChangeListener((NewContact) _c);
viewHolder.check.setOnCheckedChangeListener((ViewContact) _c);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// store the holder with the view
final SelectPhoneContact data = (SelectPhoneContact) arraylist.get(i);
//in the listview for contacts, set the name
viewHolder.title.setText(data.getName());
//in the listview for contacts, set the number
viewHolder.phone.setText(data.getPhone());
////*********************
//for every phone number in the MatchingContactsAsArrayList array list...
for (int number = 0; number < MatchingContactsAsArrayList.size(); number++) {
//if a phone number is in our array of matching contacts
if (MatchingContactsAsArrayList.contains(data.getPhone()))
{
//if a matching contact, no need to show the Invite button
viewHolder.invite.setVisibility(View.GONE);
System.out.println("it's a match: phoneNumberofContact is : " + data.getPhone());
//once a matching contact is found, no need to keep looping x number of time, move onto next contact
break;
}
else {
//if not a matching contact, no need to show the check box
viewHolder.check.setVisibility(View.GONE);
}
}
viewHolder.check.setChecked(data.isSelected());
viewHolder.check.setTag(data);
// Return the completed view to render on screen
return convertView;
}
両方の 'Activity'クラスが' OnCheckedChangeListener'を実装している場合、 'OnCheckedChangeListener'に' _c'をキャストするだけで、 'setOnCheckedChangeListener()'コールが1つ必要です。 –
はい。それは大丈夫です。回答として投稿してください、私は喜んで受け入れるでしょう。 – CHarris