I am very new to android so this might sound stupid. I have a listview populated from a databse. I want to search through it and display the items in the list as the user types in the searchbox. Found many tutorials and answers but couldn't figure out how to do that. Posting my code.
This is the main class
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);
}
};
}
I can post the databasehelper class as well if needed