我在那个适配器中创建了一个带有简单游标适配器的列表视图我现在获取我的sqlite列名我想从drawable文件夹向同一个适配器添加图像我们怎么做才能给出想法和建议谢谢..
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
final int[] imageResource =new int[] {R.drawable.juice, R.drawable.medicinebowl, R.drawable.kam};
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.listView);
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
sqLiteDatabase = dbHelper.getReadableDatabase();
cursor = dbHelper.gettitles(sqLiteDatabase);
String[] from = new String[]{dbHelper.TITLE, String.valueOf(circle)};
int[] to = new int[]{R.id.title,R.id.circle};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.title_row, cursor, from, to);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
最佳答案
为此,您需要自己的自定义布局和overidingsetViewBinder
方法,下面是一个可以帮助您理解的示例,
java类
// The desired columns to be bound
String[] columns = new String[] { DBHelper.KEY_IMAGE_TYPE, DBHelper.KEY_HOUSE,
DBHelper.KEY_PRICE };
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.icon,
R.id.bowler_txt};
SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(this,
R.layout.listrow, cursor, columns, to, 0);
dataAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor,
int columnIndex) {
switch (view.getId()) {
case R.id.image:
int imageType = cursor.getInt(columnIndex);
int imageToBeShown=0;
switch (imageType) {
case TYPE_ONE:
imageToBeShown=imgs[0];
break;
case TYPE_TWO:
imageToBeShown=imgs[1];
break;
case TYPE_THREE:
imageToBeShown=imgs[2];
break;
case TYPE_FOUR:
imageToBeShown=imgs[3];
break;
case TYPE_FIVE:
imageToBeShown=imgs[4];
break;
case TYPE_SIX:
imageToBeShown=imgs[5];
break;
}
((ImageView)view).setImageResource(imageToBeShown);
return true;
}
return false;
}
});
列表语言
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/bowler_txt"
android:paddingLeft="25dp"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bowler"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
关于android - 如何将图像从Drawable加载到SimpleCursorAdapter,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33452195/