我正在使用带有自定义适配器的ListView(ListItem包含四个TextView)。在同一个Activity上,我有一个Spinner。当用户在Spinner中选择一个项目时,我想根据所选项目过滤ListView(这四个文本视图中的一个是将与Spinner的所选项目进行比较的那个。)
我还有一个" All" Spinner中的选项将删除ListView过滤器。
我已经尝试了很长时间了,但似乎没有用。会发生什么,
当我第一次选择任何项目时,我只看到第0个ListItem。
因此,如果我再次选择任何其他项目,则ListView无效。
当我选择"全部"时,ListView会加载所有项目,然后当我选择任何项目时,它会按预期过滤。所以,每次我必须选择" All"然后选择目标项目。
这是Spinner的ItemSelected事件:
String[] items = new String[]{"All", "Initiated", "Processing", "Rescheduled", "Completed", "Cancelled"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
spinnerStatus= (Spinner) findViewById(R.id.spinnerStatus);
spinnerStatus.setAdapter(adapter);
spinnerStatus.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedStatus = parent.getItemAtPosition(position).toString();
if (placedRequestsItemAdapter!=null)
{
placedRequestsItemAdapter.getFilter().filter(selectedStatus, new Filter.FilterListener() {
@Override
public void onFilterComplete(int count) {
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
这是我的自定义适配器:
public class PlacedRequestsItemAdapter extends BaseAdapter implements Filterable {
private Context context;
List<PlacedRequestItemModel> placedRequests;
ValueFilter valueFilter;
List<PlacedRequestItemModel> mStringFilterList;
public PlacedRequestsItemAdapter(Context context, List<PlacedRequestItemModel> placedRequests) {
this.context = context;
this.placedRequests = placedRequests;
this.mStringFilterList = placedRequests;
}
@Override
public int getCount() {
return placedRequests.size();
}
@Override
public long getItemId(int position) {
return placedRequests.indexOf(getItem(position));
}
@Override
public PlacedRequestItemModel getItem(int position) {
return placedRequests.get(position);
}
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
List<PlacedRequestItemModel> filterList = new ArrayList<PlacedRequestItemModel>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ((mStringFilterList.get(i).getRequestStatus().equalsIgnoreCase(constraint.toString())) || constraint.toString().equals("All")) {
PlacedRequestItemModel listTemp = new PlacedRequestItemModel(mStringFilterList.get(i).getRequestName(),
mStringFilterList.get(i).getRequestDate(), mStringFilterList.get(i).getRequestStatus(),
mStringFilterList.get(i).getRequestToken());
filterList.add(listTemp);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
placedRequests = (ArrayList<PlacedRequestItemModel>) results.values;
notifyDataSetChanged();
}
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View listView;
if (convertView == null) {
listView = new View(context);
listView = inflater.inflate( R.layout.placed_requests_item , null);
TextView tvRequestName = (TextView) listView.findViewById(R.id.tvPlacedRequestName);
tvRequestName.setText(placedRequests.get(position).getRequestName());
TextView tvRequestDate= (TextView) listView.findViewById(R.id.tvPlacedRequestDate);
tvRequestDate.setText(placedRequests.get(position).getRequestDate());
TextView tvRequestStatus= (TextView) listView.findViewById(R.id.tvPlacedRequestStatus);
tvRequestStatus.setText(placedRequests.get(position).getRequestStatus());
TextView tvRequestToken = (TextView) listView.findViewById(R.id.tvRequestToken);
tvRequestToken.setText(placedRequests.get(position).getRequestToken());
} else {
listView = (View) convertView;
}
return listView;
}
}
这是Model类:
public class PlacedRequestItemModel
{
String requestName, requestDate, requestStatus, requestToken;
public PlacedRequestItemModel(String requestName, String requestDate, String requestStatus, String requestToken) {
this.requestName = requestName;
this.requestDate = requestDate;
this.requestStatus = requestStatus;
this.requestToken=requestToken;
}
public String getRequestDate() {
return requestDate;
}
public void setRequestDate(String requestDate) {
this.requestDate = requestDate;
}
public String getRequestStatus() {
return requestStatus;
}
public void setRequestStatus(String requestStatus) {
this.requestStatus = requestStatus;
}
public String getRequestName() {
return requestName;
}
public void setRequestName(String requestName) {
this.requestName = requestName;
}
public String getRequestToken() {
return requestToken;
}
public void setRequestToken(String requestToken) {
this.requestToken = requestToken;
}
}
请指导。