For me I use always this method and it work fine :)
- Frist :
-> I declare my variables :
private ListView lv; //My listview that will contain the informations
DBHelper helper = new DBHelper(this); //DBHelper is the class that contain my database on SQLite
private SQLiteDatabase db; //An instance of SQLiteDatabase that will contain my Database
private Cursor cursor; //A cursor for the query
public SimpleCursorAdapter adapter ; //The adapter of my ListView
- Second : -> I create my ViewBinder :
public static ViewBinder viewBinder = new ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
return false;
}
};
- Thirdly : -> I declare two tables (The first table contain names of column that I want to display, and the seconde TextView or ImageView... That will contain that informations) :
static final String[] FROM = { "name", "image" };
static final int[] TO = { R.id.txt_name, R.id.img };
- fourthly : -> I declare my listview on the onCreate() method :
lv = (ListView) findViewById(R.id.list);
- fifthly : -> I create my class ViewHolder that contain the object that will contain the information :
static class ViewHolder {
private TextView txtName;
private ImageView img;
public ViewHolder(View vue) {
txtName = (TextView) vue.findViewById(R.id.txt_name);
img =(ImageView) vue.findViewById(R.id.img);
}
}
- sixthly : -> I create my class of arrayAdapter :
class CustomAdapter extends SimpleCursorAdapter {
Activity context;
Cursor c;
@SuppressWarnings("deprecation")
public CustomAdapter(Activity context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.c = c;
Log.i("custom", "" + c.getCount());
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.informations, null);
//layout.informations is the layout that contain the textview and image view that will contain my informations
ViewHolder wrapper = new ViewHolder(row);
row.setTag(wrapper);
return (row);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder wrapper = (ViewHolder) view.getTag();
int n_name = c.getColumnIndex("name");
//Example how i fill my image from informations of SQLite
if (c.getString(n_name).equals("Suite royale")) {
wrapper.imge.setImageResource(R.drawable.suite_royale);
}
if (c.getString(n_name).equals("Suite familiale")) {
wrapper.img.setImageResource(R.drawable.suite_familiale);
}
wrapper.txtName.setText(c.getString(n_name));
}
}
- Finally : -> In my onResume() method :
db = helper.getReadableDatabase();
cursor = db.query("Name_of_table", null, null, null, null, null,"_id DESC");
//it's like the SQL query : SELECT * FROM Name_of_table
startManagingCursor(cursor);
adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.informations, cursor, FROM, TO);
adapter.setViewBinder(viewBinder);
lv.setAdapter(adapter);
SimpleCursorAdapter chambres = new CustomAdapter(this, R.layout.informations,cursor, FROM, TO);
lv.setAdapter(chambres);
//Just doing this you can see your informations in your ListView and if you want a clickListener on your line of the listView just add the code below :
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
Cursor c = db.query("Name_of_table", null, "_id="+(lv.getCount()-position), null, null, null,null);
//== SELECT * FROM Name_of_table WHERE _id = Id_Selected, so you need an id called (_id) on your table his type is INT AUTO_INCREMANTE for example
c.moveToFirst();
Log.i("position", ""+position);
Log.i("taille","Cursor :"+c.getCount());
String name= c.getString(c.getColumnIndex("name"));
Toast.makeText(getBaseContext(), "Name selected is : " + name, Toast.LENGTH_SHORT).show();
}
}
});
I hope this will help you, good luck :)