如何使用自动完成文本视图(如默认联系人列表视图)对列表视图进行排序?

时间:2013-11-19 07:21:04

标签: android listview android-listview autocomplete

我对自动填充文本视图有疑问。在这里,我试图根据自动完成文本视图中键入的文本过滤列表视图。我从json解析中获取值。自动完成文本视图在下拉列表中正确显示其所有值。但在列表视图中我看不到它的变化。

以下是listview的适配器代码:

    public class EventListAdapter extends BaseAdapter implements Filterable {

    public static LayoutInflater inflator =  null;

    private  ArrayList<EventsBean> mOriginalvalues = new ArrayList<EventsBean>();

    private ArrayList<EventsBean> mDisplayvalues;

    ImageLoader imageloader;

    public String datenew,datetime,date_text_value,timenew;

    public int date_text,year;

    public String time,month,description;

    public EventListAdapter( ArrayList<EventsBean> mEventarraylist,Activity activity) {
        super();
        //this.context = context;
        this.mOriginalvalues = mEventarraylist;
        inflator =(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageloader=new ImageLoader(activity.getApplicationContext());


    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mOriginalvalues.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View holder;

        holder = inflator.inflate(R.layout.activity_list_items, parent, false);

        TextView txt_name = (TextView)holder.findViewById(R.id.textname);
        TextView txt_owner_name = (TextView)holder.findViewById(R.id.ownername);
        TextView txt_time = (TextView)holder.findViewById(R.id.date);
        TextView txt_date = (TextView)holder.findViewById(R.id.txt_date_value);
        TextView txt_month = (TextView)holder.findViewById(R.id.txt_month_value);
        TextView txt_year = (TextView)holder.findViewById(R.id.txt_year_value);

        ImageView userimg = (ImageView)holder.findViewById(R.id.imageView1);

        txt_name.setText(mOriginalvalues.get(position).getName());
        txt_owner_name.setText(mOriginalvalues.get(position).getOwner_name());
        String url = mOriginalvalues.get(position).getSource();

        date_text_value = mOriginalvalues.get(position).getStart_time();


        parseDateFromString(date_text_value);

        txt_date.setText(String.valueOf(date_text));
        txt_month.setText(month);
        txt_year.setText(String.valueOf(year));

        Log.i("TEST", "Date:" + date_text_value);

        Log.i("TAG", "Country:" + mOriginalvalues.get(position).getCountry());

        imageloader.DisplayImage(url, userimg);

        txt_time.setText(timenew);
        //userimg.getFitsSystemWindows();

        return holder;
    }


    @SuppressLint("SimpleDateFormat")
    public Date parseDateFromString(String aDateString){
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

        Calendar c = Calendar.getInstance();
        Date date= new Date();
        try {

            date= inputFormat.parse(aDateString);

            System.out.println(date);

            SimpleDateFormat day = new SimpleDateFormat("dd-MMM-yyyy");
            SimpleDateFormat time = new SimpleDateFormat("hh:mm a", Locale.getDefault());

            SimpleDateFormat month_date = new SimpleDateFormat("MMM");


            c.setTime(inputFormat.parse(aDateString));

            System.out.println(day.format(date));

            datenew  = day.format(date).toString();

            date_text = c.get(Calendar.DAY_OF_MONTH);
            month = month_date.format(c.getTime());
            year = c.get(Calendar.YEAR);

            System.out.println("Year = " + c.get(Calendar.YEAR));
            System.out.println("Month = " + month);
            System.out.println("Day = " + date_text);

            System.out.println(time.format(date));

            timenew = time.format(date).toString();

        } catch (ParseException e) {

            Log.i("TAG", "DateFormat Pasring Error:" + e.getMessage());
        }

        return date;

    }

    @SuppressLint("DefaultLocale")
    @Override
    public Filter getFilter() {
        // TODO Auto-generated method stub

        Filter filter = new Filter() {

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                // TODO Auto-generated method stub


                 mOriginalvalues = (ArrayList<EventsBean>) results.values; // has the filtered values
                    if (results.count > 0) {
                        notifyDataSetChanged();
                    } else {
                        notifyDataSetInvalidated();
                    }


            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                // TODO Auto-generated method stub

                FilterResults results = new FilterResults();        // Holds the results of a filtering operation in values
                ArrayList<EventsBean> FilteredArrList = new ArrayList<EventsBean>();

                if (mDisplayvalues == null) {
                 mDisplayvalues = new ArrayList<EventsBean>(mOriginalvalues); // saves the original data in mOriginalValues
                 System.out.println("Display Value:" + mDisplayvalues.size());
                }

                /********
                 * 
                 *  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 = mDisplayvalues.size();
                    results.values = mDisplayvalues;
                } else {
                    constraint = constraint.toString().toLowerCase();
                    for (int i = 0; i < mDisplayvalues.size(); i++) {
                        EventsBean data = mDisplayvalues.get(i);
                        System.out.println("Display Value 2:" + mDisplayvalues.size());
                        if (data.getLocation_city().toLowerCase().startsWith(constraint.toString())
                                || data.getLocation_country().toLowerCase().startsWith(constraint.toString())
                                || data.getLocation_state().toLowerCase().startsWith(constraint.toString())) 
                        {
                            FilteredArrList.add(new EventsBean(mDisplayvalues.get(i).getLocation_city(),mDisplayvalues.get(i).getLocation_country(),
                                    mDisplayvalues.get(i).getLocation_state()));
                        }
                    }
                    // set the Filtered result to return
                    results.count = FilteredArrList.size();
                    results.values = FilteredArrList;
                }
                return results;
            }

        };

        return filter;
    }


}

每次尝试在我的适配器的getCount()中的自动完成文本视图中写入一些文本时,我都会收到nullpointer错误。我在自动完成文本视图状态,国家和城市中有3个不同的值存储在arraylist中。 我在网上看过很多例子,但是他们都有自动完成的textview值,只有一个值,但我有三个如何用它们过滤listview?

请帮我解决我的nullpointer错误。

1 个答案:

答案 0 :(得分:1)

我可以通过在getView()中进行更改来解决我的问题。由于错误的getView()内容,我得到了NullPointer。但它现在工作正常。