搜索从数据库填充的listview

时间:2013-05-25 13:59:59

标签: android database listview search filter

我对android很新,所以这可能听起来很愚蠢。我有一个从数据库填充的列表视图。我想搜索它并在用户在搜索框中键入时显示列表中的项目。找到了许多教程和答案,但无法弄清楚如何做到这一点。发布我的代码。

这是主要的课程

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

    };

    }

如果需要,我也可以发布databasehelper类

1 个答案:

答案 0 :(得分:1)

在你的搜索编辑文本中使用它。

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