我有一个EditContact
课程。打开时,它会显示已选中或取消选中的复选框。这是通过我的adapter
中的一些代码完成的,这些代码正常运行:
//This is for EditContact, to display checked checkboxes. Numbers not in the array will remain unchecked.
//for every phone number in the checkedContactsAsArrayList array list...
for (int number2 = 0; number2 < checkedContactsAsArrayList.size(); number2++) {
Log.i("MyMessage","checkedContactsAsArrayList is: " + checkedContactsAsArrayList);
//if a phone number is in our array of checked contacts
if (checkedContactsAsArrayList.contains(selectPhoneContact.getPhone())) {
//check the box
((MatchingContact) viewHolder).check.setChecked(true);
}
}
在我的EditContact
课程中,用户可以检查或取消选中联系人列表中的框。如果选中它们,则应保存这些内容,adapter
中的代码应反映新的checkedContactsAsArrayList
,但无论选中什么,它始终保存为空。
在System.out
我可以看到we're in the try part
,然后直接转到EditContact: there's a problem here unfortunately
。你能告诉我出了什么问题以及如何解决它吗?
以下是我的save
按钮的代码:
//for the SAVE button
private void saveContactButton() {
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("you clicked it, save");
//close the class
//(we'll be opening it again, will close now so it will be refreshed)
PopulistoListView.fa.finish();
pDialog = new ProgressDialog(EditContact.this);
// Showing progress dialog for the review being saved
pDialog.setMessage("Saving...");
pDialog.show();
try {
System.out.println("we're in the try part");
//the user will be able to check contacts who to share the review
// with, from their matching contacts list
int count = PopulistoContactsAdapter.MatchingContactsAsArrayList.size();
for (int i = 0; i < count; i++) {
LinearLayout itemLayout = (LinearLayout)recyclerView.getChildAt(i);
CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.checkBoxContact);
SelectPhoneContact contact = (SelectPhoneContact) checkbox.getTag();
// make each checked contact in selectPhoneContacts
// into an individual
// JSON object called checkedContact
JSONObject checkedContact = new JSONObject();
//if that checkbox is checked, then get the phone number
if(checkbox.isChecked()) {
// checkedContact will be of the form {"checkedContact":"+353123456"}
checkedContact.put("checkedContact", contact.getPhone());
// Add checkedContact JSON Object to checkedContacts jsonArray
//The JSON Array will be of the form
// [{"checkedContact":"+3531234567"},{"checkedContact":"+353868132813"}]
//we will be posting this JSON Array to Php, further down below
checkedContacts.put(checkedContact);
System.out.println("EditContact: checkedcontact JSONObject :" + checkedContact);
}
}
System.out.println("checkedContacts JSON Array " + checkedContacts);
} catch (Exception e) {
System.out.println("EditContact: there's a problem here unfortunately");
e.printStackTrace();
}
//post the review_id in the current activity to EditContact.php and from that
//get associated values - category, name, phone etc...
StringRequest stringRequest = new StringRequest(Request.Method.POST, EditContact_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(EditContact.this, response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(EditContact.this, "there's a problem saving this page", Toast.LENGTH_LONG).show();
}
}) {
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
//post the phone number to php to get the user_id in the user table
params.put("phonenumberofuser", phoneNoofUserCheck);
params.put("review_id", review_id);
//the second value, categoryname.getText().toString() etc...
// is the value we get from Android.
//the key is "category", "name" etc.
// When we see these in our php, $_POST["category"],
//put in the value from Android
params.put("category", categoryname.getText().toString());
params.put("name", namename.getText().toString());
params.put("phone", phonename.getText().toString());
params.put("address", addressname.getText().toString());
params.put("comment", commentname.getText().toString());
params.put("public_or_private", String.valueOf(pub_or_priv));
//this is the JSON Array of checked contacts
//it will be of the form
//[{"checkedContact":"+3531234567"},{"checkedContact":"+353868132813"}]
params.put("checkedContacts", checkedContacts.toString());
return params;
}
};
AppController.getInstance().addToRequestQueue(stringRequest);
//when saved, go to the PopulistoListView class and update with
//the edited values
Intent j = new Intent(EditContact.this, PopulistoListView.class);
EditContact.this.startActivity(j);
finish();
//hide the dialogue box when page is saved
hidePDialog();
}
});
}
在there's a problem here unfortunately
的{{1}}下面,它说:
logcat
的{p> java.lang.ClassCastException: java.lang.Integer cannot be cast to com.example.chris.tutorialspoint.SelectPhoneContact
,即
line 318
但不确定如何修复它。
在SelectPhoneContact contact = (SelectPhoneContact) checkbox.getTag();
的{{1}}中:
onBindViewHolder
我的adapter
课程:
//if the activity is EditContact
if (whichactivity == 2) {
//if the row is a matching contact
if (viewHolder.getItemViewType() == 1)
{
//in the title textbox in the row, put the corresponding name etc...
((MatchingContact) viewHolder).title.setText(selectPhoneContact.getName());
((MatchingContact) viewHolder).phone.setText(selectPhoneContact.getPhone());
((MatchingContact) viewHolder).check.setChecked(theContactsList.get(position).getSelected());
((MatchingContact) viewHolder).check.setTag(position);
//disable the check box, can't be changed
//((MatchingContact) viewHolder).check.setEnabled(false);
((MatchingContact) viewHolder).check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//pos is the row number that the clicked checkbox exists in
Integer pos = (Integer) ((MatchingContact) viewHolder).check.getTag();
//NEED THIS TO PRESERVE CHECKBOX STATE
if (theContactsList.get(pos).getSelected()) {
theContactsList.get(pos).setSelected(false);
Toast.makeText(context_type, theContactsList.get(pos).getPhone() + " unclicked!", Toast.LENGTH_SHORT).show();
} else {
theContactsList.get(pos).setSelected(true);
Toast.makeText(context_type, theContactsList.get(pos).getPhone() + " clicked!", Toast.LENGTH_SHORT).show();
}
//we want to keep track of checked boxes, so when it is '0'
//'Phone Contacts' button will switch to 'Just Me'
int count;
count = 0;
int size = theContactsList.size();
for (int i = 0; i < size; i++) {
if (theContactsList.get(i).isSelected) {
count++;
// System.out.println("The count is " + count);
}
}
Log.i("MyMessage", "The count is " + count);
}
});
} else {
((nonMatchingContact) viewHolder).title.setText(selectPhoneContact.getName());
((nonMatchingContact) viewHolder).phone.setText(selectPhoneContact.getPhone());
}
//This is for EditContact, to display the contact the review is shared with
//for every phone number in the checkedContactsAsArrayList array list...
for (int number2 = 0; number2 < checkedContactsAsArrayList.size(); number2++) {
Log.i("MyMessage","checkedContactsAsArrayList is: " + checkedContactsAsArrayList);
//if a phone number is in our array of checked contacts
if (checkedContactsAsArrayList.contains(selectPhoneContact.getPhone())) {
//check the box
((MatchingContact) viewHolder).check.setChecked(true);
}
}
}
答案 0 :(得分:1)
问题在于您将整数设置为标记:
((MatchingContact) viewHolder).check.setTag(position);
然后在获取时将其转换为SelectPhoneContact类型的对象:
SelectPhoneContact contact = (SelectPhoneContact) checkbox.getTag();
也许你可以尝试类似的东西:
Object position = checkbox.getTag();
if (position instanceof Integer) {
SelectPhoneContact contact = checkedContactsAsArrayList.get((Integer)position);
}