Android的 - 使用BaseAdapter搜索内容的ListView [英] Android - Search ListView content using BaseAdapter

查看:148
本文介绍了Android的 - 使用BaseAdapter搜索内容的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView 和我一直在使用插入的值 BaseAdapter ,但我意识到,搜索,我们有使用 ArrayAdapter 。我试图这样做,但它无法搜索。

I am having a ListView and I have inserted values using BaseAdapter, but I realized that to search we have to use ArrayAdapter. I tried doing that but it fails to search.

JAVA文件

package com.myrecipeapp.myrecipes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.myrecipeapp.R;
import com.myrecipeapp.database.DatabaseHandler;
import com.myrecipeapp.database.MyRecipesDatabaseModel;

import android.app.Activity;
import android.app.ListActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;


public class MyRecipes extends ListActivity
{

ListView list;  
byte[] b;

TextView back, hidden_text;
EditText search;

String r_name, r_tag;
Bitmap bmp;
int id;

Button delete;

int count;

DatabaseHandler db;
List<MyRecipesDatabaseModel> model;

MyRecipesDatabaseModel m;

MyRecipes_Adapter adapter;

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

    setContentView(R.layout.myrecipes);
    //list = (ListView) findViewById(R.id.myrecipes_listView1);
    db = new DatabaseHandler(this);

    count = db.getRecipesCount();

    search = (EditText) findViewById(R.id.myrecipes_edittext_search);
    hidden_text = (TextView) findViewById(R.id.myrecipes_norecipe_textView1);
    delete = (Button) findViewById(R.id.myrecipes_button_delete);

    if(count==0)
    {
        hidden_text.setVisibility(View.VISIBLE);
        delete.setVisibility(View.INVISIBLE);
        search.setVisibility(View.INVISIBLE);

    }

    model = db.getAllRecipes();

    ArrayList<Map<String, String>> values = new ArrayList<Map<String, String>>();
    ArrayList<Map<String, Integer>> id_values = new ArrayList<Map<String, Integer>>();
    ArrayList<Map<String, Bitmap>> values_img = new ArrayList<Map<String, Bitmap>>();


    for (MyRecipesDatabaseModel cn : model)
    {
        b = cn.getImage();
        r_name = cn.getName();
        r_tag = cn.getTag();
        id = cn.getID();

        bmp = BitmapFactory.decodeByteArray(b, 0, b.length);

        HashMap<String, String> recipe = new HashMap<String, String>();
        recipe.put("RNAME", r_name);
        recipe.put("RTAG", r_tag);

        HashMap<String, Bitmap> image = new HashMap<String, Bitmap>();
        image.put("RIMAGE", bmp);

        HashMap<String, Integer> id_value = new HashMap<String, Integer>();
        id_value.put("RID", id);

        values.add(recipe);
        values_img.add(image);
        id_values.add(id_value);

    }



    setListAdapter(new MyRecipes_Adapter(this, values, values_img, id_values));

    //adapter = new MyRecipes_Adapter(this, values, values_img, id_values);         
//      list.setLAdapter(adapter);


    search.addTextChangedListener(new TextWatcher()
    {

        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
        {
            // TODO Auto-generated method stub

            MyRecipes.this.adapter.getFilter().filter(arg0);


        }

        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
        {
            // TODO Auto-generated method stub

        }

        public void afterTextChanged(Editable arg0)
        {
            // TODO Auto-generated method stub

        }
    });

   }
 }

适配器文件

package com.myrecipeapp.myrecipes;

import java.util.ArrayList;
import java.util.Map;

import com.myrecipeapp.R;
import com.myrecipeapp.database.DatabaseHandler;
import com.myrecipeapp.database.MyRecipesDatabaseModel;
import com.myrecipeapp.webclip.Clip_from_web_edit;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

 public class MyRecipes_Adapter extends ArrayAdapter<ArrayList<Map<String, String>>>
 {

MyRecipes context;   
ArrayList<Map<String, String>> values;
ArrayList<Map<String, Bitmap>> values_img;
ArrayList<Map<String, Integer>> id_values;

byte[] b;

Boolean delete = false;
int delete_id;
RelativeLayout rl;

DatabaseHandler db;
MyRecipesDatabaseModel model;

public MyRecipes_Adapter(MyRecipes context,
        ArrayList<Map<String, String>> values, ArrayList<Map<String, Bitmap>> values_img,
        ArrayList<Map<String, Integer>> id_values) {
    super(context, R.layout.myrecipes_adapter);
    // TODO Auto-generated constructor stub

    this.context = context;
    this.values = values;
    this.values_img = values_img;   
    this.id_values = id_values;
}


@Override
public View getView(int arg0, View arg1, ViewGroup arg2)
{
    // TODO Auto-generated method stub

    View view;      

    if(arg1==null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getLayoutInflater();
        view = inflater.inflate(R.layout.myrecipes_adapter, arg2, false);

        ImageView iv = (ImageView) view.findViewById(R.id.myrecipes_adapter_imageView1);
        TextView tv1 = (TextView) view.findViewById(R.id.myrecipes_adapter_textView1);
        TextView tv2 = (TextView) view.findViewById(R.id.myrecipes_adapter_textView2);
        final TextView tv3 = (TextView) view.findViewById(R.id.myrecipes_adapter_textView3);
        rl = (RelativeLayout) view.findViewById(R.id.myrecipes_adapter_reltivelayout);

        iv.setImageBitmap(values_img.get(arg0).get("RIMAGE"));          
        tv1.setText(values.get(arg0).get("RNAME"));
        tv2.setText("#"+values.get(arg0).get("RTAG"));
        tv3.setText(id_values.get(arg0).get("RID").toString());

    }
    else
    {
        view = arg1;
    }

    return view;
    }
   }

我想通过 RecipeName中以搜索。我很想念?

I want to search by RecipeName. What I am missing??

推荐答案

终于找到了这个解决方案。

Finally found the solution on this.

无需更改适配器类东西。就在Java类更改 EditText上的 TextChangeListener

No need to change anything in adapter class. Just change in Java class for EditText's TextChangeListener

下面是code:

search.addTextChangedListener(new TextWatcher()
    {

        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
          //get the text in the EditText
           searchString=search.getText().toString();
           int textLength=searchString.length();
           ArrayList<Map<String, String>> searchResults = new ArrayList<Map<String, String>>();
           ArrayList<Map<String, Bitmap>> searchResultsImages = new ArrayList<Map<String, Bitmap>>();
           ArrayList<Map<String, Integer>> searchResultsId = new ArrayList<Map<String, Integer>>();

              for(int i=0;i<values.size();i++)
              {
                  String rname = values.get(i).get("RNAME").toString();
                  System.out.println("recipe name "+rname);
                  if(textLength<=rname.length())
                  {
                        //compare the String in EditText with Names in the ArrayList
                        if(searchString.equalsIgnoreCase(rname.substring(0,textLength)))
                        {
                                searchResults.add(values.get(i));
                                searchResultsImages.add(values_img.get(i));
                                searchResultsId.add(id_values.get(i));

                                System.out.println("the array list is "+values.get(i));
                                adapter.notifyDataSetChanged();
                                list.invalidate(); 
                                adapter=new MyRecipes_Adapter(MyRecipes.this, searchResults,  searchResultsImages, searchResultsId);
                                list.setAdapter(adapter);

                        }
                 }
              }
              if(searchResults.isEmpty())
              {
                if(count_items==0)
                {
                     //do nothing
                }
                else
                {
                      ll.setVisibility(View.INVISIBLE);
                      Toast toast= Toast.makeText(getApplicationContext(), 
                      "No Items Matched", Toast.LENGTH_SHORT);  
                      toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0);
                      toast.show();
                }
              }       
        }

          public void beforeTextChanged(CharSequence s, int start, int count, int after)
          {
                 System.out.println("before changed");
                 ll.setVisibility(View.VISIBLE);
                 list.invalidate();
                 adapter.notifyDataSetChanged();
          }

          public void afterTextChanged(Editable s)
          {
                 System.out.println("after changed");                    
                 list.invalidate();
                 adapter.notifyDataSetChanged();
          }          
    });

这篇关于Android的 - 使用BaseAdapter搜索内容的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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