gpt4 book ai didi

java - ClassCastException,为什么我的保存按钮在单击时没有将选中的复选框保存到我的数据库中?

转载 作者:行者123 更新时间:2023-12-02 11:37:34 34 4
gpt4 key购买 nike

我有一个 EditContact 类。打开后,它会显示哪些复选框已选中或未选中。这是通过我的适配器中的一些代码完成的,它可以正常工作:

    //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 类中,用户可以选中或取消选中联系人列表中的框。如果选中它们,则应保存它们,并且适配器中的代码应反射(reflect)新的checkedContactsAsArrayList,但无论选中什么,它始终会保存为空。

System.out中,我可以看到我们处于尝试部分,然后它直接进入EditContact:不幸的是,这里有一个问题。您能告诉我出了什么问题以及如何解决吗?

这是我的保存按钮的代码:

  //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();
}


});
}

logcat不幸的是下面有一个问题,它说:

java.lang.ClassCastException:java.lang.Integer 无法转换为 com.example.chris.tutorialspoint.SelectPhoneContact,位于 第 318 行,即

SelectPhoneContact contact = (SelectPhoneContact) checkbox.getTag();

但不知道如何修复它。

在我的适配器onBindViewHolder中:

//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);
}
}

}

还有我的SelectPhoneContact类:

public class SelectPhoneContact {

String phone;

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

boolean isMatching;

public boolean isMatching(){return isMatching;}

public void setIsMatchingContact(boolean isMatching){

this.isMatching = isMatching;

}



//*****************************************
//this is for the checkbox
//by default, make it unchecked
boolean isSelected = false;

public boolean getSelected() {
return isSelected;
}

public void setSelected(boolean selected){

isSelected = selected;

}


String type_row;

public String getType_row() {
return type_row;
}

public void setType_row(String type_row) {
this.type_row = type_row;
}



}

最佳答案

问题是您将整数设置为标签:

((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);
}

关于java - ClassCastException,为什么我的保存按钮在单击时没有将选中的复选框保存到我的数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48813332/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com