过滤的ListView自定义(对象)适配器自定义、适配器、对象、ListView

2023-09-11 12:17:35 作者:霸气让你着迷

我想实现一个ListView这是使用自定义的对象适配器的过滤,但我无法找到任何有用的样本。所包含的code是非常简单,所以无糖记住我不能使用常规的ArrayAdapter。 我已经在ListView以上的EditText,当用户在窗口小部件的EditText中输入文本,我想通过写在的EditText文本过滤的ListView。 任何建议将是非常美联社preciated!

下面是活动类的代码片段:

 公共类management_objects延伸活动{

私有静态列表<使用者>在UserList;
私人EfficientAdapter适配器= NULL;
私人的ListView objectListView = NULL;
私人的EditText SearchText = NULL;

私有静态类EfficientAdapter扩展了BaseAdapter实现过滤的{
    私人LayoutInflater mInflater;

    公共EfficientAdapter(上下文的背景下){
        mInflater = LayoutInflater.from(上下文);
    }

    公众诠释getCount将(){
        返回UserList.size();
    }

    公共对象的getItem(INT位置){
        返回的位置;
    }

    众长getItemId(INT位置){
        返回的位置;
    }

    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        ViewHolder持有人;
        如果(convertView == NULL){
            convertView = mInflater.inflate(R.layout.imagelayout_2lines,NULL);
            持有人=新ViewHolder();
            holder.text =(TextView中)convertView.findViewById(R.id.managementObjectText);
            holder.subtext =(TextView中)convertView.findViewById(R.id.managementObjectSubText);
            holder.icon =(ImageView的)convertView.findViewById(R.id.managementObjectIcon);
            convertView.setTag(保持器);
        }
        其他 {
            支架=(ViewHolder)convertView.getTag();
        }

        holder.text.setText(UserList.get(位置).getFirstName());
        holder.subtext.setText(UserList.get(位置).getLastName());
        holder.icon.setImageResource(R.drawable.user);

        返回convertView;
    }

    静态类ViewHolder {
        TextView的文字;
        TextView的潜台词;
        ImageView的图标;
    }

    @覆盖
    公共过滤用getFilter(){
        返回null;
    }
}

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.adobjectlist);
    捆绑额外= getIntent()getExtras()。

    SearchText =(EditText上)findViewById(R.id.SearchBox);
    SearchText.addTextChangedListener(filterTextWatcher);

    objectListView =(ListView控件)findViewById(R.id.ObjectList);
    objectListView.setOnItemClickListener(ITEM_CLICK);
    适配器=新EfficientAdapter(本);
    计算机名= extras.getString(COMPUTER_NAME);

    //从web服务的用户列表
    ShowUsers();
}
 

下面是用户等级:

 公共类用户{
  私人诠释用户ID;
  私人字符串名字;
  私人字符串姓氏;

    公众诠释getUserId(){
        返回用户ID;
    }
    公共无效setUserId(INT用户ID){
        this.UserId =用户ID;
    }
    公共字符串的getFirstName(){
        返回名字;
    }
    公共无效setFirstName(字符串名字){
        this.FirstName =名字;
    }
    公共字符串getLastName(){
        返回姓氏;
    }
    公共无效setLastName(字符串名字){
        this.LastName =姓氏;
    }
}
 
Android入门第六篇之ListView 1

解决方案

您需要做几件事情:

1)在你的活动,注册您的EditText文本变化监听包含用户输入的值:

  

mSearchValue.addTextChangedListener(searchTextWatcher);

2)创建searchTextWatcher,并将它做一些事情:

 私人TextWatcher searchTextWatcher =新TextWatcher(){
    @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
            // 忽略
        }

        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){
            // 忽略
        }

        @覆盖
        公共无效afterTextChanged(编辑S){
            Log.d(Constants.TAG,***搜索值改为:+ s.toString());
            。adapter.getFilter()滤波器(s.toString());
        }
    };
 

3)在自定义的适配器覆盖用getFilter(),并把它过滤结果,并通知列表视图的数据集已经改变了。

  @覆盖
    公共过滤用getFilter(){
        返回新的过滤器(){
            @燮pressWarnings(未登记)
            @覆盖
            保护无效publishResults(CharSequence的约束,FilterResults结果){
                Log.d(Constants.TAG,****出版结果:+约束);
                myData的=(名单< MyDataType>)results.values​​;
                MyCustomAdapter.this.notifyDataSetChanged();
            }

            @覆盖
            保护FilterResults performFiltering(CharSequence的约束){
                Log.d(Constants.TAG,****执行过滤为:+约束);
                名单< MyDataType> filteredResults = getFilteredResults(约束);

                FilterResults结果=新FilterResults();
                results.values​​ = filteredResults;

                返回结果;
            }
        };
    }
 

I'm trying to implement filtering of a ListView which is uses a custom object adapter, but I can't find any useful samples. The included code is very simplified, so no- keep in mind I can't use an regular ArrayAdapter. I have a EditText above the ListView, and when the user enters text in the EditText widget I would like to filter the ListView by the text written in the EditText. Any suggestions would be much appreciated!

Here is the snippet from the activity class:

public class management_objects extends Activity {

private static List<User> UserList;
private EfficientAdapter adapter = null;
private ListView objectListView = null;
private EditText SearchText = null;

private static class EfficientAdapter extends BaseAdapter implements Filterable{
    private LayoutInflater mInflater;   

    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

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

    public Object getItem(int position) {
        return position;
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder; 
        if (convertView == null) { 
            convertView = mInflater.inflate(R.layout.imagelayout_2lines, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.managementObjectText);
            holder.subtext = (TextView) convertView.findViewById(R.id.managementObjectSubText);
            holder.icon = (ImageView) convertView.findViewById(R.id.managementObjectIcon);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(UserList.get(position).getFirstName());
        holder.subtext.setText(UserList.get(position).getLastName());
        holder.icon.setImageResource(R.drawable.user);

        return convertView;
    }

    static class ViewHolder { 
        TextView text;
        TextView subtext;
        ImageView icon;
    }

    @Override
    public Filter getFilter() {
        return null;
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.adobjectlist);
    Bundle extras = getIntent().getExtras();

    SearchText = (EditText) findViewById(R.id.SearchBox);    
    SearchText.addTextChangedListener(filterTextWatcher);

    objectListView = (ListView) findViewById(R.id.ObjectList);
    objectListView.setOnItemClickListener(Item_Click);
    adapter = new EfficientAdapter(this);
    ComputerName = extras.getString("COMPUTER_NAME");

    //Get User list from webservice
    ShowUsers();
}

Here is The User class:

 public class User {
  private int UserId;
  private String FirstName;
  private String LastName;

    public int getUserId() {
        return UserId;
    }
    public void setUserId(int UserId) {
        this.UserId = UserId;
    }
    public String getFirstName() {
        return FirstName;
    }
    public void setFirstName(String FirstName) {
        this.FirstName = FirstName;
    }
    public String getLastName() {
        return LastName;
    }
    public void setLastName(String LastName) {
        this.LastName = LastName;
    }
}

解决方案

You need to do a few things:

1) In your activity, register for a text change listener on your EditText that contains the value the user enters:

mSearchValue.addTextChangedListener(searchTextWatcher);

2) Create your searchTextWatcher and have it do something:

private TextWatcher searchTextWatcher = new TextWatcher() {
    @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // ignore
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // ignore
        }

        @Override
        public void afterTextChanged(Editable s) {
            Log.d(Constants.TAG, "*** Search value changed: " + s.toString());
            adapter.getFilter().filter(s.toString());
        }
    };

3) Override getFilter() in your custom adapter and have it filter the results and notify the listview that the dataset has changed.

    @Override
    public Filter getFilter() {
        return new Filter() {
            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                Log.d(Constants.TAG, "**** PUBLISHING RESULTS for: " + constraint);
                myData = (List<MyDataType>) results.values;
                MyCustomAdapter.this.notifyDataSetChanged();
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                Log.d(Constants.TAG, "**** PERFORM FILTERING for: " + constraint);
                List<MyDataType> filteredResults = getFilteredResults(constraint);

                FilterResults results = new FilterResults();
                results.values = filteredResults;

                return results;
            }
        };
    }