【发布时间】:2018-01-17 23:59:53
【问题描述】:
你能告诉我为什么我的代码不起作用吗?它曾经运行良好,现在我从 listView 切换到 recyclerView 我得到了:
java.lang.ClassCastException: java.lang.Integer cannot be cast to com.app.SelectPhoneContact
无论哪个复选框在我的recyclerView 中,它应该是toast 对应的那些被选中的数字。
Button btnGetItem = (Button) findViewById(R.id.btnGetItem);
btnGetItem.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//loop through the Matching Contacts
int count = MatchingContactsAsArrayList.size();
for (int i = 0; i < count; i++) {
//for each Matching Contacts row in the recyclerview
LinearLayout itemLayout = (LinearLayout) recyclerView.getChildAt(i); // Find by under LinearLayout
//for each Matching Contacts checkbox
CheckBox checkbox = (CheckBox) itemLayout.findViewById(R.id.checkBoxContact);
//get other data related to the selected contact - name and number
SelectPhoneContact data = (SelectPhoneContact) checkbox.getTag();
//if that checkbox is checked, then get the phone number
if (checkbox.isChecked()) {
Toast.makeText(NewContact.this, data.getPhone(), Toast.LENGTH_LONG).show();
}
}
}
});
错误在303行,即:
SelectPhoneContact data = (SelectPhoneContact) checkbox.getTag();
【问题讨论】:
-
您在哪里以及如何设置标签?
-
在我的
recyclerViewAdapter上的onBindViewHolder中有CheckBox check = ((MatchingContact) viewHolder).check;和check.setTag(position);,这是我需要的。是否只是在那里设置另一个标签,如check.setTag(data);?
标签: java android android-recyclerview