java - 如何使用 ImageView 将现有 sqlite 数据库中的数据填充到 ListView
问题描述
我正在寻找一种方法来将我之前使用“DB Browser”创建的现有 sqlite 数据库中的数据显示到 ListView 中。在数据库中有一个徽标/图像 (Blob) 列,我想将其输入到 ListView 中。我怎样才能做到这一点?
我从网上得到了下面的代码,但不幸的是这种方法不能显示图像,只能在一行中显示两行。
这是我的 main_activity 更改为 Home_Act..
public class Home_Act extends AppCompatActivity {
static final String DBNAME = "testing.db";
static final String DBASSETPATH = "databases/" + DBNAME;
static final String CHANNELTABLE = "Channel_Info";
static final String KEY_NO = "No";
static final String KEY_NAME = "Name";
static final String KEY_CATEGORY = "Category";
static final String KEY_LOGO = "Logo";
ListView channelList;
SQLiteDatabase mDB;
SimpleCursorAdapter mSCA;
Cursor mCsr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_home);
channelList = this.findViewById(R.id.channelList);
mDB = openDB();
if (mDB != null) {
mCsr = mDB.query(CHANNELTABLE,
new String[]{KEY_NO + " AS _id",
KEY_NAME, KEY_CATEGORY, KEY_LOGO
},
null,null,null,null,null);
mSCA = new SimpleCursorAdapter(this,R.layout.mylist,mCsr,
new String[]{KEY_NAME, KEY_CATEGORY, KEY_LOGO},
new int[]{R.id.item, R.id.textView1, R.id.icon},0);
mSCA.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
// Binds the Cursor column defined by the specified index to the specified view
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
if(view.getId() == R.id.icon){
//...
String channelName = cursor.getString(3);
Home_Act.this.getResources();
int resID = getResources().getIdentifier(channelName, "drawable", getPackageName());
//Option 1
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //>= API 21
((ImageView)view).setImageDrawable(getResources().getDrawable(resID, getApplicationContext().getTheme()));
} else {
((ImageView)view).setImageDrawable(getResources().getDrawable(resID));
}
//Option 2
//((ImageView)view).setImageDrawable(getResources().getDrawable(resID));
return true; //true because the data was bound to the view
}
return false;
}
});
channelList.setAdapter(mSCA);
} else {
Toast.makeText(this,"Unable to open Database.",Toast.LENGTH_LONG);
}
channelList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem = itemname[+position];
channelName = Slecteditem;
OpenDialog();
//Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
}
});
}
private SQLiteDatabase openDB() {
String dbpath = this.getDatabasePath(DBNAME).getPath();
if (this.getDatabasePath(DBNAME).exists()) {
Log.d("OPENDB","Opening already existing Database");
return SQLiteDatabase.openDatabase(dbpath,null,SQLiteDatabase.OPEN_READWRITE);
}
InputStream is;
byte[] buffer;
FileOutputStream db;
try {
is = this.getAssets().open(DBASSETPATH);
buffer = new byte[is.available()];
is.read(buffer);
is.close();
} catch (Exception e) {
e.printStackTrace();
Log.d("OPENDB","Unable to locate or buffer input from assets " + DBASSETPATH);
return null;
}
// Just in case the databases directory doesn't exist create it.
File dbmkdir = (this.getDatabasePath(DBNAME)).getParentFile();
dbmkdir.mkdirs();
try {
db = new FileOutputStream(this.getDatabasePath(DBNAME).getPath());
} catch (Exception e) {
e.printStackTrace();
Log.d("OPENDB","Unable to create outputstream for DB at path " + dbpath);
try {
is.close();
} catch (Exception e2) {
}
return null;
}
try {
db.write(buffer);
db.flush();
db.close();
is.close();
} catch (Exception e) {
Log.d("OPENDB","Failed to copy asset to DB");
e.printStackTrace();
return null;
}
return SQLiteDatabase.openDatabase(dbpath,null,SQLiteDatabase.OPEN_READWRITE);
}
上面的代码仅在 1 行中显示 2 行,我想在左侧包含图像。
这是上面的代码,图像没有显示,也没有错误。
这就是我想要的。
编辑 :
现在它正在按我的意愿工作。第一,您必须拥有自己的布局,其中包括 2 个 TextView 和 1 个 ImageView。使用 .xml 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="100dp"
android:layout_height="60dp"
android:padding="5dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#33CC33" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
第二,按照上面的代码。
解决方案
SimpleCursorAdapter
不支持中的图像to
。在你的代码中
new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, mCsr,
new String[]{KEY_NAME, KEY_CATEGORY, KEY_LOGO},
new int[]{android.R.id.text1, android.R.id.text2}, 0);
您将 3 列传递给 2 个文本视图,这些文本视图毫无例外地显示在 UI 中。如果要显示图像 + 文本视图,则需要创建自己的自定义适配器。
或者尝试如下设置:
mSCA.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
/** Binds the Cursor column defined by the specified index to the specified view */
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
if(view.getId() == R.id.your_image_view_id){
String yourImageNameFromDb = cursor.getString(2);
int resID = getResources().getIdentifier(yourImageNameFromDb , "drawable", getPackageName());
((ImageView)view).setImageDrawable(getResources().getDrawable(resID));
return true; //true because the data was bound to the view
}
return false;
}
});
推荐阅读
- google-bigquery - 如何使用 CLI bq 命令创建一次性数据集副本(无计划重复)
- android - 如何在 xml 中为 android 背景制作多色渐变?
- amazon-web-services - AWS Elastic Beanstalk EFS 挂载错误:未知文件系统类型“efs”
- matlab - 在 MATLAB 中实现因式分解?
- mysql - 为什么“Multi-statement transaction required more than 'max_binlog_cache_size' byte s of storage”异常只发生在部分从站中
- android - gemfile 将如何维护,作为 iOS 和 Android 在 react-native 应用程序中的通用文件
- python - AWS 精确定位 | 有没有办法向拥有该应用程序的所有用户发送精确通知?
- excel - 我正在寻找一种方法来从工作表 C 中选择一个值,将其与工作表 A 中的一个值匹配,然后从 A 填充 C
- javascript - 将新数组动态添加到现有对象数组 React JS
- r - R - 小值的错误计算