过滤后如何获取列表视图项的原始位置?

时间:2018-09-23 01:46:35

标签: android listview search

这可能会被标记为重复,但是我在这里搜索了与我的问题相关的每篇文章,但都没有解决它,因此我正在撰写自己的文章,看看是否有人可以专门帮助您我。

我的问题是,当我在列表视图中搜索某些内容时,位置被弄乱了。因此,如果我有一个列表视图AA,BB,CC,AB,并且我搜索A,它将按预期返回AA,AB,但是如果我单击AB,它将给我BB,因为AB现在就位1而不是3。如何获取onclick侦听器的数据的原始位置?谢谢。

这是我的自定义列表视图类:

<html>

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.js"></script>
</head>

<body>
  <canvas id="chart"></canvas>
</body>

</html>

我的自定义列表视图适配器:

public class CustomListViewClass {

// Store the id of the  movie poster
private String mEquation;
// Store the name of the movie
public String mName;
public int previousPosition;

// Constructor that is used to create an instance of the CustomListViewClass object
public CustomListViewClass(String mName, String mEquation) {
    this.mEquation = mEquation;
    this.mName = mName;
}

public String getmEquation() {
    return mEquation;
}

public void setmEquation(String mEquation) {
    this.mEquation = mEquation;
}

public String getmName() {
    return mName;
}

public void setmName(String mName) {
    this.mName = mName;
}

} 

还有我的主要片段

public class CustomListviewAdapter extends ArrayAdapter<CustomListViewClass> implements Filterable  {

//Define list items
TextView textView;
TextView formula_two;

//Define list
private Context mContext;
private List<CustomListViewClass> moviesList = new ArrayList<>();
private List<CustomListViewClass> moviesListFiltered = new ArrayList<>();

private int layoutResource;

public CustomListviewAdapter(@NonNull Context context, @LayoutRes ArrayList<CustomListViewClass> list) {
    super(context, 0 , list);
    mContext = context;
    moviesList = list;
    moviesListFiltered = list;

}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    View listItem = convertView;
    if(listItem == null)
        listItem = LayoutInflater.from(mContext).inflate(R.layout.row_layout,parent,false);

    CustomListViewClass currentMovie = moviesListFiltered.get(position);

    textView = (TextView) listItem.findViewById(R.id.textView);
    textView.setText(currentMovie.getmName());
    TextView equation = (TextView) listItem.findViewById(R.id.formula_one);
    equation.setText(currentMovie.getmEquation());

    return listItem;
}

int previousPosition;

public int getPreviousPosition() {
    return previousPosition;
}


public void setPreviousPosition(int previousPosition) {
    this.previousPosition = previousPosition;
}

@Override
public long getItemId(int position) {
    return position;
}

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


@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint == null || constraint.length() == 0) {
                //no constraint given, just return all the data. (no search)
                results.count = moviesList.size();
                results.values = moviesList;
            } else {//do the search
                List<CustomListViewClass> resultsData = new ArrayList<>();
                String searchStr = constraint.toString().toUpperCase();
                for (CustomListViewClass o : moviesListFiltered)
                    if (o.mName.toUpperCase().startsWith(searchStr)) resultsData.add(o);
                results.count = resultsData.size();
                results.values = resultsData;
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            moviesListFiltered = (ArrayList<CustomListViewClass>) results.values;
            notifyDataSetChanged();
        }
    };
}
}

1 个答案:

答案 0 :(得分:0)

尝试下面的一种替换以下两种方法

@NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View listItem = convertView;
        if(listItem == null)
            listItem = LayoutInflater.from(mContext).inflate(R.layout.row_layout,parent,false);

        CustomListViewClass currentMovie = moviesList.get(position);

        textView = (TextView) listItem.findViewById(R.id.textView);
        textView.setText(currentMovie.getmName());
        TextView equation = (TextView) listItem.findViewById(R.id.formula_one);
        equation.setText(currentMovie.getmEquation());

        return listItem;
    }

,您的过滤器应如下所示

@Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                if (constraint == null || constraint.length() == 0) {
                    //no constraint given, just return all the data. (no search)
                    moviesList = moviesListFiltered;
                } else {//do the search
                    List<CustomListViewClass> resultsData = new ArrayList<>();
                    String searchStr = constraint.toString().toUpperCase();
                    for (CustomListViewClass o : moviesListFiltered)
                        if (o.mName.toUpperCase().startsWith(searchStr)) resultsData.add(o);
                    moviesList = resultsData;
                }
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                moviesList = (ArrayList<CustomListViewClass>) results.values;
                notifyDataSetChanged();
            }
        };
    }