我尝试使用带有游标适配器的viewHolder来显示来自SQLITE DataBase的一些数据。
我搜索了一些解决方案,但我不明白如何在这个例子中使用de viewHolder。
有人能帮帮我吗? 这是我的代码:
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);
}
}
}
答案 0 :(得分:3)
每次需要新行时,CursorAdapter都不会调用newView;如果它已经有一个View,它将调用bindView,因此创建的视图实际上是重用的。因此,根据您的要求更新这些方法
@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());
//...................
}