过滤后,在Listview OnItemClick中获取错误的位置

时间:2017-05-26 09:28:32

标签: android listview filter position android-adapter

这是我对ListView的适配器类

需要帮助,我在过滤列表视图后获得原始位置,而不是过滤结果的位置。代码如下,请仔细阅读,如有任何疑问请询问。

// This is my Adapter Class for ListView



        public class mAdapter extends BaseAdapter implements Filterable {

        ArrayList<MlaData> dats;
        public ArrayList<MlaData> filterList;
        enter code here
        CustomFilter filter;

        Context c;
        ImageLoader imageLoader;


        public mAdapter(Context ctx,ArrayList<MlaData> dats){
            this.c=ctx;
            this.dats=dats;
            this.filterList = dats;

        }


        @Override
        public int getCount() {
            return dats.size();
        }

        @Override
        public Object getItem(int position) {
            return dats.get(position);
        }

        @Override
        public long getItemId(int position) {
            int itemID;

            // orig will be null only if we haven't filtered yet:
            if (filterList == null)
            {
                itemID = position;
            }
            else
            {
                itemID = dats.indexOf(dats.get(position));
            }
            return itemID;
        }
    @Override

     public Filter getFilter() {
            if(filter==null)
            {
                filter = new CustomFilter();
            }
            return filter;
        }



        class CustomFilter extends  Filter{

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                FilterResults results = new FilterResults();
                if(constraint!=null && constraint.length()>0)
                {
                    constraint = constraint.toString().toUpperCase();
                    ArrayList<MlaData> filters = new ArrayList<MlaData>();


                    //Filtering

                    for(int i=0;i<filterList.size();i++)
                    {
                        if(filterList.get(i).getName().toUpperCase().contains(constraint)){
                            MlaData MlaDat = new MlaData(filterList.get(i).getName(),filterList.get(i).getImageUrl(),filterList.get(i).getArea(),filterList.get(i).getId(),filterList.get(i).getEmail());
                            filters.add(MlaDat);
                        }
                        results.count=filters.size();
                        results.values=filters;
                    }
                }else {
                    results.count=filterList.size();
                    results.values=filterList;
                }
                return results;
            }@Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                dats=(ArrayList<MlaData>)results.values;
                notifyDataSetChanged();

            }
        }

MainActivity.java (仅限ListView OnClick功能)

        private ArrayList<MlaData> MlaDats = new ArrayList<MlaData>();
       private String MlaNameString, MlaImageString, MlaIdString, MlaEmailString, MlaAreaString;
         private   GridView listView;
          private  mAdapter adapt;
          private  SearchView sv;


         listView = (GridView) findViewById(R.id.mldata);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        int posi = (int) adapt.getItemId(position);
                        Intent intent = new Intent(Rajasthan_Mla.this, MainActivity.class);
                       // int pos = (int) adapt.getItemId(position);
                        String MLAID = MlaDats.get(posi).getId();
                        String MLANAME = MlaDats.get(posi).getName();
                        String MLAEMAIL = MlaDats.get(posi).getEmail();
                        String MLAIMAGE = MlaDats.get(posi).getImageUrl();
                        String MLAAREA=MlaDats.get(posi).getArea();

                        intent.putExtra("MLA_ID", MLAID);
                        intent.put

Extra("MLA_NAME", MLANAME);
                    intent.putExtra("MLA_EMAIL", MLAEMAIL);
                    intent.putExtra("MLA_IMAGE", MLAIMAGE);
                    intent.putExtra("MLA_AREA", MLAAREA);

                    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("Mukesh", 0);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("MLAID", MLAID);
                    editor.putString("MLANAME",MLANAME);
                    editor.commit();

                    startActivity(intent);
                }
            });

DataModel类

package com.wdm.mukku.wdm;


public class MlaData {
    private String name;
    private String imageUrl;
    private String area;
    private String email;
    private String id;

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return imageUrl;
    }

    public void setImage(String image) {
        this.imageUrl = image;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public MlaData(String name,String image,String area,String id,String email) {
        this.name=name;
        this.imageUrl=image;
        this.area=area;
        this.id=id;
        this.email=email;

    }
    public MlaData() {


    }
}

3 个答案:

答案 0 :(得分:0)

首先改变这个

public mAdapter(Context ctx,ArrayList<MlaData> dats){
   this.c=ctx;
   this.dats=dats;
   // will create a new array instead of referencing to the same object
   this.filterList = new ArrayList<MlaData>(dats); 
}

接下来不再修改dats并仅使用已过滤的列表,因此您将保留原始列表,并在过滤其他内容时使用该列表。

所以在你的过滤器中使用它

        // temp array to store filtered data
        ArrayList<MlaData> filters = new ArrayList<MlaData>();
        // use original list to loop through and check for matches
        for (int i = 0; i < dats.size(); i++) {
            if (dats.get(i).getName().toUpperCase().contains(constraint)) {   
                MlaData MlaDat = new MlaData(dats.get(i).getName(), dats.get(i).getImageUrl(), dats.get(i).getArea(), dats.get(i).getId(), dats.get(i).getEmail());
                filters.add(MlaDat);
            }
        }
        results.count = filters.size();
        results.values = filters;
    } 
    else {
        results.count = dats.size();
        results.values = dats;
    }
    return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
      // assign the filtered data to filterList
      filterList = (ArrayList<MlaData>)results.values;
      notifyDataSetChanged();
}

最后更改您的方法以使用已过滤的列表

@Override
public int getCount() {
    return filterList.size();
}

@Override
public Object getItem(int position) {
    return filterList.get(position);
}

@Override
public long getItemId(int position) {
    return ((MlaData)filterList.get(position)).getId();
}

清除过滤器后,您应该将filterList重新设置为原始数据

filterList = new ArrayList<MlaData>(dats);

答案 1 :(得分:0)

1。更新适配器的方法getItemId(),如下所示:

    @Override
    public long getItemId(int position) {

        return position;
    }

2。更新ListView onItemClick()方法,如下所示:

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            MlaData item = MlaDats.get(position);

            String MLAID = item.getId();
            String MLANAME = item.getName();
            String MLAEMAIL = item.getEmail();
            String MLAIMAGE = item.getImageUrl();
            String MLAAREA = item.getArea();

            Intent intent = new Intent(Rajasthan_Mla.this, MainActivity.class);
            intent.putExtra("MLA_ID", MLAID);
            intent.putExtra("MLA_NAME", MLANAME);
            intent.putExtra("MLA_EMAIL", MLAEMAIL);
            intent.putExtra("MLA_IMAGE", MLAIMAGE);
            intent.putExtra("MLA_AREA", MLAAREA);

            SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("Mukesh", 0);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("MLAID", MLAID);
            editor.putString("MLANAME",MLANAME);
            editor.commit();

            startActivity(intent);
        }
    });

答案 2 :(得分:0)

您可以通过

获取过滤列表视图的位置
((ListView) parent).getAdapter().getItem(position)

实施例         private ArrayList MlaDats = new ArrayList();

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
            MlaData mModel = ((ListView) parent).getAdapter().getItem(position);
            String id = mModel.getId();
            Log.d(TAG,"id = "+id);
        }
    });