У меня есть список с пользовательскими строками внутри него. Всякий раз, когда я использую фильтр в моем поисковом окне, появляется только первый элемент, когда он может что-то фильтровать. Я использую Массивы для хранения данных.
Ex.
items: A, B, C
Если я ищу «A», он показывает «A» (первые данные, или в данном случае строку), и всякий раз, когда я ищу «B» или «C», он все равно показывает «A» , Тогда всякий раз, когда я ищу что-то, кроме предметов, например, «H», оно ничего не показывает (что правильно).
Я бы хотел найти их по имени контакта "mContact".
Вот мои коды:
MainActivity.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);
}