我有一个带有AutoCompleteTextView的列表视图。对于列表视图,我创建了类似复杂对象类型的适配器。此适配器实现Filterable。一切都好。但是我希望显示像list一样的过滤结果来自复杂对象的一个属性。
private class OrderAdapter extends ArrayAdapter<Request> implements
Filterable {
private Context context;
private int resourceId;
private List<Request> listOrders;
private List<Request> mOriginalValues;
private ListFilter listFilter;
public OrderAdapter(Context context, int resource, List<Request> objects) {
super(context, resource, objects);
this.context = context;
this.resourceId = resource;
this.listOrders = objects;
this.mOriginalValues = new ArrayList<Request>(listOrders);
}
@Override
public int getCount() {
return listOrders.size();
}
@Override
public Request getItem(int pos) {
return listOrders.get(pos);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(LAYOUT_INFLATER_SERVICE);
View rowView = layoutInflater.inflate(resourceId, parent, false);
Request request = listOrders.get(position);
TextView clientName = (TextView) rowView
.findViewById(R.id.client_name);
clientName.setText(request.getClientName());
TextView opportunityNumber = (TextView) rowView
.findViewById(R.id.opportunity_number);
opportunityNumber.setText(request.getOpportunity()
.getOpportunityNumber());
TextView detailedDescription = (TextView) rowView
.findViewById(R.id.product_desc);
detailedDescription.setText(request.getProjectDescription());
TextView ownerName = (TextView) rowView
.findViewById(R.id.requester);
ownerName.setText(request.getOpportunity().getOpportunityOwner());
TextView lastModifiedDate = (TextView) rowView
.findViewById(R.id.last_modified_date_opportunity);
lastModifiedDate.setText(DateTimeConverter.convertDateToString(
request.getOpportunity().getModifiedDate(), context
.getResources().getString(R.string.date_format)));
return rowView;
}
@Override
public Filter getFilter() {
if (listFilter == null) {
listFilter = new ListFilter();
}
return listFilter;
}
private class ListFilter extends Filter {
private Object lock;
@Override
public CharSequence convertResultToString(Object order) {
return ((Request) order).getClientName();
}
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults filterResults = new FilterResults();
if (mOriginalValues == null) {
synchronized (lock) {
mOriginalValues = new ArrayList<Request>(listOrders);
}
}
if (prefix == null || prefix.length() == 0) {
List<Request> list = new ArrayList<Request>(mOriginalValues);
filterResults.values = list;
filterResults.count = list.size();
} else {
String prefixString = prefix.toString().toLowerCase();
List<Request> values = new ArrayList<Request>(
mOriginalValues);
int count = values.size();
List<Request> filteredValues = new ArrayList<Request>(count);
for (Request request : values) {
if (request.getOpportunity().getOpportunityNumber()
.toLowerCase().contains(prefixString)
|| request.getClientName().toLowerCase()
.contains(prefixString)) {
filteredValues.add(request);
}
}
filterResults.values = filteredValues;
filterResults.count = filteredValues.size();
}
return filterResults;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence filterString,
FilterResults results) {
List<Request> values = (ArrayList<Request>) results.values;
if (!values.isEmpty()) {
listOrders = values;
} else {
listOrders = new ArrayList<Request>();
}
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
我应该有2种类型的适配器还是存在另一种方式呢?
答案 0 :(得分:1)
您可以使用一个适配器,
为了支持多个视图类型,您需要覆盖适配器中的两个方法:
1。getViewTypeCount
2。getItemViewType
示例:
/**
* Indicates how many view types this adapter support. two types - incoming and outgoing view
*/
private final static int NUM_VIEW_TYPE = 2;
private final static int VIEW_INCOMING = 0;
private final static int VIEW_OUTGOING = 1;
private static final String TAG = "ConversationAdapter";
public ConversationAdapter(Context context, Cursor c, int threadId) {
super(context, c, 0);
this.inflater = LayoutInflater.from(context);
mThreadId = threadId;
}
@Override
public int getViewTypeCount() {
return NUM_VIEW_TYPE;
}
@Override
public int getItemViewType(int position) {
Cursor c = (Cursor) getItem(position);
int itemType = c.getInt(c.getColumnIndex(Sms.TYPE));
return (itemType == 1 ? VIEW_INCOMING : VIEW_OUTGOING);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
int viewType = getItemViewType(cursor.getPosition());
View RowView = (viewType == VIEW_INCOMING) ? inflater.inflate(
R.layout.conversation_item_received, parent, false) : inflater
.inflate(R.layout.conversation_item_sent, parent, false);
final ViewHolder holder = new ViewHolder();
holder.textBody = (TextView) RowView
.findViewById(R.id.conversation_item_body);
holder.textDate = (TextView) RowView
.findViewById(R.id.conversation_item_date);
holder.imageType = (ImageView) RowView.findViewById(R.id.img_type);
RowView.setTag(holder);
return RowView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final ViewHolder holder = (ViewHolder) view.getTag();
}