2014-01-24 64 views
1

我有ListView,我使用BaseAdapter插入值,但是我意識到要搜索我們必須使用ArrayAdapter。我試圖做到這一點,但它無法搜索。Android - 使用BaseAdapter搜索ListView內容

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; 
    } 
    } 

enter image description here

我想RecipeName中進行搜索。我在想什麼?

+0

創建自定義的搜索過濾器 – Vijju

+0

檢查我的ListActivity代碼..我已經創建了.. –

+0

,你已經創建了? – Vijju

回答

1

終於找到了這個解決方案。

無需更改適配器類中的任何內容。在Java類的EditText'sTextChangeListener

這裏只是改變的是代碼:

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(); 
      }   
    }); 
+0

工作....我可以做同樣的多個數組列表? –

0

在listactivity創建這樣的過濾器:

public static List<YourDataObject> filter(String string, 
     Iterable<YourDataObject> iterable, boolean byName) { 
    if (iterable == null) 
     return new LinkedList<YourDataObject>(); 
    else { 
     List<YourDataObject> collected = new LinkedList<YourDataObject>(); 
     Iterator<YourDataObject> iterator = iterable.iterator(); 
     if (iterator == null) 
      return collected; 
     while (iterator.hasNext()) { 
      YourDataObject item = iterator.next(); 

      if (item.name.toLowerCase().contains(string.toLowerCase())) 
        collected.add(item); 

     } 
     return collected; 
    } 

說它從ontextchangelistener:

List<YourDataObject> list = filter(s.toString(), 
          YourDataObject, true); 
        YourDataObject.addAll(list); 

,然後再次調用setAdapter。

+0

????? ListView對象..? –

+0

YourDataObject是您的id_values – Vijju

+0

但是我想按照食譜名搜尋...... ???? –

0

更換自定義過濾器與這一個

public static List<HashMap<String, String>> filter(String string, 
     Iterable<HashMap<String, String>> iterable, boolean byName) { 
    if (iterable == null) 
     return new LinkedList<HashMap<String, String>>(); 
    else { 
     List<HashMap<String, String>> collected = new LinkedList<HashMap<String, String>>(); 
     Iterator<HashMap<String, String>> iterator = iterable.iterator(); 
     if (iterator == null) 
      return collected; 
     while (iterator.hasNext()) { 
      HashMap<String, String> item = iterator.next(); 

      if (item.get("recipename").toLowerCase().contains(string.toLowerCase())) 
       collected.add(item); 

     } 
     return collected; 
    } 
} 
+0

我需要將它粘貼到Activity類或適配器類中? –

+0

在Activity類中...並從您的搜索框中調用它的文本更改事件偵聽器 – Vijju

+0

Dude ....在te上寫什麼? xtafterchanged()你已經定義爲過濾器函數中的迭代器..我有arraylist和hashmap只有 –