我是Android开发的新手。这是我的代码。实际上它的作用是生成一个列表,并使用EditText
框搜索列表中的特定记录。当我在列表中搜索时,它会给我记录,但当我将其值从“替换”修改为“修复”或“修复为替换”时,它会在后端成功更改数组,但用户界面不会更改。
代码:
public class listViewAdapter_clmform_prtLst extends
ArrayAdapter<PartList_Holder> {
private ArrayList<PartList_Holder> originalList;
private ArrayList<PartList_Holder> countryList;
private CountryFilter filter;
int index;
Context mcontext;
ViewHolder holder;
String search = "0";
public listViewAdapter_clmform_prtLst(Activity context,
int rowQuoteapprovel, ArrayList<PartList_Holder> countryList) {
super(context, rowQuoteapprovel, countryList);
this.mcontext = context;
this.countryList = new ArrayList<PartList_Holder>();
this.countryList.addAll(countryList);
this.originalList = new ArrayList<PartList_Holder>();
this.originalList.addAll(countryList);
}
@Override
public Filter getFilter() {
if (filter == null) {
filter = new CountryFilter();
}
return filter;
}
class ViewHolder {
TextView partname;
CheckBox repair;
CheckBox replace;
}
@SuppressLint("CutPasteId")
@Override
public View getView(final int position, View convertView,
final ViewGroup parent) {
holder = null;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) mcontext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.row_quoteapprovel, null);
holder = new ViewHolder();
holder.partname = (TextView) convertView
.findViewById(R.id.tv_partname);
holder.repair = (CheckBox) convertView
.findViewById(R.id.CheckBox01);
holder.replace = (CheckBox) convertView
.findViewById(R.id.CheckBox02);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final PartList_Holder country = countryList.get(position);
final CheckBox repair = (CheckBox) convertView
.findViewById(R.id.CheckBox01);
final CheckBox replace = (CheckBox) convertView
.findViewById(R.id.CheckBox02);
repair.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ind(country.getPartName());
holder.repair.setChecked(true);
Constants.repair[index] = "true";
holder.replace.setChecked(false);
Constants.replace[index] = "false";
// notifyDataSetChanged();
}
});
replace.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ind(country.getPartName());
holder.replace.setChecked(true);
Constants.replace[index] = "true";
holder.repair.setChecked(false);
Constants.repair[index] = "false";
// notifyDataSetChanged();
}
});
holder.partname.setText(country.getPartName());
if (country.getRepair().equals("true")) {
holder.repair.setChecked(true);
} else {
holder.repair.setChecked(false);
}
if (country.getReplace().equals("true")) {
holder.replace.setChecked(true);
} else {
holder.replace.setChecked(false);
}
return convertView;
}
public void ind(String text) {
index = -1;
for (int i = 0; i < Constants.parts.length; i++) {
if (Constants.parts[i].equals(text)) {
index = i;
break;
}
}
}
private class CountryFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
search = (String) constraint;
FilterResults result = new FilterResults();
if (constraint != null && constraint.toString().length() > 0) {
ArrayList<PartList_Holder> filteredItems = new ArrayList<PartList_Holder>();
for (int i = 0, l = originalList.size(); i < l; i++) {
PartList_Holder country = originalList.get(i);
if (country.toString().toLowerCase().contains(constraint))
filteredItems.add(country);
}
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) {
countryList = (ArrayList<PartList_Holder>) results.values;
notifyDataSetChanged();
clear();
for (int i = 0, l = countryList.size(); i < l; i++)
add(countryList.get(i));
notifyDataSetInvalidated();
}
}
}
搜索之前查看修复和替换的第一行值
如果我搜索成功显示的第一个值,我可以选中并取消选中单选按钮。
但我回去意味着清除Edittext视图中的字符,它显示以前的值
答案 0 :(得分:0)
在publishResults
方法中,在每次搜索时,都会为您的基础适配器数据分配给新的:
countryList = (ArrayList<PartList_Holder>)results.values;
所以你不能保存选定/未选中状态。
一般情况下,您不需要originalList
,也不要修改countryList
。
所以看起来应该是这样的:
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
search = (String) constraint;
FilterResults result = new FilterResults();
if (TextUtils.isEmpty(constraint)) {
results.count = contacts.size();
results.values = contacts;
return results;
}
ArrayList<PartList_Holder> filteredItems = new ArrayList<PartList_Holder>();
for (int i = 0, l = originalList.size(); i < l; i++) {
PartList_Holder country = countryList.get(i);
if (country.toString().toLowerCase().contains(constraint))
filteredItems.add(country);
}
result.count = filteredItems.size();
result.values = filteredItems;
}
return result;
}
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
addAll((List<PartList_Holder>) results.values);
}