0

i would really need some help with the SearchView and my listview with custom list item. I think that my activity class is coded ok, but I really don't know what to do in my Adapter..

I will put the code of the Activity class if needed.. This is my Adapter for now:

public class List_message extends BaseAdapter implements Filterable {

    private Context context;
    private List<String> sender;
    private List<String> type;

    private LayoutInflater inflater;


    public List_message(Context context,List<String> sender,List<String> type ) { 

        inflater = LayoutInflater.from( context );
        this.context = context;
        this.sender = sender;
        this.type = type;

    }

    public int getCount() {                        
        return sender.size();
    }

    public Object getItem(int position) {     
        return sender.get(position);
    }

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

    public View getView(final int position, View convertView, ViewGroup parent) { 

        String sender_tekst = sender.get(position);
        String type_tekst = type.get(position);

        View v = null;
        if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate( R.layout.vrstica_private_message, parent, false);

        TextView posiljatelj = (TextView)v.findViewById( R.id.message_sender);
        posiljatelj.setText( sender_tekst );

        TextView type = (TextView)v.findViewById( R.id.message_writer);
        type.setText( type_tekst );

        ImageButton button = (ImageButton)v.findViewById( R.id.message_delete);
            button.setOnClickListener(
                        new OnClickListener() {
                            public void onClick(View v) {                                       
                            }
                        });
        return v;
    }
    public Filter getFilter() {
        return null;
    }
}
DJack
  • 631
  • 1
  • 8
  • 34
  • Just re-set the filtred list of `List sender` and do a notifyDataSetChanged on your adapter. This way, no need to change your adapter. Do not forget to backup the complete list of senders and the filtred list of senders – Jivy Dec 05 '14 at 14:11
  • Ok, how can I re-set the filtred list of List sender? I should backup only the list of the senders and not the filtered one, because the filtered one will change anytime the search view is in use right? – DJack Dec 05 '14 at 14:19
  • I the end I found the answer here: http://stackoverflow.com/questions/23422072/searchview-in-listview-having-a-custom-adapter – DJack Dec 08 '14 at 19:55

2 Answers2

0

I do not like filterd adapter, but if you want use it, try this link : List Filter Custom Adapter dont give result

Personally, I prefer re-set list managed by the adapter. In your adapter, add a setter method:

public void setSender(List<String> sender) {
  this.sender = sender;
}

Then in your activity, you can do :

List<String> allSenders = getSenders();
List<String> filtredSenders = filterSender(allSenders);
YourBaseAdapter yourAdapter = (YourBaseAdapter) mListView.getAdapter();
yourAdapter.setSender(filtredSenders);
yourAdapter.notifyDataSetChanged();

With this, your adapter will refresh with only filtred items.

Community
  • 1
  • 1
Jivy
  • 1,213
  • 1
  • 10
  • 10
0

You have to create an Instance of Filter and return it from the getFilter() method in the adapter.

public class List_message extends BaseAdapter implements Filterable {

private Context context;
private List<String> sender;
private List<String> allSender;
private List<String> type;

private LayoutInflater inflater;
private Filter filter;


public List_message(Context context,List<String> senders,List<String> type ) { 

    inflater = LayoutInflater.from( context );
    this.context = context;
    this.sender = senders;
    this.allSender = new ArrayList<String>(sender);
    this.type = type;
    filter = new Filter() {

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            sender = (ArrayList<String>) results.values;
            notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {


            FilterResults results = new FilterResults();


            if (constraint == null) {
                results.count = allSender.size();
                results.values = allSender;
                return results;
            }

            List<String> filteredList = new ArrayList<String>();

            List<String> startsWithList = new ArrayList<String>();
            List<String> containsList = new ArrayList<String>();

            // perform your search here using the searchConstraint String.


            constraint = constraint.toString().toLowerCase();
            for (int i = 0; i < allSender.size(); i++) {
                String senderString = allSender.get(i);



                if (senderString.toLowerCase().startsWith(constraint.toString())) {
                    startsWithList.add(senderString);
                } else if (senderString.toLowerCase().contains(constraint.toString())) {
                    containsList.add(senderString);
                }

            }


            filteredList.addAll(startsWithList);
            filteredList.addAll(containsList);

            results.count = filteredList.size();
            results.values = filteredList;

            return results;
        }
    };

}

public int getCount() {                        
    return sender.size();
}

public Object getItem(int position) {     
    return sender.get(position);
}

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

public View getView(final int position, View convertView, ViewGroup parent) { 

    String sender_tekst = sender.get(position);
    String type_tekst = type.get(position);

    View v = null;
    if( convertView != null )
        v = convertView;
    else
        v = inflater.inflate( R.layout.vrstica_private_message, parent, false);

    TextView posiljatelj = (TextView)v.findViewById( R.id.message_sender);
    posiljatelj.setText( sender_tekst );

    TextView type = (TextView)v.findViewById( R.id.message_writer);
    type.setText( type_tekst );

    ImageButton button = (ImageButton)v.findViewById( R.id.message_delete);
        button.setOnClickListener(
                    new OnClickListener() {
                        public void onClick(View v) {                                       
                        }
                    });
    return v;
}
public Filter getFilter() {
    return filter;
}
}
Shadab Mirza
  • 361
  • 1
  • 8
  • I am getting only errors with this code..I don't know if is something wrong with my imports or what.. I tried with copying line by line and also at the end errors.. :/ – DJack Dec 07 '14 at 11:52
  • If you feel the errors are due to imports, then try auto imports. Also i have assumed that u had implemented a TextWatcher on your edit text. – Shadab Mirza Dec 07 '14 at 16:40