如何使用自定义过滤器从自定义Listview中搜索

时间:2015-08-15 08:45:21

标签: android json listview search

我正在创建一个应用程序,我正在从JSON填充自定义列表视图数据,这完全没有问题。 我想使用Edittext在listview中实现搜索功能 我遵循这个tutorial

我完成了所有步骤但是当我在Edittext中写一些内容时,列表中没有任何内容显示,甚至在删除listview之后也不会再次填充。

我不想每次都从JSON获取数据。

请帮帮我。

这是我的代码

自定义适配器类代码

public class CategoryAdapter extends BaseAdapter {
private ArrayList<CategoryData> CategoryList = new ArrayList<CategoryData>();
private LayoutInflater inflater = null;
private ArrayList<CategoryData> mlist;
private Context context;

public CategoryAdapter(Context c, ArrayList<CategoryData> mlist) {
    this.context = c;
    this.CategoryList = mlist;
    this.mlist = mlist;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.mlist.addAll(mlist);
}

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

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

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.custom_item_category,
                parent, false);
        holder = new ViewHolder();
        holder.tv_best = (LetterSpacing) convertView
                .findViewById(R.id.tv_best);
        holder.tv_title = (LetterSpacing) convertView
                .findViewById(R.id.tv_title);
        holder.tv_votes = (TextView) convertView
                .findViewById(R.id.tv_votes);
        holder.tv_dish = (TextView) convertView.findViewById(R.id.tv_dish);
        holder.tv_restaurant = (TextView) convertView
                .findViewById(R.id.tv_restaurant);
        holder.img_logo = (ImageView) convertView
                .findViewById(R.id.img_logo);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    CategoryData categoryData = mlist.get(position);
    holder.tv_best.setSpacing(-6f);
    holder.tv_best.setText("Best");
    holder.tv_title.setSpacing(-6f);
    holder.tv_title.setText(categoryData.bestName);
    holder.tv_votes.setText(categoryData.bestVotes);
    holder.tv_dish.setText(categoryData.bestDishes);
    holder.tv_restaurant.setText(categoryData.bestRestaurant);
    // Picasso.with(context).load(categoryData.logo).into(holder.img_logo);
    holder.mCity = categoryData.City;
    String imageUrl = categoryData.bestRetaurantLogo;
    Picasso.with(context).load(imageUrl).into(holder.img_logo);
    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, ItemRankActivity.class);
            intent.putExtra("title", holder.tv_title.getText().toString());
            intent.putExtra("category", holder.tv_dish.getText().toString());
            intent.putExtra("city", holder.mCity);
            context.startActivity(intent);

        }
    });
    return convertView;
}

static class ViewHolder {
    LetterSpacing tv_best;
    LetterSpacing tv_title;
    TextView tv_votes;
    TextView tv_dish;
    TextView tv_restaurant;
    ImageView img_logo;
    String mCity;
}

// Filter Class
public void filter(String charText) {

    CategoryList.clear();
    if (charText.length() == 0) {
        CategoryList.addAll(mlist);
    } else {
        for (CategoryData Wd : mlist) {
            if (Wd.bestName.equals(charText)) {
                CategoryList.add(Wd);
            }
        }
    }
    notifyDataSetChanged();

}}

我在MainActivity中的代码用于文本更改

et_search.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            String text = et_search.getText().toString();
            adapter.filter(text);
            adapter.notifyDataSetChanged();
        }

        @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

        }
    });

请帮我解决这个问题。过去2天我遇到了这个问题。

0 个答案:

没有答案