gpt4 book ai didi

Android 在过滤 ListView 时未选中复选框

转载 作者:行者123 更新时间:2023-11-30 02:31:22 25 4
gpt4 key购买 nike

以下用于过滤 listView 的代码是正确的,没有任何问题。但是在搜索并选中复选框并按 Android 键盘上的 BackSpace 键清除字符和字符串开始后,所有复选框都未选中,我不想拥有此功能,我想在搜索或清除可搜索的 EditText 时选中 listView。

public class ContactsAdapter extends ArrayAdapter<ContactListStructure>  implements Filterable {
private ArrayList<ContactListStructure> item = new ArrayList<ContactListStructure>();
private ArrayList<ContactListStructure> originalList;
private NameFilter filter;
public ContactsAdapter (ArrayList<ContactListStructure> data) {
super(G.context, R.layout.send_sms, data);
item = data;
originalList = new ArrayList<ContactListStructure>();
originalList.addAll(data);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
ContactListStructure item = getItem(position);
if (convertView == null) {
convertView = G.inflater.inflate(R.layout.send_sms, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.fill(this, item, position);
return convertView;
}

private static class ViewHolder {
private CheckBox chk_name_mobile;
private ImageView photo;

public ViewHolder(View view) {
chk_name_mobile = (CheckBox) view.findViewById(R.id.chk_name_mobile);
photo = (ImageView) view.findViewById(R.id.photo);
}

public void fill(final ArrayAdapter<ContactListStructure> adapter, final ContactListStructure item, final int position) {
chk_name_mobile.setText ( item.name );
if( item.checked ){
chk_name_mobile.setChecked( true );
if( item.photo != null ) photo.setImageBitmap(item.photo);
}else{
chk_name_mobile.setChecked( false );
if( item.photo == null )
photo.setImageDrawable( G.context.getResources().getDrawable(R.drawable.user) );
else
photo.setImageBitmap(item.photo);
}

chk_name_mobile.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if( isChecked ){
item.checked = true;
}else{
item.checked = false;
}
}
});
}
}
@Override
public Filter getFilter() {
if (filter == null){
filter = new NameFilter();
}
return filter;
}
private class NameFilter extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
if(constraint != null && constraint.toString().length() > 0)
{
ArrayList<ContactListStructure> filteredItems = new ArrayList<ContactListStructure>();
for(int i = 0, l = originalList.size(); i < l; i++)
{
ContactListStructure nameList = originalList.get(i);
if(nameList.name.toString ().contains(constraint)) {
filteredItems.add ( nameList );
}
}
result.count = filteredItems.size();
result.values = filteredItems;
}
else
{
synchronized(this)
{
result.values = originalList;
result.count = originalList.size();
}
}
return result;
}

@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
item = (ArrayList<ContactListStructure>)results.values;
notifyDataSetChanged();
clear();
for(int i = 0, l = item.size(); i < l; i++)
add(item.get(i));
notifyDataSetInvalidated();
}
}
}

最佳答案

我已经为您修改了代码。首先阅读并理解代码,然后在你的程序中使用它:)

Model Class:

package com.example.model;

public class ContactListStructure {
String name;
boolean check;

public ContactListStructure(String name, boolean check) {
super();
this.name = name;
this.check = check;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isCheck() {
return check;
}
public void setCheck(boolean check) {
this.check = check;
}


}


In main activity;

namesarrayList=new Arraylist<ContactListStructure >();

ContactListStructure conliststruct;
for(int i=0;i<namesarray.length();i++)
{
conliststruct=new ContactListStructure(namesarray[i], false);
namesarrayList.add(conliststruct);
}
ContactsAdapter adapter=new ContactsAdapter (namesarrayList);
listview.setAdapter(adapter);





Adapter Class:

public class ContactsAdapter extends ArrayAdapter<ContactListStructure> implements Filterable {
private ArrayList<ContactListStructure> filteritemList = new ArrayList<ContactListStructure>();
private ArrayList<ContactListStructure> originalList;
private NameFilter filter;
private CheckBox chk_name_mobile;
private ImageView photo;



public ContactsAdapter (ArrayList<ContactListStructure> data) {
super(G.context, R.layout.send_sms, data);

this.originalList = new ArrayList<ContactListStructure>();
originalList.addAll(data);
this.filteritemList = new ArrayList<ContactListStructure>(originalList);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos=position;

ContactListStructure item = (ContactListStructure)getItem(position);
if (convertView == null) {
convertView = G.inflater.inflate(R.layout.send_sms, parent, false);


} else {

}

chk_name_mobile = (CheckBox) convertView.findViewById(R.id.chk_name_mobile);
photo = (ImageView) convertView.findViewById(R.id.photo);
chk_name_mobile.setText(item.getName().toString());
chk_name_mobile.setChecked(item.isCheck());
chk_name_mobile.setTag(item);

chk_name_mobile.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
ContactListStructure itemobj=(ContactListStructure) cb.getTag();


if (originalList.get(Integer.valueOf(pos)).isCheck()) {
cb.setSelected(false);
originalList.get(Integer.valueOf(pos)).setCheck(false);
notifyDataSetChanged();
} else {
cb.setSelected(true);
originalList.get(Integer.valueOf(pos)).setCheck(true);
notifyDataSetChanged();
}

//region.setCheck(chkItem.isChecked());
}
});
return convertView;
}

@Override
public Filter getFilter() {
if (filter == null){
filter = new NameFilter();
}
return filter;
}
private class NameFilter extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
if(constraint != null && constraint.toString().length() > 0)
{
ArrayList<ContactListStructure> filteredItems = new ArrayList<ContactListStructure>();
for(int i = 0, l = filteritemList.size(); i < l; i++)
{
ContactListStructure nameList = filteritemList.get(i);
if(nameList.name.toString ().contains(constraint)) {
filteredItems.add ( nameList );
}
}
result.count = filteredItems.size();
result.values = filteredItems;
}
else
{
synchronized(this)
{
result.values = filteritemList;
result.count = filteritemList.size();
}
}
return result;
}

@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
originalList = (ArrayList<ContactListStructure>)results.values;
notifyDataSetChanged();
clear();
for(int i = 0, l = originalList.size(); i < l; i++)
add(originalList.get(i));
notifyDataSetInvalidated();
}
}
}

关于Android 在过滤 ListView 时未选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27290841/

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