1

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

  • I have a sample @ http://stackoverflow.com/questions/10816243/search-in-listview-with-edittext/15367403#15367403. If you can modify the same according to your needs good for you. You can use the same for reference especially the search part and filtering – Raghunandan May 25 '13 at 14:19

1 Answers1

1

Use this in ur search edittext.

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


            });

then in afterTextChange method do like this.

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

 AL.getFilter().filter(s);
Senthil
  • 1,244
  • 10
  • 25