简体   繁体   中英

How to sort the listview using autocomplete textview like the default contacts listview?

I Have a doubt in my autocomplete textview. Here i am trying to filter the listview based on the text that is typed in the autocomplete textview. I am getting the values from json parsing. The auto complete textview shows all its values correctly in the drop down list. But in the listview i see no changes in it.

Below is my adapter code for the 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;
    }


}

I am getting a nullpointer error everytime i try to write some text in my autocomplete textview in my getCount() of my adapter. I have 3 different values in my autocomplete textview state,country and city which are stored in the arraylist. I have seen lot of examples on net but they all have autocomplete textview values having only one value but i have three how do i filter the listview with them??

Pleae help me in solving up my nullpointer error.

I could solve my problem by making the changes in the getView(). I was getting NullPointer due to wrong getView() contents. but it works fine now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM