0

我实际上是在尝试过滤我的列表视图。它是一个数组列表。当我在编辑文本中键入字符时,我得到一个 Nullpointerexception

public 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;
}

}

已发送项目.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;
}

}

发件箱.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                          
        }
    });
4

0 回答 0