Android:ListView制作搜索过滤器

发布于 2024-10-27 15:24:51 字数 231 浏览 2 评论 0原文

有没有办法在使用 BaseAdapter 而不是 ArrayAdapter 创建的自定义列表视图上创建搜索过滤器,因为我只遇到过在自定义列表视图中使用 ArrayAdapter 的方法。以下是我遇到的使用数组制作过滤器适配器

Is there a way to create a search filter over my custom list view which created using BaseAdapter rather than ArrayAdapter, because I have only come across methods that are using ArrayAdapters in there Custom List Views. Follwing is what I have come acrossMaking a filter using Array Adapter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

年少掌心 2024-11-03 15:24:51

如果您使用 List 集合,则扩展 ArrayAdapter - 它是 BaseAdapter 的子类。

覆盖 getView 方法(在您的情况下,只需复制旧的 getView 代码并进行少量修改)。

覆盖集合和集合中对象的 toString:

listView.setTextFilterEnabled(true);

If you're using List collection then extend ArrayAdapter - it's subclass of BaseAdapter.

Override getView method (In your case just copy your old getView code with little modifications).

Override toString of object that is in your collection and set:

listView.setTextFilterEnabled(true);
茶花眉 2024-11-03 15:24:51

以下是使用基本适配器创建的列表视图的示例。
我搜索了很多,但没有得到任何令人满意的解决方案。因此我想把它放出来,以便将来可以帮助其他人。

这是history_container.xml的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:id="@+id/filter_text" />
    <FrameLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:id="@+id/historyContainerLayout" />
</LinearLayout>

这是history_list_view.xml的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:gravity="left|center"
    android:layout_width="wrap_content" android:paddingBottom="5px"
    android:paddingTop="5px" android:paddingLeft="5px">
    <TextView android:text="@+id/historytext" android:id="@+id/historytext"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_marginLeft="10px" android:textColor="#0099CC"/>
</LinearLayout>

这是history_schedule.xml的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView android:id="@+id/scrollItemInfo"
        android:layout_width="fill_parent" android:layout_height="1000dip">
        <LinearLayout android:orientation="vertical"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
                <ListView android:id="@+id/historylist" android:layout_height="1000dip"
                        android:layout_width="fill_parent"/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

抱歉,我不是能够以正确的格式放置代码。似乎有些事情发生了变化,因为不再有标签,这很好。但为了用户方便,使用选项卡也是一个好主意。
也许是我的错,我无法以正确的格式发布。而且似乎我们无法发布 xml 文件内容。所以,想想布局是如何设计的。

活动代码是:-

package com.testfilter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;


public class TestFilterListView extends Activity {
    FrameLayout historyContainer;
    ViewStub viewStub;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history_container);
        historyContainer = (FrameLayout) findViewById(R.id.historyContainerLayout);
        EditText filterEditText = (EditText) findViewById(R.id.filter_text);
        filterEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                historyContainer.removeAllViews();
                final List<String> tempHistoryList = new ArrayList<String>();
                tempHistoryList.addAll(historyList);
                for(String data : historyList) {
                    if(data.indexOf((s.toString())) == -1) {
                        tempHistoryList.remove(data);
                    }
                }
                viewStub = new ViewStub(TestFilterListView.this, R.layout.history_schedule);
                viewStub.setOnInflateListener(new ViewStub.OnInflateListener()
                {
                    public void onInflate(ViewStub stub, View inflated)
                    {

                        setUIElements(inflated, tempHistoryList);
                    }
                });
                historyContainer.addView(viewStub);
                viewStub.inflate();

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });

        setViewStub();
    }


    /********************************************************************************************************/
    private void setViewStub()
    {
        historyList.add("first");
        historyList.add("second");
        historyList.add("third");
        historyList.add("fourth");
        historyList.add("fifth");
        historyList.add("sixth");
        historyList.add("seventh");


        viewStub = new ViewStub(TestFilterListView.this, R.layout.history_schedule);
        viewStub.setOnInflateListener(new ViewStub.OnInflateListener()
        {
            public void onInflate(ViewStub stub, View inflated)
            {

                setUIElements(inflated, historyList);
            }
        });
        historyContainer.addView(viewStub);
        viewStub.inflate();
    }

    /********************************************************************************************************/
    final List<String> historyList = new ArrayList<String>();
    String displayName = "";
    ListView historyListView;
    private void setUIElements(View v, List<String> historyLists)
    {

        if (v != null)
        {
            historyScheduleData.clear();
            //historyList.clear();

            historyScheduleData.addAll(historyLists);
            historyListView = (ListView) findViewById(R.id.historylist);
            historyListView.setAdapter(new BeatListAdapter(this));

            registerForContextMenu(historyListView);


        }
    }

    /********************************************************************************************************/
    private static class BeatListAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

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

        }

        public int getCount() {
            return historyScheduleData.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.history_list_view, null);
                holder = new ViewHolder();
                holder.historyData = (TextView) convertView
                        .findViewById(R.id.historytext);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.historyData.setText(historyScheduleData.get(position));

            return convertView;
        }

        static class ViewHolder {

            TextView historyData;
        }
    }

    private static final List<String> historyScheduleData = new ArrayList<String>();


}

Here is an example for list view created using base adapter.
I searched a lot but didn't get any satisfactorily solution.Hence I thought to put it up so that in future it might help others.

Here is the code of history_container.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:id="@+id/filter_text" />
    <FrameLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:id="@+id/historyContainerLayout" />
</LinearLayout>

Here is the code of history_list_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:gravity="left|center"
    android:layout_width="wrap_content" android:paddingBottom="5px"
    android:paddingTop="5px" android:paddingLeft="5px">
    <TextView android:text="@+id/historytext" android:id="@+id/historytext"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_marginLeft="10px" android:textColor="#0099CC"/>
</LinearLayout>

Here is the code of history_schedule.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView android:id="@+id/scrollItemInfo"
        android:layout_width="fill_parent" android:layout_height="1000dip">
        <LinearLayout android:orientation="vertical"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
                <ListView android:id="@+id/historylist" android:layout_height="1000dip"
                        android:layout_width="fill_parent"/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

Sorry, I am not being able to put the code in correct format.Seems something has changed as there is no tag anymore which was nice.But for user convenience this is also a good idea to have tab.
Maybe it is my fault that I'm not being able to post in correct format.And seems like we cannot post xml file content..So, think how the layout would have designed.

The activity code is :-

package com.testfilter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;


public class TestFilterListView extends Activity {
    FrameLayout historyContainer;
    ViewStub viewStub;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history_container);
        historyContainer = (FrameLayout) findViewById(R.id.historyContainerLayout);
        EditText filterEditText = (EditText) findViewById(R.id.filter_text);
        filterEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                historyContainer.removeAllViews();
                final List<String> tempHistoryList = new ArrayList<String>();
                tempHistoryList.addAll(historyList);
                for(String data : historyList) {
                    if(data.indexOf((s.toString())) == -1) {
                        tempHistoryList.remove(data);
                    }
                }
                viewStub = new ViewStub(TestFilterListView.this, R.layout.history_schedule);
                viewStub.setOnInflateListener(new ViewStub.OnInflateListener()
                {
                    public void onInflate(ViewStub stub, View inflated)
                    {

                        setUIElements(inflated, tempHistoryList);
                    }
                });
                historyContainer.addView(viewStub);
                viewStub.inflate();

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });

        setViewStub();
    }


    /********************************************************************************************************/
    private void setViewStub()
    {
        historyList.add("first");
        historyList.add("second");
        historyList.add("third");
        historyList.add("fourth");
        historyList.add("fifth");
        historyList.add("sixth");
        historyList.add("seventh");


        viewStub = new ViewStub(TestFilterListView.this, R.layout.history_schedule);
        viewStub.setOnInflateListener(new ViewStub.OnInflateListener()
        {
            public void onInflate(ViewStub stub, View inflated)
            {

                setUIElements(inflated, historyList);
            }
        });
        historyContainer.addView(viewStub);
        viewStub.inflate();
    }

    /********************************************************************************************************/
    final List<String> historyList = new ArrayList<String>();
    String displayName = "";
    ListView historyListView;
    private void setUIElements(View v, List<String> historyLists)
    {

        if (v != null)
        {
            historyScheduleData.clear();
            //historyList.clear();

            historyScheduleData.addAll(historyLists);
            historyListView = (ListView) findViewById(R.id.historylist);
            historyListView.setAdapter(new BeatListAdapter(this));

            registerForContextMenu(historyListView);


        }
    }

    /********************************************************************************************************/
    private static class BeatListAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

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

        }

        public int getCount() {
            return historyScheduleData.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.history_list_view, null);
                holder = new ViewHolder();
                holder.historyData = (TextView) convertView
                        .findViewById(R.id.historytext);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.historyData.setText(historyScheduleData.get(position));

            return convertView;
        }

        static class ViewHolder {

            TextView historyData;
        }
    }

    private static final List<String> historyScheduleData = new ArrayList<String>();


}
情痴 2024-11-03 15:24:51

ArrayAdapter 是 BaseAdapter 的子类。

ArrayAdapter 的源代码位于此处。看看它..

这应该可以解决您的问题。

ArrayAdapter is a child class of BaseAdapter.

Source code for ArrayAdapter is here. Take a look at it..

This should solve your problem.

最舍不得你 2024-11-03 15:24:51

这是一个例子。编辑并使用您需要的内容。

public class CustomListAdapter extends BaseAdapter {
private ArrayList<String> countryStringList;
private ArrayList<Integer> countryImageList;
private LayoutInflater mInFlater;
// private Bitmap mIcon1;
// private Bitmap mIcon2;
private int layoutID;
private Context context;
// private LayoutInflater mInflater;
public CustomListAdapter(Context context, int textViewResourceId,
        ArrayList<String> stringObjects, ArrayList<Integer> imgObjects) {
    super();
    this.context = context;
    countryStringList = stringObjects;
    countryImageList = imgObjects;
    layoutID = textViewResourceId;
    mInFlater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    // return countryStringList.size();
    return countryStringList.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final ViewHolder holder;
    if (convertView == null) {

        convertView = mInFlater.inflate(layoutID, null);
        holder = new ViewHolder();
        holder.textCountryName = (TextView) convertView
                .findViewById(R.id.txtSelectedCountryName);
        holder.icon = (ImageView) convertView
                .findViewById(R.id.imgSelectedCountryImage);
        holder.textCountryPosition = (TextView) convertView
                .findViewById(R.id.txtSelectedCountryPosition);
        // holder.checkBoxListView=(CheckBox)convertView.findViewById(R.id.CheckBoxListView);
        holder.relativeLayoutList = (RelativeLayout) convertView
                .findViewById(R.id.relativeLayoutListItem);
        holder.checkBoxListView = (CheckBox) convertView
                .findViewById(R.id.checkBoxSelectedCountry);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
        // holder=(ViewHolder)convertView.findViewById(R.id.TextViewIcon1)
    }
    holder.checkBoxListView.setTag(position);
    holder.textCountryName.setText(countryStringList.get(position));
    holder.textCountryPosition.setText(position + 1 + "/"
            + countryStringList.size());
    holder.icon.setImageResource((countryImageList.get(position)));
    if (ListViewController.checkStatusList.get(position).equalsIgnoreCase("present")) {
        holder.checkBoxListView.setVisibility(View.VISIBLE);
    } else {
        holder.checkBoxListView.setVisibility(View.GONE);
    }
    /*
     * holder.checkBoxListView.setOnCheckedChangeListener(new
     * OnCheckedChangeListener(){
     * 
     * @Override public void onCheckedChanged(CompoundButton buttonView,
     * boolean isChecked) { // TODO Auto-generated method stub
     * 
     * ListActivityImplementation listActivityImplementation = new
     * ListActivityImplementation(); //show("Checkbox");
     * ListActivityImplementation.countryStringList.remove(position);
     * ListActivityImplementation.countryImageList.remove(position);
     * ListActivityImplementation.lv.invalidateViews();
     * 
     * //ListActivityImplementation.show("Checkbox"); }
     * 
     * }) ;
     */
    holder.textCountryName.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            holder.textCountryPosition.setText(position + 1 + "/"
                    + countryStringList.size() + "clicked");
        }

    });
    holder.checkBoxListView
            .setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    MathHelper.showToast(context, buttonView.getTag()
                            + " clicked");
                }
            });
    return convertView;
}
}
class ViewHolder {
TextView textCountryName;
ImageView icon;
TextView textCountryPosition;
RelativeLayout relativeLayoutList;
CheckBox checkBoxListView;

}

Here is an example . Edit and use what you need.

public class CustomListAdapter extends BaseAdapter {
private ArrayList<String> countryStringList;
private ArrayList<Integer> countryImageList;
private LayoutInflater mInFlater;
// private Bitmap mIcon1;
// private Bitmap mIcon2;
private int layoutID;
private Context context;
// private LayoutInflater mInflater;
public CustomListAdapter(Context context, int textViewResourceId,
        ArrayList<String> stringObjects, ArrayList<Integer> imgObjects) {
    super();
    this.context = context;
    countryStringList = stringObjects;
    countryImageList = imgObjects;
    layoutID = textViewResourceId;
    mInFlater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    // return countryStringList.size();
    return countryStringList.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final ViewHolder holder;
    if (convertView == null) {

        convertView = mInFlater.inflate(layoutID, null);
        holder = new ViewHolder();
        holder.textCountryName = (TextView) convertView
                .findViewById(R.id.txtSelectedCountryName);
        holder.icon = (ImageView) convertView
                .findViewById(R.id.imgSelectedCountryImage);
        holder.textCountryPosition = (TextView) convertView
                .findViewById(R.id.txtSelectedCountryPosition);
        // holder.checkBoxListView=(CheckBox)convertView.findViewById(R.id.CheckBoxListView);
        holder.relativeLayoutList = (RelativeLayout) convertView
                .findViewById(R.id.relativeLayoutListItem);
        holder.checkBoxListView = (CheckBox) convertView
                .findViewById(R.id.checkBoxSelectedCountry);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
        // holder=(ViewHolder)convertView.findViewById(R.id.TextViewIcon1)
    }
    holder.checkBoxListView.setTag(position);
    holder.textCountryName.setText(countryStringList.get(position));
    holder.textCountryPosition.setText(position + 1 + "/"
            + countryStringList.size());
    holder.icon.setImageResource((countryImageList.get(position)));
    if (ListViewController.checkStatusList.get(position).equalsIgnoreCase("present")) {
        holder.checkBoxListView.setVisibility(View.VISIBLE);
    } else {
        holder.checkBoxListView.setVisibility(View.GONE);
    }
    /*
     * holder.checkBoxListView.setOnCheckedChangeListener(new
     * OnCheckedChangeListener(){
     * 
     * @Override public void onCheckedChanged(CompoundButton buttonView,
     * boolean isChecked) { // TODO Auto-generated method stub
     * 
     * ListActivityImplementation listActivityImplementation = new
     * ListActivityImplementation(); //show("Checkbox");
     * ListActivityImplementation.countryStringList.remove(position);
     * ListActivityImplementation.countryImageList.remove(position);
     * ListActivityImplementation.lv.invalidateViews();
     * 
     * //ListActivityImplementation.show("Checkbox"); }
     * 
     * }) ;
     */
    holder.textCountryName.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            holder.textCountryPosition.setText(position + 1 + "/"
                    + countryStringList.size() + "clicked");
        }

    });
    holder.checkBoxListView
            .setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    MathHelper.showToast(context, buttonView.getTag()
                            + " clicked");
                }
            });
    return convertView;
}
}
class ViewHolder {
TextView textCountryName;
ImageView icon;
TextView textCountryPosition;
RelativeLayout relativeLayoutList;
CheckBox checkBoxListView;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文