如何使用搜索功能在Android的自定义列表视图 [英] How to use search functionality in custom list view in Android

查看:131
本文介绍了如何使用搜索功能在Android的自定义列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个列表视图,在连续多个项目。我也创建一个以上的搜索框。欲实现列表的特定字段的基础上的搜索功能。我怎样才能做到这一点?任何帮助将AP preciated。

I have created a list view with multiple items in row. I have also created a search box above. I want to implement search functionality on the basis of particular fields of the list. How can I achieve this? Any help will be appreciated.

推荐答案

您必须使用模式,ListView和customadapter与滤波这一点。 我创建了一个演示这一点,假设你有一个型号命名的产品,并且正在显示其在名称,价格都显示在TextView中自定义列表视图的内容。我的意思是在有两个自定义行textviews.and要过滤对列表进行自定义的领域之一row.here我已筛选与名

you have to use model,listview and customadapter with filtering for this. I have created a demo for this,suppose you have a model named Product,and you are displaying its content in a custom listview where name,price are displayed in textview .I mean in a custom row having two textviews.and you want to filter the list by one of the field of custom row.here i have filtered with "name"

截图:

初​​始

筛选

来源$ C ​​$ C

//这是你的模型

public class Product {

public String name;
public Integer price;
public Product(String name, Integer price) {
    super();
    this.name = name;
    this.price = price;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Integer getPrice() {
    return price;
}
public void setPrice(Integer price) {
    this.price = price;
}

}

//这是自定义的适配器和ListView

//this is your activity with custom adapter and listview

public class MainActivity extends Activity{
private LinearLayout llContainer;
private EditText etSearch;
private ListView lvProducts;

private ArrayList<Product> mProductArrayList = new ArrayList<Product>();
private MyAdapter adapter1;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    initialize();



    // Add Text Change Listener to EditText
    etSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // Call back the Adapter with current character to Filter
            adapter1.getFilter().filter(s.toString());
        }

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

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}

private void initialize() {
    etSearch = (EditText) findViewById(R.id.etSearch);
    lvProducts = (ListView)findViewById(R.id.lvOS);
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    mProductArrayList.add(new Product("a", 100));
    mProductArrayList.add(new Product("b", 200));
    mProductArrayList.add(new Product("c", 300));
    mProductArrayList.add(new Product("d", 400));
    mProductArrayList.add(new Product("e", 500));
    mProductArrayList.add(new Product("f", 600));
    mProductArrayList.add(new Product("g", 700));
    mProductArrayList.add(new Product("h", 800));
    mProductArrayList.add(new Product("i", 900));
    mProductArrayList.add(new Product("j", 1000));
    mProductArrayList.add(new Product("k", 1100));
    mProductArrayList.add(new Product("l", 1200));
    mProductArrayList.add(new Product("m", 1000));
    mProductArrayList.add(new Product("n", 1300));
    mProductArrayList.add(new Product("o", 1400));
    mProductArrayList.add(new Product("p", 1500));


    adapter1 = new MyAdapter(MainActivity.this, mProductArrayList);
    lvProducts.setAdapter(adapter1);
}


// Adapter Class            
public class MyAdapter extends BaseAdapter implements Filterable {

private ArrayList<Product> mOriginalValues; // Original Values
private ArrayList<Product> mDisplayedValues;    // Values to be displayed
LayoutInflater inflater;

public MyAdapter(Context context, ArrayList<Product> mProductArrayList) {
    this.mOriginalValues = mProductArrayList;
    this.mDisplayedValues = mProductArrayList;
    inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return mDisplayedValues.size();
}

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

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

private class ViewHolder {
    LinearLayout llContainer;
    TextView tvName,tvPrice;
}

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

    ViewHolder holder = null;

    if (convertView == null) {

        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.row, null);
        holder.llContainer = (LinearLayout)convertView.findViewById(R.id.llContainer);
        holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
        holder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.tvName.setText(mDisplayedValues.get(position).name);
    holder.tvPrice.setText(mDisplayedValues.get(position).price+"");

    holder.llContainer.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            Toast.makeText(MainActivity.this, mDisplayedValues.get(position).name, Toast.LENGTH_SHORT).show();
        }
    });

    return convertView;
}

@Override
public Filter getFilter() {
    Filter filter = new Filter() {

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

            mDisplayedValues = (ArrayList<Product>) results.values; // has the filtered values
            notifyDataSetChanged();  // notifies the data with new filtered values
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();        // Holds the results of a filtering operation in values
            ArrayList<Product> FilteredArrList = new ArrayList<Product>();

            if (mOriginalValues == null) {
                mOriginalValues = new ArrayList<Product>(mDisplayedValues); // saves the original data in mOriginalValues
            }

            /********
             * 
             *  If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
             *  else does the Filtering and returns FilteredArrList(Filtered)  
             *
             ********/
            if (constraint == null || constraint.length() == 0) {

                // set the Original result to return  
                results.count = mOriginalValues.size();
                results.values = mOriginalValues;
            } else {
                constraint = constraint.toString().toLowerCase();
                for (int i = 0; i < mOriginalValues.size(); i++) {
                    String data = mOriginalValues.get(i).name;
                    if (data.toLowerCase().startsWith(constraint.toString())) {
                        FilteredArrList.add(new Product(mOriginalValues.get(i).name,mOriginalValues.get(i).price));
                    }
                }
                // set the Filtered result to return
                results.count = FilteredArrList.size();
                results.values = FilteredArrList;
            }
            return results;
        }
    };
    return filter;
}
}
}

//这是你activity_main.xml

//this is your activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/etSearch"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<ListView 
    android:id="@+id/lvProducts"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    ></ListView>    

</LinearLayout>

//这是你row.xml

//this is your row.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >


    <TextView 
        android:id="@+id/tvName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:layout_weight="1"
        />
    <TextView 
        android:id="@+id/tvPrice"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:layout_weight="1"
        />


</LinearLayout>

这篇关于如何使用搜索功能在Android的自定义列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆