Search code examples
androidlistviewandroid-fragmentsandroid-listviewandroid-filterable

Search filter in listview return wrong value on item click


I have a custom ListView with a Custom Filter, but after entering data in the Edit_text field, I have the ListView changed and after clicking on the modified list, I get the wrong information. How do I solve this problem?

With the application launching myself, I have the correct information displayed when I click, but after I edit EditText and therefore List I have completely incorrect information when I click.

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;
}}

Please help me with this. I tried to solve it, but everything ended with a departure or an empty value. It is advisable to show what needs to be changed or specified in the code.

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


Solution

  • You need to get the filter data list and after that if you click on any item you get correct data.

    Please try to use below code:

    Create another global Array List :

    ArrayList<SingleRow> finalfilters;
    

    And initialise inside class CustomFilter extends 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;
    }
    

    Call getFilterList() into your activity , set into another list in main activity class and manage click events of list item and get the filtered data through index.