我实际上是想过滤我的listview.It是一个arraylist。当我在edittext中输入char时,我在
中得到一个Nullpointerexceptionpublic int getCount() {
return listData.size();
}
我不知道getFilter方法中发生了什么,我只是从教程中复制它。任何人都可以帮忙解决这个问题。
CustomListAdapter.java
public class CustomListAdapter extends BaseAdapter implements Filterable{
private ArrayList<SentItems> listData;
List<String> arrayList;
List<String> mOriginalValues; // Original Values
private LayoutInflater layoutInflater;
public CustomListAdapter(Context context, ArrayList<SentItems> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.listview_item_row_for_sentbox, null);
holder = new ViewHolder();
holder.CustomerNameView = (TextView) convertView.findViewById(R.id.name);
holder.MessageView = (TextView) convertView.findViewById(R.id.message);
holder.SentDateView = (TextView) convertView.findViewById(R.id.date);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.CustomerNameView.setText(listData.get(position).getCustomerName());
holder.MessageView.setText(listData.get(position).getMessage());
holder.SentDateView.setText(listData.get(position).getDate());
return convertView;
}
static class ViewHolder {
TextView CustomerNameView;
TextView MessageView;
TextView SentDateView;
}
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
listData = (ArrayList<SentItems>) results.values; // has the filtered values
notifyDataSetChanged(); // notifies the data with new filtered values
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values
List<String> FilteredArrList = new ArrayList<String>();
if (mOriginalValues == null) {
mOriginalValues = new ArrayList<String>(arrayList); // saves the original data in mOriginalValues
}
/********
*
* If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
* else does the Filtering and returns FilteredArrList(Filtered)
*
********/
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = mOriginalValues.size();
results.values = mOriginalValues;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < mOriginalValues.size(); i++) {
String data = mOriginalValues.get(i);
if (data.toLowerCase().startsWith(constraint.toString())) {
FilteredArrList.add(data);
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
}
SentItems.java
公共类SentItems {
private String customerName;
private String Message;
private String date;
private String phoneNo;
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getMessage() {
return Message;
}
public void setMessage(String Message) {
this.Message = Message;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getPhone() {
return phoneNo;
}
public void setPhone(String phone) {
this.phoneNo = phone;
}
@Override
public String toString() {
return date;
}
}
Sent_box.java
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Sent_box.this.adapter.getFilter().filter(cs);;
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});