我一直在努力,但我无法找出问题所在。 在我的情况下,我得到了几个充满数据的“模块”。可以使用EditText过滤列表,但它不会在我的自定义适配器中返回正确的行ID,即使它是相同的(除了对象不同)。
这是我的2个文件,在RecordsListAdapter.java中,过滤器工作正常,在ContactsListAdapter.java中它返回错误的rowID。
希望你能帮助我!
- 编辑 - 我认为问题在于返回了rowID,它没有返回'原始'rowID,它返回'过滤'的rowID,因此在两种情况下点击第一行都会返回1,但是在过滤时它不应该返回。
package nl.cuse.crm.adapters;
import java.util.ArrayList;
import nl.cuse.crm.objects.lists.RecordsListItem;
import android.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;
public class RecordsListAdapter extends ArrayAdapter<RecordsListItem> {
private ArrayList<RecordsListItem> original;
private ArrayList<RecordsListItem> filtered;
private ArrayList<RecordsListItem> fulllist;
private Filter filter;
int resource;
public RecordsListAdapter(Context context, int textViewResourceId,
ArrayList<RecordsListItem> items) {
super(context, textViewResourceId, items);
this.original = items;
this.fulllist = new ArrayList<RecordsListItem>(items);
this.filtered = new ArrayList<RecordsListItem>(items);
resource = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.simple_list_item_1, null);
}
RecordsListItem m = original.get(position);
if (m != null) {
TextView tt = (TextView) v.findViewById(R.id.text1);
tt.setText(m.get_name());
} else {
System.out.println("m is null");
}
return v;
}
@Override
public Filter getFilter() {
if (filter == null)
filter = new CustomFilter();
return filter;
}
private class CustomFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
System.out.println("Called it: " + constraint);
FilterResults results = new FilterResults();
String prefix = constraint.toString().toLowerCase();
if ((prefix == null) || (prefix.length() == 0)) {
ArrayList<RecordsListItem> list = new ArrayList<RecordsListItem>(
fulllist);
results.values = list;
results.count = list.size();
} else {
final ArrayList<RecordsListItem> list = new ArrayList<RecordsListItem>(
fulllist);
final ArrayList<RecordsListItem> nlist = new ArrayList<RecordsListItem>();
int count = list.size();
for (int i = 0; i < count; i++) {
final RecordsListItem pkmn = list.get(i);
final String value = pkmn.get_name().toLowerCase();
if (value.contains(prefix)) {
nlist.add(pkmn);
}
}
results.values = nlist;
results.count = nlist.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
filtered = (ArrayList<RecordsListItem>) results.values;
clear();
int count = filtered.size();
for (int i = 0; i < count; i++) {
RecordsListItem pkmn = filtered.get(i);
add(pkmn);
}
}
}
}
package nl.cuse.crm.adapters;
import java.util.ArrayList;
import nl.cuse.crm.R;
import nl.cuse.crm.objects.lists.ContactListItem;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;
public class ContactListAdapter extends ArrayAdapter<ContactListItem> {
private ArrayList<ContactListItem> original;
private ArrayList<ContactListItem> filtered;
private ArrayList<ContactListItem> fulllist;
private Filter filter;
int resource;
public ContactListAdapter(Context context, int textViewResourceId,
ArrayList<ContactListItem> items) {
super(context, textViewResourceId, items);
this.original = items;
this.fulllist = new ArrayList<ContactListItem>(items);
this.filtered = new ArrayList<ContactListItem>(items);
resource = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.custom_list_item_contacts, null);
}
ContactListItem m = original.get(position);
if (m != null) {
TextView tt = (TextView) v.findViewById(R.id.rd_text1);
TextView tt2 = (TextView) v.findViewById(R.id.rd_text2);
if (tt != null) {
tt.setText(m.get_firstname() + " " + m.get_lastname());
}
if (tt2 != null) {
tt2.setText(m.get_title());
}
}
return v;
}
@Override
public Filter getFilter() {
if (filter == null)
filter = new CustomFilter();
return filter;
}
private class CustomFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
System.out.println("Called it: " + constraint);
FilterResults results = new FilterResults();
String prefix = constraint.toString().toLowerCase();
if ((prefix == null) || (prefix.length() == 0)) {
ArrayList<ContactListItem> list = new ArrayList<ContactListItem>(
fulllist);
results.values = list;
results.count = list.size();
} else {
final ArrayList<ContactListItem> list = new ArrayList<ContactListItem>(
fulllist);
final ArrayList<ContactListItem> nlist = new ArrayList<ContactListItem>();
int count = list.size();
for (int i = 0; i < count; i++) {
final ContactListItem pkmn = list.get(i);
final String value = pkmn.get_firstname().toLowerCase()
+ "" + pkmn.get_lastname().toLowerCase();
if (value.contains(prefix)) {
nlist.add(pkmn);
}
}
results.values = nlist;
results.count = nlist.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
filtered = (ArrayList<ContactListItem>) results.values;
clear();
int count = filtered.size();
for (int i = 0; i < count; i++) {
ContactListItem pkmn = filtered.get(i);
add(pkmn);
}
}
}
}
答案 0 :(得分:0)
我刚刚发现了我做错了什么,并修好了。我将为其他人解释同样的问题。当过滤时,列表获取新索引,因此列表中的第一个始终是第1行。我的侦听器是使用ArrayList.get(rowID)编写的,当rowID被更改时,它在过滤时不起作用。我为解决这个问题所做的是制作一个ArrayAdapter的副本并过滤掉那个,这显然改变了过滤器的列表索引