我在该适配器中使用简单光标适配器创建一个列表视图我现在获取我的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);
答案 0 :(得分:0)
您只需添加此行
即可your_imageview.setImageResource(R.drawable.drwable_image);
如果您尚未创建扩展SimpleCursorAdapter的自定义适配器,则可以执行此操作
String[] columns = new String[] { People.NAME, People.NUMBER };
int[] to = new int[] { R.id.name_entry, R.id.number_entry };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_example_entry, cursor, columns, to);
colomns数组应包含日期,而to数组应包含要为其分配数据的对象ID
这是图像
listAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
if(view.getId() == R.id.your_image_view_id){
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; //true because the data was bound to the view
}
return false;
}
});
设置图像资源应该取决于您的行类型
答案 1 :(得分:0)
为此,您需要自己的自定义布局,并且还需要覆盖setViewBinder
方法,以下是一个可以帮助您理解的示例,
YourClass.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;
}
});
listrow.xml
<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>
答案 2 :(得分:0)
我认为你可以使用这种方法
int[] imageResource = {R.drawable.image_1, R.drawable.image_2, R.drawable.image_3};
并将其传递给SimpleCursorAdapter构造函数。