JSON
[{"MemberId":"MI001","MemberName":"Member 1"},
{"MemberId":"MI002","MemberName":"Member 2"},
{"MemberId":"MI003","MemberName":"Member 3"}]
当我在member
中输入AutoCompleteTextView
时,它将显示其中三个。
[![在此处输入图片描述] [1]] [1]
然后,当我输入member 2
时,它会显示member 2
的值
这里有问题:当我清空AutoCompleteTextView
并再次输入member
时,它仅显示member 2
适配器
public class Adapter_member extends ArrayAdapter<Model_member> implements Filterable{
private ArrayList<Model_member> mData,filteredmember,mDataAll;
public Adapter_member(Context context, ArrayList<Model_member> m_member) {
super(context, 0, m_member);
this.mData = m_member;
this.mDataAll = m_member;
filteredmember = new ArrayList<Model_member>();
}
@Override
public Model_member getItem(int position) {
return mData.get(position);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.single_member_layout, parent, false);
}
TextView memberName = convertView.findViewById(R.id.membername);
TextView memberId = convertView.findViewById(R.id.memberid);
Model_member people = mData.get(position);
if (people != null) {
memberId.setText(people.getMember_id());
memberName.setText(people.getMember_name());
}
return convertView;
}
@Override
public Filter getFilter() {
return myFilter;
}
private Filter myFilter = new Filter() {
@Override
public CharSequence convertResultToString(Object resultValue)
{
String str = resultValue.toString();
return str;
}
@SuppressWarnings("unchecked")
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.d("TA", constraint.toString());
if (constraint != null) {
filteredmember.clear();
for (Model_member people : mDataAll) {
if (people.getMember_name().toLowerCase().startsWith(constraint.toString().toLowerCase())) {
filteredmember.add(people);
}
}
}else{
return new FilterResults();
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredmember;
filterResults.count = filteredmember.size();
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList<Model_member> filteredList = (ArrayList<Model_member>) results.values;
if (results != null && results.count > 0) {
clear();
for (Model_member people : filteredList) {
add(people);
notifyDataSetChanged();
}
}else {
notifyDataSetInvalidated();
}
}
};
}
这是我的音乐。
public class Model_member {
String member_id,member_name;
public Model_member(){
}
public String getMember_id() {
return member_id;
}
public void setMember_id(String member_id) {
this.member_id = member_id;
}
public String getMember_name() {
return member_name;
}
public void setMember_name(String member_name) {
this.member_name = member_name;
}
@Override
public String toString() {
return getMember_id() + " - " +getMember_name();
}
}
我该如何解决我的问题?抱歉,如果我一直在Stackoverflow中询问。
答案 0 :(得分:1)
我已解决问题。感谢downvote。我使用他的MatchableArrayAdapter遵循@pskink方法。这是我的新适配器
public class Adapter_member extends MatchableArrayAdapter<Model_member>{
ArrayList<Model_member> m_member;
public Adapter_member(Context context, int resource, ArrayList<Model_member> m_member) {
super(context, resource , m_member);
}
@Override
protected void onBind(Model_member item, View itemView, int position) {
TextView text1 = (TextView) itemView.findViewById(R.id.memberid);
TextView text2 = (TextView) itemView.findViewById(R.id.membername);
text1.setText(item.getMember_id());
text2.setText(item.getMember_name());
}
@Override
protected boolean matches(Model_member value, CharSequence prefix, CharSequence lowerCasePrefix) {
return value.getMember_name().toLowerCase().contains(lowerCasePrefix);
}
}