我正在尝试在列表视图中执行搜索。 Listview从db获取所有信息并显示它们。问题是搜索的ArrayAdapter与listview的ListAdapter存在冲突。因为如果在ListAdapter之前执行搜索不起作用,那么如果我将它放在ListAdapter搜索工作下面,但在另一页上不会显示其他信息。
private void updateList() {
ListView lv = getListView();
Set<String> unionSet = new HashSet<String>();
for (HashMap<String, String> hashMap : mPlatesList) {
for(String key : hashMap.keySet())
if(key.equals(TAG_TARGA))
unionSet.add(hashMap.get(key));
}
String[] table = unionSet.toArray(new String[unionSet.size()]);
inputSearch = (EditText) findViewById (R.id.searchText);
myAdapter = new ArrayAdapter<String>(this, R.layout.plates_search, R.id.plates_search, table);
lv.setAdapter(myAdapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
Lista.this.myAdapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
ListAdapter adapter = new SimpleAdapter(this, mPlatesList,
R.layout.plates, new String[] { TAG_TARGA, TAG_NGJYRA, TAG_MARKA, TAG_SIGURACION, TAG_GJOBA, TAG_PRONAR, TAG_SGS },
new int[] { R.id.title, R.id.ngjyra, R.id.marka, R.id.siguracion, R.id.gjoba, R.id.pronar, R.id.sgs});
setListAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String targa = ((TextView)view.findViewById(R.id.title)).getText().toString();
String ngjyra = ((TextView)view.findViewById(R.id.ngjyra)).getText().toString();
String marka = ((TextView)view.findViewById(R.id.marka)).getText().toString();
String siguracion = ((TextView)view.findViewById(R.id.siguracion)).getText().toString();
String gjoba = ((TextView)view.findViewById(R.id.gjoba)).getText().toString();
String sgs = ((TextView)view.findViewById(R.id.sgs)).getText().toString();
String pronar = ((TextView)view.findViewById(R.id.pronar)).getText().toString();
String username = getIntent().getExtras().getString("Username");
Intent intent = new Intent(Lista.this,Info.class);
intent.putExtra("Targa",targa);
intent.putExtra("Ngjyra",ngjyra);
intent.putExtra("Marka",marka);
intent.putExtra("Gjoba",gjoba);
intent.putExtra("Siguracion",siguracion);
intent.putExtra("Sgs",sgs);
intent.putExtra("Pronar",pronar);
intent.putExtra("Username",username);
startActivity(intent);
}
});
}
问题是如果搜索在ListAdapter之上,则显示在另一页上的R.id.title, R.id.ngjyra, R.id.marka, R.id.siguracion, R.id.gjoba, R.id.pronar, R.id.sgs
为空,否则显示另一页上的信息但搜索不起作用。怎么办?
答案 0 :(得分:0)
在OnItemClickListener
中,您不应该从列表中读取数据来源。由于这个原因,您已通过position
,因此您应该能够从table
答案 1 :(得分:0)
您可以在此处参考CustomSearch功能,您需要为此编写CustomAdapter - &gt; ReferThis
public void search() {
ListView lv = getListView();
Set<String> unionSet = new HashSet<String>();
List<Map<String, String>> mPlateClone = new ArrayList<Map<String, String>>(
mPlatesList);
for (Map<String, String> hashMap : mPlateClone) {
for (String key : hashMap.keySet())
if (key.equals(TAG_TARGA))
unionSet.add(hashMap.get(key));
}
String[] table = unionSet.toArray(new String[unionSet.size()]);
inputSearch = (EditText) findViewById(R.id.searchText);
// myAdapter = new ArrayAdapter<String>(this, R.layout.plates,
// R.id.title,
// table);
// lv.setAdapter(myAdapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable s) {
// // ((SimpleAdapter)
// //
// adapter).getFilter().filter(inputSearch.getText().toString().trim());
((SimpleAdapter) Lista.this.adapter).getFilter().filter(
inputSearch.getText().toString().trim());
}
});
}