我的ListView出现问题。每当我关闭我的应用程序时,它会创建重复项目,当我第三次运行应用程序时,它会向我显示单个项目3次。有什么问题,如何解决?
好的,这是我的LazyAdapter代码
@SuppressLint("DefaultLocale")
public android.widget.Filter getFilter() {
return new android.widget.Filter() {
@SuppressLint("DefaultLocale")
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults Result = new FilterResults();
// if constraint is empty return the original names
if (TextUtils.isEmpty(constraint)){
//Result.values = storedata;
Result.count = listDetails.size();
return Result;
}
ArrayList<items> Filtered_Names = new ArrayList<items>();
String filterString = constraint.toString().toLowerCase();
String filterableString;
for(int i = 0; i<listDetails.size(); i++){
items searchdata = listDetails.get(i);
String title = searchdata.title();
String id = searchdata.id();
String imageUrl = searchdata.imageUrl();
String pprice = searchdata.pprice();
String shippingPrice = searchdata.shippingPrice();
String stock = searchdata.stock();
filterableString = title+" "+id+" "+imageUrl+" "+pprice+"
"+shippingPrice+" "+status+" "+stitle+" "+stock+"
if(filterableString.toLowerCase().contains(filterString)){
Filtered_Names.add(listDetails.get(i));
//Log.e("Added", String.valueOf(Filtered_Names.size()));
}
}
Result.values = Filtered_Names;
Result.count = Filtered_Names.size();
//Log.e("Results", Result.values.toString() +
// String.valueOf(Result.count));
return Result;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// TODO Auto-generated method stub
@SuppressWarnings("unchecked")
ArrayList<items> resultList =(ArrayList<items>) results.values;
LazyAdapter.this.listDetails = resultList;
LazyAdapter.this.notifyDataSetChanged();
}
};
}
lazyAdapter中的构造函数
public LazyAdapte(Context context , ArrayList<items> list)
{
this.context = context;
listDetails = list;
downloader = new ImageDownloader();
}
这是活动代码
public class ItemsActivity extends Activity{
EditText inputsearch;
LazyAdapter adapter;
ArrayList<items> list;
Controller control;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.items_activity);
RelativeLayout layout = (RelativeLayout) findViewById(R.id._view);
control = (Controller) getApplicationContext();
list = (ArrayList<items>) control.Table_items.GetData();
final ListView listview = (ListView)findViewById(R.id._data);
final LazyAdapter adapter = new LazyAdapter(this, list);
adapter.setLayout(this, layout);
listview.setAdapter(adapter);
inputSearch = (EditText) findViewById(R.id.inputsearch);
if(adapter!=null){
}
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
try{
ItemsActivity.this.adapter.getFilter().filter(cs.toString());
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
}
答案 0 :(得分:0)