我有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;
}
}
我想RecipeName中進行搜索。我在想什麼?
創建自定義的搜索過濾器 – Vijju
檢查我的ListActivity代碼..我已經創建了.. –
,你已經創建了? – Vijju