如何将 ViewHolder 与我的 CursorAdapter 一起使用?
How to Use a ViewHolder with my CursorAdapter?
我正在尝试将 viewHolder 与我的游标适配器一起使用,以显示来自 SQLITE 数据库的一些数据。
我搜索了一些解决方案,但我不明白如何在此示例中使用 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);
}
}
}
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());
//...................
}
我正在尝试将 viewHolder 与我的游标适配器一起使用,以显示来自 SQLITE 数据库的一些数据。
我搜索了一些解决方案,但我不明白如何在此示例中使用 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);
}
}
}
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());
//...................
}