Searchbox für Custom Listview

  • Antworten:2
Laire
  • Forum-Beiträge: 71

13.06.2012, 05:03:45 via Website

Hallo,

ich brauche für meine Custom Listview eine searchbox also sowas wie hier:



Die Beispiele die ich im Netzt gefunden haben, waren meist für normale Fieldlists oder sehr speziell auf ein Beispiel beschränkt, so das ich die nich adaptieren konnte.

Die activity sieht so aus:

[code]package de.bodprod.rettinfo;

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

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class AntidotList extends Activity{
ArrayList<HashMap<String, String>> antidotList;

String[] sqliteIds;

public static String TAG_ID = "id";
public static String TAG_TOX = "tox";
public static String TAG_ANTIDOT = "antidot";

ListView lv;
int textlength=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.antidotlist_layout);

antidotList = new ArrayList<HashMap<String, String>>();
lv = (ListView) findViewById(R.id.antidotlistlayout);

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

}
});
new loadStoreAntidots().execute();
}

class loadStoreAntidots extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... args) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
List<AntidotsClass> antidots = db.getAllAntidots();
sqliteIds = new String[antidots.size()];

for (int i = 0; i < antidots.size(); i++) {

AntidotsClass s = antidots.get(i);

// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put(TAG_ID, s.getId().toString());
map.put(TAG_TOX, s.getTox());
map.put(TAG_ANTIDOT, s.getAntidot());

// adding HashList to ArrayList
antidotList.add(map);

// add sqlite id to array
// used when deleting a website from sqlite
sqliteIds[i] = s.getId().toString();
}
ListAdapter adapter = new SimpleAdapter(
AntidotList.this,
antidotList, R.layout.antidotlistitem_layout,
new String[] { TAG_ID, TAG_TOX, TAG_ANTIDOT },
new int[] { R.id.sqlite_id, R.id.tox_layout, R.id.antidot_layout }
);
lv.setAdapter(adapter);
}
});
return null;
}
}
}[/code]

und die Layout Datei so:

1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 style="@style/mainView"
6 android:orientation="vertical" >
7 <EditText android:id="@+id/EditText01"
8 android:layout_height="wrap_content"
9 android:layout_width="fill_parent"
10 android:hint="Suchen">
11 </EditText>
12 <ListView
13 android:id="@+id/antidotlistlayout"
14 style="@style/mainView"
15 android:layout_width="fill_parent"
16 android:layout_height="wrap_content" >
17 </ListView>
18</LinearLayout>

Wäre für jede Hilfe dankbar.

— geändert am 13.06.2012, 05:05:38

Antworten
Laire
  • Forum-Beiträge: 71

13.06.2012, 05:06:58 via Website

Habe den Beitrag zweimal editiert, aber er will den erst Code nicht als code anzeigen....

Antworten
Stefan S.
  • Forum-Beiträge: 560

13.06.2012, 07:50:58 via Website

Du must bei deinem Adapter ein FilterQueryProvider registrieren. In meinem Beispiel habe ich eine Liste von Adressen welche ich filtern möchte.
Durch das Eingeben von Text verändert sich der Inhalt des Cursors.
1aa = new MyFillialAdapter(getApplicationContext(), R.layout.pizza_main_item, crFiliale, fields, new int[]{R.id.txtFilialName,R.id.txtFilialStrasse});
2
3 aa.setFilterQueryProvider(new FilterQueryProvider() {
4
5 @Override
6 public Cursor runQuery(CharSequence constraint) {
7 //uri, projection, and sortOrder might be the same as previous
8 //but you might want a new selection, based on your filter content (constraint)
9 Cursor cur = GetFilialenBySearch(constraint);
10 crFiliale = cur;
11 crFiliale.requery();
12 return cur; //now your adapter will have the new filtered content
13 }
14 });
15 private Cursor GetFilialenBySearch(CharSequence s)
16 {
17 try
18 {
19 Cursor c;
20 String selection = PizzaDB.FilialName + " Like ?";
21 String like = "%" + (String)s + "%";
22 String[] args = new String[]{like};
23 c = db.query(PizzaDB.PizzaFilialeTable,
24 new String[]{PizzaDB.ID, PizzaDB.FilialID, PizzaDB.FilialName, PizzaDB.FilialStrasse, PizzaDB.FilialOrt, PizzaDB.FilialeVon, PizzaDB.FilialeBis,PizzaDB.FilialeVon2, PizzaDB.FilialeBis2, PizzaDB.FilialeMinPreis, PizzaDB.FilialeZusatz, PizzaDB.FilialeLand, PizzaDB.FilialeKatID, PizzaDB.FilialeNurAbholen, PizzaDB.FilialeRuhetag, PizzaDB.FilialeTelefon, PizzaDB.FilialeNurTelefon},
25 selection,args,null,null,PizzaDB.FilialName);
26
27 startManagingCursor(c);
28 return c;
29 }
30 catch(Exception e)
31 {
32 return null;
33 }
34 }

Antworten