【发布时间】:2013-04-01 16:36:34
【问题描述】:
我有一个自定义列表,我正在通过 SimpleCursorAdapter 在其中加载数据..我的列表有一个图像视图,我想为列表的每一行更改其图像..我使用了 viewbilnder..但没有得到任何东西.. 这是我的适配器代码
private void getData() {
try {
// obtain a list of from DB
db = SQLiteDatabase.openDatabase(ClubCP.DbPath,null,SQLiteDatabase.CREATE_IF_NECESSARY);
String TABLE_NAME = "cat";
String COLUMN_ID = "_id";
String COLUMN_POET_ID = "poet_id";
String COLUMN_TEXT = "text";
String COLUMN_PARENT_ID = "parent_id";
String [] columns ={COLUMN_ID,COLUMN_TEXT,COLUMN_POET_ID};
String mySQL = "SELECT DISTINCT id as _id "+","+COLUMN_POET_ID+","+COLUMN_TEXT+","+COLUMN_PARENT_ID
+ " from "+TABLE_NAME
+ " where " + COLUMN_PARENT_ID+"= 0 "
+ " order by " + COLUMN_POET_ID+"= 0 ";
// String mySQL="SELECT DISTINCT id as _id,poet_id,text,parent_id from cat where parent_id=0 order by poet_id";
//Cursor c = db.query(TABLE_NAME,columns,COLUMN_PARENT_ID+"= 0", null, null, null, COLUMN_POET_ID);
Cursor c = db.rawQuery(mySQL, null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, c,
columns, new int[] {R.id.list_item_text_sub,R.id.list_item_text_main,R.id.list_item_text_id}, 0);
adapter.setViewBinder(new CustomViewBinder());
ListView list = (ListView) findViewById(R.id.list_poet_name);
list.setAdapter(adapter);
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), 1).show();
}
}
这是viewbinder
public class CustomViewBinder implements ViewBinder {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == cursor.getColumnIndex(ClubCP.KEY_POET_ID)) {
// If the column is IS_STAR then we use custom view.
int poet_id = cursor.getInt(columnIndex);
String path = ClubCP.SDcardPath + "/temp/"+poet_id+".jpg";
File imgFile = new File(path);
ImageView img=(ImageView)findViewById(R.id.list_item_img);
if(imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
img.setImageBitmap(myBitmap);
}
else
Toast.makeText(Read.this,"no IMAGE IS PRESENT'",Toast.LENGTH_SHORT).show();
img.setImageResource(R.drawable.music_album_header_vinyl);
}
/// if (poet_id != 6) {
//TextView verse = (TextView)findViewById(R.id.poem_verse_list_item_01);
// verse.setGravity(Gravity.LEFT);
// } else {
// }
// return true;
// }
return false;
}
}
有人可以帮我解决这个问题吗? 我搜索并找到了一种从数据库加载图像的方法,但我想从 sdcard 加载图像并且只有图像的名称来自数据库
我搜索了很多,但找不到任何解决方案来解决我的问题...没有人可以帮助我解决这个问题吗?
【问题讨论】:
-
使用惰性加载器,检查stackoverflow.com/questions/541966/…
-
而不是
ImageView img=(ImageView)findViewById(R.id.list_item_img);使用ImageView img=(ImageView)view; -
谢谢我会测试它并说它是否有效..再次感谢
-
我更改了它,但我的列表中没有看到任何更改..我认为视图活页夹根本没有做任何事情..我不知道为什么?...关于惰性列表。 .我不知道如何使用它:(
标签: android imageview simplecursoradapter