为什么searchview只输出列表视图中的第一项?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么searchview只输出列表视图中的第一项?相关的知识,希望对你有一定的参考价值。
我有一个listview,里面有自定义行。每当我在我的搜索视图上使用过滤器时,只有第一个项目可以在过滤某些内容时显示。我使用Arrays来存储数据。
防爆。
items: A, B, C
如果我搜索“A”,它会显示“A”(第一个数据,或者在本例中为行),每当我搜索“B”或“C”时,它仍会显示“A”。然后,每当我搜索除项目之外的任何内容(例如“H”)时,它都不会显示任何内容(这是正确的)。
我想通过联系人的名字“mContact”搜索它们。
这些是我的代码:
main activity.Java
ListView listView;
TextView totalText;
String currency = "₱";
String mContact[] = {"Craig", "Agatha", "Dave", "Brandon", "Russel", "Gleceper", "Percy", "Test"};
double mDebt[] = {2000, 525, 8000, 5000, 955, 4000, 50123, 51247};
double mDebtTotal = 0;
String mDesc[] = {"This is a description... etc", "", "", "", "", "", "", ""};
String mDate[] = {"Apr. 21, 2016", "", "", "", "", "", "", ""};
int mImg[] = {R.drawable.debtcontact_craig, R.drawable.debtcontact_agatha, R.drawable.debtcontact_dave, R.drawable.debtcontact_brandon, R.drawable.debtcontact_russel, R.drawable.debtcontact_gleceper, R.drawable.debtcontact_lola, R.drawable.ic_launcher_background};
MyAdapter adapter;
DecimalFormat formatter = new DecimalFormat("###,###,###.##");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Set text of Total based on total amt of mDebt*/
totalText = findViewById(R.id.TotalTextView);
for (double x: mDebt) { //Compute total mDebt
mDebtTotal += x;
}
if (mDebtTotal == 0){ //If mDebtTotal == 0
totalText.setText("Create a debt with an amount first.");
} else {
totalText.setText("Total: " + currency + " " + formatter.format(mDebtTotal));
}
/* Display contents of listview*/
listView = findViewById(R.id.listView);
adapter = new MyAdapter(this, mContact, mDebt, mDesc, mDate, mImg);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
makeDialog(mContact[i], mDebt[i], mDesc[i], mDate[i]);
}
});
}
class MyAdapter extends ArrayAdapter<String> {
Context context;
String rContact[];
double rDebt[];
String rDesc[];
String rDate[];
int rImg[];
MyAdapter(Context c, String contact[], double debt[], String desc[], String date[], int img[]){
super(c, R.layout.listview_row, R.id.Contact, contact);
this.context = c;
this.rContact = contact;
this.rDebt = debt;
this.rDesc = desc;
this.rDate = date;
this.rImg = img;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = layoutInflater.inflate(R.layout.listview_row, parent, false);
ImageView images = row.findViewById(R.id.image);
TextView myContact = row.findViewById(R.id.Contact);
TextView myDebt = row.findViewById(R.id.Debt);
TextView myDesc = row.findViewById(R.id.Desc);
TextView myDate = row.findViewById(R.id.Date);
images.setImageResource(rImg[position]);
myContact.setText(rContact[position]);
myDebt.setText(currency + " " + formatter.format(rDebt[position])); /* Adds currency at the beginning */
myDesc.setText(rDesc[position]);
myDate.setText(rDate[position]);
return row;
}
}
MainActivity.java SearchView
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.app_searchbutton);
searchItem.getActionView();
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Enter a contact e.g 'Robert'");
searchView.setIconified(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getApplicationContext(), query, Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText.toString());
adapter.notifyDataSetChanged();
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
答案
ArrayAdapter创建在构造函数中发送的数组的副本,并将该副本继续用于所有操作。因此,当您调用filter()时,该副本将被修改而不是rContact数组。因此,当适配器决定在列表中仅显示1个项目时,您将填充rContact中始终为“A”的第一个项目。
更换
myContact.setText(rContact[position]);
同
myContact.setText(getItem[position]);
但是你必须考虑一种方法来自己填充其他领域的正确数据。
以上是关于为什么searchview只输出列表视图中的第一项?的主要内容,如果未能解决你的问题,请参考以下文章