我正在尝试使用SearchView更新ListAdapter并删除项目,但它不起作用。
一个例子:
1 A23-01 2015-10-11 2324 no info
2 A23-45 2015-10-11 5363 no info
3 A24-51 2015-10-15 3336 no info
4 A24-34 2015-10-15 9389 no info
5 A24-03 2015-10-17 0304 no info
使用SearchView,过滤“A24 - ”:
3 A24-51 2015-10-15 3336 no info
4 A24-34 2015-10-15 9389 no info
5 A24-03 2015-10-17 0304 no info
删除第4项,ListAdapter未更新。这段代码出了什么问题?
我的代码:
public class SuperList extends ListActivity {
ArrayList<HashMap<String, String>> listdata = new ArrayList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...code omitted...
Cursor c = db.query(tabledef, null, null, null, null, null, null);
int ColumnasTotal = c.getColumnCount();
while(c.moveToNext()){
HashMap<String, String> hm = new HashMap<String, String>();
for (int i = 0; i < ColumnasTotal; i++) {
String key = c.getColumnName(i);
String value = c.getString(i);
hm.put(key, value);
}
listdata.add(hm);
}
}
...code omitted...
String[] from = { "device" , "date" , "number" , "info"};
int[] to = { R.id.Device, R.id.Date, R.id.Number, R.id.Info };
ListAdapter adapter = new SimpleAdapter(SuperList.this, listdata, R.layout.activity_super_list, from, to);
setListAdapter(adapter);
@Override
protected void onListItemClick(ListView listView, View view, final int position, long id) {
final CharSequence[] items = { "Add", "View", "Delete" };
AlertDialog.Builder builder = new AlertDialog.Builder(SuperList.this);
builder.setTitle("¿QUE DESEA HACER?");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
ListAdapter adapter = getListAdapter();
Object userobj = adapter.getItem(position);
int position = listdata.indexOf(userobj);
@SuppressWarnings("unchecked")
HashMap<String, String> userdata = (HashMap<String, String>) userobj;
String whereClause = "Id=" + userdata.get("Id");
String idIncidente = listdata.get(position).get("Id");
switch (item) {
case 0:
// ADD
...code omitted...
break;
case 1:
// View
Intent explicit_intent = new Intent(SuperList.this, InfoClient.class);
explicit_intent.putExtra("userdata", userdata);
startActivity(explicit_intent);
break;
case 2:
// Delete
listdata.remove(position);
((SimpleAdapter) adapter).notifyDataSetChanged();
// ***********************
// *** WHEN DELETE A ITEM, SimpleAdapter IS NOT UPDATED USING SearchView *** //
// ***********************
break;
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.super_list, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setQueryHint("Search...");
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextChange(String newQueryText){
ListAdapter adapter = SuperList.this.getListAdapter();
((SimpleAdapter) adapter).getFilter().filter(newQueryText);
((SimpleAdapter) adapter).notifyDataSetChanged();
return true;
}
@Override
public boolean onQueryTextSubmit(String query){
// Do nothing
return true;
}
}
);
return true;
}
}
任何人都可以提供建议吗?