用于列表视图的Android Searchview - >如何修改适配器

时间:2014-12-05 13:57:39

标签: android listview adapter searchview

我真的需要一些关于SearchView和我的listview以及自定义列表项的帮助。我认为我的活动课程编码正常,但我真的不知道我的适配器做了什么..

如果需要,我会将Activity类的代码放入.. 这是我现在的适配器:

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

2 个答案:

答案 0 :(得分:0)

我不喜欢filterd适配器,但如果您想使用它,请尝试以下链接:List Filter Custom Adapter dont give result

就个人而言,我更喜欢由适配器管理的重新设置列表。在适配器中,添加setter方法:

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

然后在您的活动中,您可以:

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

这样,您的适配器将只刷新过滤项目。

答案 1 :(得分:0)

您必须创建一个Filter实例并从适配器中的getFilter()方法返回它。

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