来自json getfilter的listview自定义

时间:2014-10-31 11:05:21

标签: android listview

在我的应用程序中,我创建了一个自定义列表视图,我想实现一个过滤器,以便可以根据EditText中输入的文本过滤列表。我使用BaseAdapter作为单独的类。

这是我的适配器:

公共类MyAdapter扩展了BaseAdapter {

// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
public HashMap<String, String> resultp;

public AllProductAdapter(Context context,
        ArrayList<HashMap<String, String>> arraylist) {
    this.context = context;
    data = arraylist;
    imageLoader = new ImageLoader(context);

}

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

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

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

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

    // Declare Variables
    TextView namaBarang;
    ImageView image;
    TextView harga;
    ImageButton cart;

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View itemView = inflater.inflate(R.layout.listview_viewitem, parent,
            false);
    // Get the position from the results
    resultp = new HashMap<String, String>();
    resultp = data.get(position);

    // Locate the TextViews in listview_item.xml
    namaBarang = (TextView) itemView.findViewById(R.id.judul);
    cart = (ImageButton) itemView.findViewById(R.id.cart);
    harga = (TextView) itemView.findViewById(R.id.posterFile);
    image = (ImageView) itemView.findViewById(R.id.poster);

    // Capture position and set results to the TextViews
    namaBarang.setText(resultp.get(AllProductAgent.TITLE));

    harga.setText(resultp.get(AllProductAgent.HARGA),
            TextView.BufferType.SPANNABLE);

    Spannable spannable = (Spannable) harga.getText();
    spannable.setSpan(new StrikethroughSpan(), 0,
            resultp.get(AllProductAgent.HARGA).length(),
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    // Capture position and set results to the ImageView
    // Passes flag images URL into ImageLoader.class to download and cache
    // images
    imageLoader
            .DisplayImageList(resultp.get(AllProductAgent.GAMBAR), image);

    // Link to Register Screen
    itemView.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

        }
    });

    // Add ArrayList
    cart.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            // Get the position from the results
            HashMap<String, String> resultp = new HashMap<String, String>();
            resultp = data.get(position);

            // show toast if item was been added to cart
            Toast.makeText(
                    context,
                    resultp.get(AllProductAgent.TITLE)
                            + " has been added to Cart", Toast.LENGTH_LONG)
                    .show();

            HashMap<String, String> map = new HashMap<String, String>();
            map.put("nama", resultp.get(AllProductAgent.TITLE));
            map.put("harga", resultp.get(AllProductAgent.HARGA));
            map.put("link_gambar", resultp.get(AllProductAgent.GAMBAR));
            map.put("website", resultp.get(AllProductAgent.WEBSITE));
            BaseActivity.cartList.add(map);

        }
    });
    itemView.setBackgroundColor(position % 2 == 0 ? Color
            .parseColor("#c7eaf8") : Color.parseColor("#FFFFFF"));

    return itemView;
}

我如何过滤我的应用?我尝试了这么多代码而仍然无法获得它。

感谢

2 个答案:

答案 0 :(得分:1)

在Searchedittext

中设置TextChangedListener
Searchtxt.addTextChangedListener(new TextWatcher() {

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

                ListAdapterview.getFilter().filter(s.toString());
            }

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

            }

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

然后在Adapter Class中实现“Filterable”

然后添加此代码

@SuppressLint("DefaultLocale")
private class ItemFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        String filterString = constraint.toString().toLowerCase();

        FilterResults results = new FilterResults();

        final ArrayList<HashMap<String, String>> list = data;

        int count = list.size();
        final ArrayList<HashMap<String, String>> filtereddata = new ArrayList<HashMap<String, String>>();

        String filterableString;

        for (int i = 0; i < count; i++) {
            filterableString = list.get(i).get("ur item name");
            if (filterableString.toString().toLowerCase().contains(filterString)) {
                filtereddata.add(list.get(i));
            }
        }

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

        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        if (results != null) {
            if (results.count > 0) {
                data = new ArrayList<HashMap<String,String>>((ArrayList<HashMap<String, String>>) results.values) ;
            } 

        }
        notifyDataSetChanged();
    }

}

答案 1 :(得分:0)

假设您有一个id =“field1”

的EditText
Field1 = (EditText)findViewById(R.id.field1);
Field1.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {}

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

   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
         /* WORK HERE TO FILTER */
   }
});

现在在onTextChanged方法中,您可以以您可以知道的最佳方式过滤数据(数据必须是适配器使用的ArrayList实例)(您可以使用自定义函数或实现过滤器)然后刷新用于刷新和过滤列表的适配器。

 public void onTextChanged(CharSequence s, int start, int before, int count) {
     /* WORK HERE TO FILTER */
     doFilter(data);
     MyAdapterInstance.notifyDataSetChanged();
 }