gpt4 book ai didi

Android:Listview 不保存复选框的状态

转载 作者:行者123 更新时间:2023-11-29 21:36:25 24 4
gpt4 key购买 nike

我检查了所有有类似问题的主题,但我仍然停留在死点。我有一个带有联系人列表的 ListView ,但是当我向下滚动和向上滚动 ListView 时,不保存我的选择状态。所有复选框都设置为 false。当我调试时,我看到,当我滚动 ListView 时,我的 onClickListener 认为复选框已签名并重置它。我做了所有事情,其他带有位置 bool 值的数组等等,但我仍然不知道该怎么做:(我将非常感谢任何帮助:)

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
readContacts();
Contact_Database dbhandler = new Contact_Database(
getApplicationContext(), null, null, 1);
numbers.addAll( dbhandler.getAllNumbers());
System.out.println("onCreate");
ListView lv = getListView();
registerForContextMenu(lv);
lv.setTextFilterEnabled(true);
lv.clearChoices();
MyAdapter adapter = (new MyAdapter(this, R.layout.contact_list_sms, contacts));
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);

}

/**
* When I destroy intent i send request to database, faster!
*/

@Override
protected void onDestroy() {
super.onDestroy();
for (int i = 0; i < numbers.size(); ++i)
System.out.println("@@@@" + numbers.get(i));

swap_contacts();

}

class MyAdapter extends ArrayAdapter<Contact> {

LayoutInflater inflat;

public MyAdapter(Context context, int textViewResourceId,
ArrayList<Contact> objects) {
super(context, textViewResourceId, objects);
inflat = LayoutInflater.from(context);
System.out.println("LOLLLLL");
notifyDataSetChanged();

}

@Override
public int getItemViewType(int position) {
return (position == this.getCount() - 1) ? 1 : 0;
}

@Override
public int getViewTypeCount() {
return 2;
}

// if that contact exist in database? numbers, in onCreate method
// i init this with saved position from databse
private boolean exist(String x) {
for (int i = 0; i < numbers.size(); i++)
if (numbers.get(i).compareTo(x) == 0)
return true;
return false;
}

@SuppressLint("NewApi")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("ROWS !!! ->" + numbers.size());
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflat.inflate(R.layout.contact_row, null);
holder.textView1 = (TextView) convertView
.findViewById(R.id.name);
holder.textView2 = (TextView) convertView
.findViewById(R.id.number);
holder.on_off = (CheckBox) convertView
.findViewById(R.id.enable);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Contact it = contacts.get(position);
Log.v("XXX", holder.toString());
if (it != null) {
holder.textView1.setText(contacts.get(position).name);
holder.textView2.setText(contacts.get(position).phone);
// update state of checbox from database
if (exist(contacts.get(position).phone))
holder.on_off.setChecked(true);
else
holder.on_off.setChecked(false);
}

final int element_position = position;

holder.on_off
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
// update list when we click on it
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
String nr_telefonu = contacts.get(element_position).phone;
if (isChecked) {
numbers.add(nr_telefonu);
} else {
numbers.remove(nr_telefonu);
}
}
});
return convertView;
}


@Override
public Contact getItem(int position) {
return contacts.get(position);
}

private class ViewHolder {
TextView textView1, textView2;
CheckBox on_off;

public String toString() {
return "-";
}
}
}

最佳答案

列表在滚动时不会保存它的状态,因为每次都会为行项目创建新对象。因此,您需要在 Pojo 类(Setter 和 getter)的帮助下显式保存 CheckBox 的状态。

在 getView() 方法中,您需要检查每个复选框的值。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
////////////////////

并且在 setOnCheckedChangeListener 上你需要保存复选框的状态

                  holder.on_off
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
// update list when we click on it
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// need to save the state

更多细节请引用下面的博客

http://amitandroid.blogspot.in/2013/03/android-listview-with-checkbox-and.html

您可以根据自己的需要进行更改。

关于Android:Listview 不保存复选框的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18320772/

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