私はずっと努力してきましたが、何が問題なのかわかりません。私の場合、データで満たされたいくつかの「モジュール」を取得しました。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);
}
}
}
}