我在定制搜索上花了太多时间。
我想用textwatcher进行搜索。但是,我混淆了如何在我的代码中实现它。任何人都可以告诉我,我将如何在我的代码中实现它或如何在此代码中使用addTextChangedListener(){}。
editTxt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
对于completition,我正在显示已创建的类:
Daftar_Masakan.java (FoodList)
public class Daftar_Masakan3 extends Activity{
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> DaftarMasakanAndro = new
ArrayList<HashMap<String,String>>();
private static String link_dft_masakan =
"http://10.0.3.2/masakan/masakan_andro/daftar_masakan_andro.php";
public static final String TAG_ID = "id_masakan";
public static final String TAG_NAMA = "nama_";
public static final String TAG_GAMBAR = "gambar";
JSONArray masakan = null;
ListView list;
LazyAdapter adapter;
EditText inputSearch;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.pencarian);
inputSearch = (EditText)findViewById(R.id.inputSearch);
DaftarMasakanAndro = new ArrayList<HashMap<String,String>>();
new AmbilDataJSON().execute();
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@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
}
});
list = (ListView) findViewById(R.id.list);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map = DaftarMasakanAndro.get(position);
//memulai intent baru
Intent in = new Intent(getApplicationContext(), Detail_Masakan.class);
in.putExtra(TAG_ID, map.get(TAG_ID));
in.putExtra(TAG_GAMBAR, map.get(TAG_GAMBAR));
startActivity(in);
}
});
}
public void SetListViewAdapater(ArrayList<HashMap<String, String>>daftar_masakan) {
adapter = new LazyAdapter(this, daftar_masakan);
list.setAdapter(adapter);
}
class AmbilDataJSON extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Daftar_Masakan3.this);
pDialog.setMessage("Mohon Tunggu Men...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json= jParser.AmbilJsonUrl(link_dft_masakan, "GET", params);
//untuk mengecek data di background proses
//Log.d("hasil json", "Hasil json :"+json.toString());
try {
masakan = json.getJSONArray("daftar_masakan");
for (int i = 0; i < masakan.length(); i++) {
JSONObject ar = masakan.getJSONObject(i);
String id_msk = ar.getString(TAG_ID);
String nama_msk = ar.getString(TAG_NAMA);
String link_gbr = ar.getString(TAG_GAMBAR);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id_msk);
map.put(TAG_NAMA, nama_msk);
map.put(TAG_GAMBAR, link_gbr);
DaftarMasakanAndro.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
SetListViewAdapater(DaftarMasakanAndro);
}
});
}
}
}
这适用于我的代码中的Lazy Adapter:
LazyAdapter.java
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.tampil_baris_daftar, null);
TextView id_masakan = (TextView) vi.findViewById(R.id.kode);
TextView nm_masakan = (TextView) vi.findViewById(R.id.nama_);
ImageView thumb_image = (ImageView) vi.findViewById(R.id.gambarthumb);
HashMap<String, String> daftar_masakan_andro = new HashMap<String, String>();
daftar_masakan_andro = data.get(position);
id_masakan.setText(daftar_masakan_andro.get(Daftar_Masakan3.TAG_ID));
nm_masakan.setText(daftar_masakan_andro.get(Daftar_Masakan3.TAG_NAMA));
imageLoader.DisplayImage(daftar_masakan_andro.get(Daftar_Masakan3.TAG_GAMBAR),thumb_image);
return vi;
}
我的代码应该有什么变化,我的搜索将会执行。 Plz我是Android的初学者。在此先感谢
答案 0 :(得分:0)
修改LazyAdapter,使其实现Filterable接口并覆盖getFilter()方法。
现在通过扩展Filter类来创建一个类,并提供两个覆盖方法的实现。
This代码示例将说明如何以编程方式实现它。