列表视图中的搜索过滤器在项目单击上返回错误的值

时间:2019-05-14 10:12:24

标签: android listview android-fragments android-listview android-filterable

我有一个带有自定义过滤器的自定义ListView,但是在Edit_text字段中输入数据后,我改变了ListView,并且在单击修改后的列表后,我得到了错误的信息。我该如何解决这个问题?

应用程序启动后,单击时我会显示正确的信息,但是在编辑EditText并因此单击List后,单击时我的信息完全不正确。

DictionaryFragment

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_dictionary, container, false);

    listView = view.findViewById(R.id.dictionaryList);
    editText = view.findViewById(R.id.edit_search_dict);
    editText.addTextChangedListener(textWatcher);
    mylist = new ArrayList<>();
    SingleRow singleRow;

    for (int i = 0;i<mTitle.length;i++){
        singleRow = new SingleRow(mTitle[i],mDescriprion[i],images[i],mFind[i]);
        mylist.add(singleRow);
    }

    myAdapter = new MyAdapter(getActivity(),mylist);
    listView.setAdapter(myAdapter);

    return view;
}

    @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            mCallback.setMyVariableX(getListOfWords()[position]);
            FragmentManager manager = getFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
          transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            transaction.replace(R.id.fragment_container, new DetailFragment()).addToBackStack(null);
            transaction.commit();
        }
    });

MyAdapter

public class MyAdapter extends BaseAdapter implements Filterable {

DataCommunication mCallback;

Context c;
ArrayList<SingleRow> originalArray,tempArray;
CustomFilter cs;

public MyAdapter(Context c, ArrayList<SingleRow> originalArray){
    this.c = c;
    this.originalArray = originalArray;
    this.tempArray = originalArray;
}

@Override
public Object getItem(int i) {
    return originalArray.get(i);
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    LayoutInflater inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.row,null);

    ImageView images = row.findViewById(R.id.imageViewKanji);
    TextView myTitle = row.findViewById(R.id.textView1);
    TextView myDescription = row.findViewById(R.id.textView2);

    myTitle.setText(originalArray.get(i).getTitle());
    myDescription.setText(originalArray.get(i).getDescription());
    images.setImageResource(originalArray.get(i).getImage());



    return row;
}

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

@Override
public long getItemId(int i) {
    //return originalArray.indexOf(getItemId(i));
    return i;
}

@Override
public Filter getFilter() {
    if (cs == null) {
        cs = new CustomFilter();
    }
    return cs;
}

class CustomFilter extends Filter{

    @Override
    protected FilterResults performFiltering(CharSequence charSequence) {
        FilterResults results = new FilterResults();

        if (charSequence != null && charSequence.length() > 0) {

            charSequence = charSequence.toString().toUpperCase();
            ArrayList<SingleRow> filters = new ArrayList<>();

        for (int i = 0; i < tempArray.size(); i++) {
            if (tempArray.get(i).getTitle().toUpperCase().contains(charSequence)) {
                SingleRow singleRow = new SingleRow(tempArray.get(i).getTitle()
                        , tempArray.get(i).getDescription()
                        , tempArray.get(i).getImage()
                        ,tempArray.get(i).getKanji());
                filters.add(singleRow);
            }
        }
        for (int i = 0; i < tempArray.size(); i++) {
                if (tempArray.get(i).getDescription().toUpperCase().contains(charSequence)) {
                    SingleRow singleRow = new SingleRow(tempArray.get(i).getTitle()
                            , tempArray.get(i).getDescription()
                            , tempArray.get(i).getImage()
                           ,tempArray.get(i).getKanji());
                    filters.add(singleRow);
                }
            }
            for (int i = 0; i < tempArray.size(); i++) {
                if (tempArray.get(i).getKanji().toUpperCase().contains(charSequence)) {
                    SingleRow singleRow = new SingleRow(tempArray.get(i).getTitle()
                            , tempArray.get(i).getDescription()
                            , tempArray.get(i).getImage()
                            ,tempArray.get(i).getKanji());
                    filters.add(singleRow);

                }
            }

        results.count = filters.size();
        results.values = filters;
    }
        else
        {
            results.count = tempArray.size();
            results.values = tempArray;
        }

        return results;
    }

    @Override
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
        originalArray = (ArrayList<SingleRow>)filterResults.values;
        notifyDataSetChanged();
    }
}}

SingleRow

public class SingleRow {

String title;
String description;
int image;
String kanji;

public String getKanji() {
    return kanji;
}

public void setKanji(String kanji) {
    this.kanji = kanji;
}

public SingleRow(String title, String description, int image, String kanji){
    this.title = title;
    this.description = description;
    this.image = image;
    this.kanji = kanji;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public int getImage() {
    return image;
}

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

请帮助我。我试图解决它,但一切都以偏离或空值结尾。建议显示代码中需要更改或指定的内容。

GitHub:https://github.com/figyshkin/KanjiLearn

1 个答案:

答案 0 :(得分:0)

您需要获取过滤器数据列表,此后,如果单击任何项​​目,则将获得正确的数据。

请尝试使用以下代码:

创建另一个全局数组列表:

ArrayList<SingleRow> finalfilters;

在CustomFilter类内部进行初始化会扩展Filter {

class CustomFilter extends Filter{
finalfilters = new ArrayList<>();

//After search set final search data into finalfilters list:

finalfilters=filters;

}

//Create one method who returns final search list:
public void getFilterList()
{
return finalfilter;
}

将getFilterList()调用到您的活动中,在主活动类中设置到另一个列表中,并管理列表项的单击事件,并通过索引获取过滤后的数据。