我显示数据库中的数据,如文本和图像,但我的代码无效。我使用this tutorial作为我的项目。这是我的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;
}
}
如何解决这个问题?
答案 0 :(得分:0)
在ListView中未使用pepak变量。 ListView为空。
setListAdapter(pepak); //