I'm trying to use a viewHolder with my cursor adapter to show some data from SQLITE DataBase.
I search some solutions but I don't understand how to use de viewHolder with this example.
Can anyone help me please ? this is my code:
public class MainActivity extends AppCompatActivity {
FilmDbAdapter mDbHelper;
Cursor FilmsCursor;
String[] arrayTitulo,arrayAno,arrayDirector;
Integer [] arrayPoster;
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list);//
fillData();
}
public class CursorDemo extends CursorAdapter {
public CursorDemo(Context context, Cursor c) {
super(context, c , false);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView title = (TextView) view.findViewById(R.id.txtViewTitulo);
TextView dir = (TextView) view.findViewById(R.id.txtViewDirector);
TextView anio = (TextView) view.findViewById(R.id.txtViewAno);
ImageView img = (ImageView) view.findViewById(R.id.imgPoster);
title.setText(cursor.getString(cursor.getColumnIndex("film")));
dir.setText(cursor.getString(cursor.getColumnIndex("director")));
anio.setText(cursor.getString(cursor.getColumnIndex("ano")));
Context c = getApplicationContext();
String nom = (cursor.getString(cursor.getColumnIndex("img")));
int idimg = c.getResources().getIdentifier(nom,"mipmap" ,c.getPackageName());
img.setImageResource(idimg);
}
@Override
public void changeCursor(Cursor cursor) {
// TODO Auto-generated method stub
super.changeCursor(cursor);
}
@Override
public View newView(Context context , Cursor cursor, ViewGroup viewGroup) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.row, viewGroup ,false);
return view;
}
}
private void fillData (){
mDbHelper = new FilmDbAdapter(this);
mDbHelper.open();
FilmsCursor = mDbHelper.fetchAll("hero");
final CursorDemo cursorDemo = new CursorDemo(MainActivity.this, FilmsCursor);
ListView lstViewPelis = (ListView) findViewById(R.id.lstViewPelis);
lstViewPelis.setAdapter(cursorDemo);
}
public static class viewHolder {
TextView Ano,Director;
viewHolder(View row){
this.Ano = (TextView) row.findViewById(R.id.txtViewAno);
this.Director = (TextView) row.findViewById(R.id.txtViewDirector);
}
}
}
CursorAdapter won't call the newView each time it needs a new row; if it already has a View, it will call the bindView, so the created view is actually reused. So that update these methods as per your requirement
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.row, viewGroup ,false);
viewHolder holder = new viewHolder(view);
view.setTag(holder);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
viewHolder holder = (viewHolder)view.getTag();
holder.title.setText(cursor.getString(cursor.getColumnIndex("film")));
//......follow all
Context c = getApplicationContext();
String nom = (cursor.getString(cursor.getColumnIndex("img")));
int idimg = c.getResources().getIdentifier(nom,"mipmap" ,c.getPackageName());
//...................
}