使用 SimpleCursorAdapter.ViewBinder 显示数据库中的文本和图像
我显示数据库中的数据(例如文本和图像),但我的代码无法正常工作。我在我的项目中使用本教程。这是我的 java 类:
public class PepakPostView extends ListActivity {
protected TextView postOne;
protected TextView postTwo;
//protected view picture;
protected ListAdapter adapter;
protected int subcatId;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post);
subcatId = getIntent().getIntExtra("SUBCATEGORY_ID", 0);
SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT _id, subcatId, postOne, postTwo, picture FROM posts WHERE subcatId = ?",
new String[]{""+subcatId});
SimpleCursorAdapter pepak = new SimpleCursorAdapter(
this,
R.layout.post_list,
cursor,
new String[] {"postOne", "postTwo", "picture"},
new int[] { R.id.postOne, R.id.postTwo, R.id.picture });
pepak.setViewBinder(new MyViewBinder());
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater blowUp = getMenuInflater();
blowUp.inflate(R.menu.coll_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.about:
Intent i = new Intent("com.pepakbahasajawa.ABOUT");
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
return false;
}
}
和 MyViewBinder.java:
public class MyViewBinder implements ViewBinder {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewId = view.getId();
switch(viewId) {
/*
case R.id.postOne:
TextView postOne = (TextView) view;
postOne.setText(Cursor.getString(columnIndex));
break;
case R.id.postTwo:
TextView postTwo = (TextView) view;
postTwo.setText(Cursor.getString(columnIndex));
break;
*/
case R.id.picture:
ImageView pictureIcon = (ImageView) view;
int picture = cursor.getInt(columnIndex);
switch(picture) {
case 1:
pictureIcon.setImageResource(R.drawable.icon);
break;
case 2:
pictureIcon.setImageResource(R.drawable.home_normal);
break;
}
break;
}
return false;
}
}
如何解决这个问题?
I show the data from database like text and image, but my code is not working. I use this tutorial for my project. This my java class:
public class PepakPostView extends ListActivity {
protected TextView postOne;
protected TextView postTwo;
//protected view picture;
protected ListAdapter adapter;
protected int subcatId;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post);
subcatId = getIntent().getIntExtra("SUBCATEGORY_ID", 0);
SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT _id, subcatId, postOne, postTwo, picture FROM posts WHERE subcatId = ?",
new String[]{""+subcatId});
SimpleCursorAdapter pepak = new SimpleCursorAdapter(
this,
R.layout.post_list,
cursor,
new String[] {"postOne", "postTwo", "picture"},
new int[] { R.id.postOne, R.id.postTwo, R.id.picture });
pepak.setViewBinder(new MyViewBinder());
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater blowUp = getMenuInflater();
blowUp.inflate(R.menu.coll_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.about:
Intent i = new Intent("com.pepakbahasajawa.ABOUT");
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
return false;
}
}
and MyViewBinder.java
:
public class MyViewBinder implements ViewBinder {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewId = view.getId();
switch(viewId) {
/*
case R.id.postOne:
TextView postOne = (TextView) view;
postOne.setText(Cursor.getString(columnIndex));
break;
case R.id.postTwo:
TextView postTwo = (TextView) view;
postTwo.setText(Cursor.getString(columnIndex));
break;
*/
case R.id.picture:
ImageView pictureIcon = (ImageView) view;
int picture = cursor.getInt(columnIndex);
switch(picture) {
case 1:
pictureIcon.setImageResource(R.drawable.icon);
break;
case 2:
pictureIcon.setImageResource(R.drawable.home_normal);
break;
}
break;
}
return false;
}
}
How to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ListView 中不使用 pepak 变量。列表视图为空。
setListAdapter(pepak);//?
pepak variable is not used in ListView. ListView is empty.
setListAdapter(pepak);//?