我想知道如何基于与字符串的相关性来组织我的列表视图?这在Android中是否可行?
所以我要说结果是:
“你好” “GOD BLESS H” “找到嗨”
,字符串为“Hi”
它将根据与字符串
的相关性进行组织编辑1: 我的自定义ArrayAdapter列表视图:
public class MyAdapter extends ArrayAdapter<AdapterItem> {
private List<AdapterItem> items = new ArrayList<AdapterItem>();
public MyAdapter(Context context, int textviewid) {
super(context, textviewid);
}
public void addAdapterItem(AdapterItem item) {
items.add(item);
}
public int getCount() {
return items.size();
}
@Override
public AdapterItem getItem(int position) {
return ((null != items) ? items.get(position) : null);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
View rowView = getLayoutInflater().inflate(R.layout.itemview, null);
TextView firstTextView = (TextView) rowView.findViewById(R.id.txtTitle);
firstTextView.setText(items.get(position).first);
// do the same with second and third
return rowView;
}
}
My Comparer&amp; AdapterItem
public class TComparator implements Comparator<AdapterItem> {
private final Collator collator = Collator.getInstance();
public int compare(AdapterItem ob1, AdapterItem ob2) {
return collator.compare(ob1.first, string);
}
}
class AdapterItem {
public String first;
public String second;
public String third;
public int fourth;
public AdapterItem(String first, String second, String third, int fourth) {
this.first = first;
this.second = second;
this.third = third;
this.fourth = fourth;
}
}
答案 0 :(得分:1)
您可以为相关性制作启发式,然后遍历适配器的元素,对它们进行评分,然后根据分数对它们进行排序,然后在适配器中重新排列它们(或者您可能需要制作新的适配器),然后将listview设置为新适配器并更新。
看起来好像很多工作......有一些内置的方法来猜测这个人想要输入什么,其中一个是通过搜索界面:我在这里读到它:{{3 }}
编辑:
实际上,如果使用ArrayAdapter,则有一个sort()方法,您可以使用比较器对适配器进行排序:http://developer.android.com/guide/topics/search/search-dialog.html
你可以使用你的启发式作为比较器并按顺序对适配器进行排序!
EDIT2:
假设您的ArrayAdapter是T类型,那么您的适配器如下所示:
ArrayAdapter<T> myAdapter = ...
然后你这样做:
public class TComparator implements Comparator<T> {
public int compare(T ob1, T ob2) {
if (ob1 is more relevant than ob2) {
return 1;
} else if (ob1 is less relevant than ob2) {
return -1;
} else (ob1 is of equal relevance to ob2) {
return 0;
}
}
}
您如何确定相关性,以便您了解并依赖于任务
然后你可以简单地做
myAdapter.sort(new TComparator());
并对适配器进行排序