我有一个使用基础适配器的列表视图,这是我初始化它的方式:
if (mBuses.size() < 1) {
// Initialising and loading data into adapter
mBuses = buses;
mBusesAdapter = new BusesArrayAdapter(BusesActivity.this, mBuses);
mBusesView.setAdapter(mBusesAdapter);
} else {
mBuses.addAll(buses);
mBusesAdapter.notifyDataSetChanged();
}
我正在使用监听器来检查用户何时滚动到底部,它从服务器的服务器获取更多数据并附加到现有集而不是使用分页。它每页加载100个项目。
现在我的问题是当我尝试过滤数据时,它总是只过滤前100个项目,但不包括其他项目。
这是我的基础适配器:
public class BusesArrayAdapter extends BaseAdapter implements Filterable {
private Context context;
private int altColour;
private ArrayList<BusModel> buses;
private BusModel bus;
private ValueFilter valueFilter;
private ArrayList<BusModel> filterList;
private static class ViewHolder{
RelativeLayout container;
TextView id;
TextView busType;
TextView registrationNo;
TextView vehicleModel;
TextView driverName;
TextView driverContact;
ImageButton btnEdit;
ImageButton btnRemove;
}
public BusesArrayAdapter(@NonNull Context context, @NonNull ArrayList<BusModel> buses) {
this.context = context;
this.buses = buses;
this.filterList = new ArrayList<>(buses);
}
@Override
public int getCount() {
return buses.size();
}
@Override
public BusModel getItem(int position) {
return buses.get(position);
}
@Override
public long getItemId(int position) {
return getItem(position).getId();
}
public void remove(BusModel bus) {
buses.remove(bus);
}
@NonNull
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
// Get the data item for this position
bus = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
final ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
// If there's no view to re-use, inflate a brand new view for row
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.row_buses, parent, false);
viewHolder.container = (RelativeLayout) convertView.findViewById(R.id.row_buses_ll_container);
viewHolder.id = (TextView) convertView.findViewById(R.id.row_buses_tv_id);
viewHolder.busType = (TextView) convertView.findViewById(R.id.row_buses_tv_bus_type);
viewHolder.registrationNo = (TextView) convertView.findViewById(R.id.row_buses_tv_registration_no);
viewHolder.vehicleModel = (TextView) convertView.findViewById(R.id.row_buses_tv_vehicle_model);
viewHolder.driverName = (TextView) convertView.findViewById(R.id.row_buses_tv_driver_name);
viewHolder.driverContact = (TextView) convertView.findViewById(R.id.row_buses_tv_driver_contact);
viewHolder.btnEdit = (ImageButton) convertView.findViewById(R.id.row_buses_btn_edit);
viewHolder.btnEdit.setTag(position);
viewHolder.btnRemove = (ImageButton) convertView.findViewById(R.id.row_buses_btn_trash);
viewHolder.btnRemove.setTag(position);
// Cache the viewHolder object inside the fresh view
convertView.setTag(viewHolder);
} else {
// View is being recycled, retrieve the viewHolder object from tag
viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data from the data object via the viewHolder object
// into the template view.
if (altColour == 0) {
viewHolder.container.setBackgroundColor(Color.parseColor("#FFFFFF"));
altColour = 1;
} else {
viewHolder.container.setBackgroundColor(Color.parseColor("#EFEFEF"));
altColour = 0;
}
viewHolder.id.setText(String.valueOf(bus.getId()));
viewHolder.busType.setText(bus.getBusType());
viewHolder.registrationNo.setText(bus.getRegistrationNo());
viewHolder.vehicleModel.setText(bus.getVehicleModel());
viewHolder.driverName.setText(bus.getDriverName());
viewHolder.driverContact.setText(bus.getDriverContact());
viewHolder.btnEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = Integer.parseInt(viewHolder.btnRemove.getTag().toString());
BusModel bus = getItem(position);
if (bus != null) {
Intent editBusIntent = new Intent(context, EditBusActivity.class);
editBusIntent.putExtra("bus", bus);
((Activity) context).startActivityForResult(editBusIntent, 200);
}
}
});
viewHolder.btnRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = Integer.parseInt(viewHolder.btnRemove.getTag().toString());
removeBus(position);
}
});
// Return the completed view to render on screen
return convertView;
}
private void removeBus(final int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Confirm");
builder.setMessage("Are you sure you want to delete this item?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
BusesRequest busesRequest = new BusesRequest(context);
busesRequest.remove(getItem(position).getId(), mTrashOnSuccessListener, mTrashOnErrorListener);
remove(getItem(position));
notifyDataSetChanged();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
private Response.Listener<JSONObject> mTrashOnSuccessListener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//
}
};
Response.ErrorListener mTrashOnErrorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Utils.showNetworkResponse(context, error);
}
};
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
//Invoked in a worker thread to filter the data according to the constraint.
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if(constraint != null && constraint.length() > 0){
ArrayList<BusModel> filterList = new ArrayList<BusModel>();
for(int i = 0; i < BusesArrayAdapter.this.filterList.size(); i++)
{
BusModel bus = BusesArrayAdapter.this.filterList.get(i);
if((bus.getRegistrationNo().toLowerCase())
.contains(constraint.toString().toLowerCase())
|| (bus.getDriverName().toLowerCase())
.contains(constraint.toString().toLowerCase()) ) {
filterList.add(new BusModel(
bus.getId(),
bus.getBusType(),
bus.getRegistrationNo(),
bus.getVehicleModel(),
bus.getDriverName(),
bus.getDriverContact()
));
}
}
results.count = filterList.size();
results.values = filterList;
} else{
results.count = filterList.size();
results.values = filterList;
}
return results;
}
//Invoked in the UI thread to publish the filtering results in the user interface.
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
buses = (ArrayList<BusModel>) results.values;
notifyDataSetChanged();
}
}
}
答案 0 :(得分:1)
因为filterList
在创建Adapter实例时只启动了一次。你必须在filterList
中添加新的100个项目。
this.filterList = new ArrayList<>(buses);
在创建像这样的新实例时只存储100条记录。
mBusesAdapter = new BusesArrayAdapter(BusesActivity.this, mBuses);
尝试添加每个已提取的项目filterList
。