2013-05-25 65 views
1

我對android非常陌生,所以這可能聽起來很愚蠢。我有一個從數據庫填充的列表視圖。我想要搜索它並在用戶鍵入搜索框時在列表中顯示這些項目。找到許多教程和答案,但無法弄清楚如何做到這一點。發佈我的代碼。通過從數據庫中填充的listview搜索

這是主類

public class MainActivity extends Activity { 
public final static String ID_EXTRA="com.example.app.med.medimonics._ID"; 
private databasehelper dbhelper = null; 
private Cursor ourcursor = null; 
private databaseadapter adapter = null; 

@SuppressWarnings("deprecation") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    EditText searchbox = (EditText) findViewById (R.id.editText1); 
    ListView list = (ListView) findViewById(R.id.listView1); 
    dbhelper = new databasehelper(this); 
    dbhelper.createdatabase(); 
    dbhelper.openDataBase(); 
    ourcursor = dbhelper.getCursor(); 
    startManagingCursor (ourcursor); 
    adapter = new databaseadapter (ourcursor); 
    list.setAdapter(adapter); 
    list.setOnItemClickListener(onListClick); 


     }; 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
class databaseadapter extends CursorAdapter{ 
    @SuppressWarnings("deprecation") 
    databaseadapter(Cursor c){ 
     super(MainActivity.this, c); 
    } 

    @Override 
    public void bindView(View row, Context context, Cursor c) { 
     // TODO Auto-generated method stub 
     dataholder holder = (dataholder) row.getTag(); 
     holder.populateFrom(c, dbhelper); 
    } 

    @Override 
    public View newView(Context context, Cursor c, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     LayoutInflater inflater = getLayoutInflater(); 
     View row=inflater.inflate(R.layout.row, parent, false); 
     dataholder holder = new dataholder(row); 
     row.setTag(holder); 
     return (row); 
    } 
} 
    static class dataholder{ 
private TextView name = null; 
dataholder(View row){ 
    name=(TextView)row.findViewById(R.id.textView1); 
} 
void populateFrom(Cursor c, databasehelper r){ 
    name.setText(r.getname(c)); 
} 
    } 
    private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() { 

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    // TODO Auto-generated method stub 
    Intent i = new Intent(MainActivity.this, Clicked.class); 
    i.putExtra(ID_EXTRA, String.valueOf(id)); 
    startActivity(i); 
} 

    }; 

    } 

如果需要

+0

我有一個示例@ http://stackoverflow.com/questions/10816243/search-in-listview-with-edittext/15367403#15367403。如果您可以根據自己的需求修改相同的產品。您可以使用相同的參考,特別是搜索部分和過濾 – Raghunandan

回答

1

使用此回合中搜索的EditText我可以張貼databasehelper類爲好。

searchedittext.addTextChangedListener(new TextWatcher() { 
       public void onTextChanged(CharSequence s, int start, 
         int before, int count) { 

       } 


       public void beforeTextChanged(CharSequence s, int start, 
         int count, int after) { 
       } 

       public void afterTextChanged(Editable s) { 


      }); 

然後在afterTextChange方法做這樣的事情。

val=s.toString(); // s is Editable s 

AL.getFilter().filter(s); 
相關問題