[英]How to populate data from existing sqlite database to ListView with ImageView
I'm looking for a way to display data from the existing sqlite database that I created earlier using "DB Browser" into ListView.我正在寻找一种方法来将现有 sqlite 数据库中的数据显示到 ListView 中,该数据库是我之前使用“DB Browser”创建的。 In the database there is a Logo / Image (Blob) column that I want to enter into the ListView.
在数据库中有一个徽标/图像 (Blob) 列,我想将其输入到 ListView 中。 How can I do that?
我怎样才能做到这一点?
I got the code below from the internet, but unfortunately this method cannot display images, only displays two lines in one row.我从网上得到了下面的代码,但不幸的是这种方法不能显示图像,只能在一行中显示两行。
This is my main_activity changed to Home_Act..这是我的 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);
}
The code above only shows 2 lines in 1 row, which I want to include the image on the left.上面的代码仅在 1 行中显示 2 行,我想在左侧包含图像。
this is With Code Above, image not showing and there is no error.这是上面的代码,图像没有显示,也没有错误。
and this is What I Want.这就是我想要的。
EDIT:编辑:
Now it's working as I want.现在它正在按我的意愿工作。 Number one, you must have your own layout which includes 2 TextView and 1 ImageView.
第一,您必须拥有自己的布局,其中包括 2 个 TextView 和 1 个 ImageView。 with the.xml code as follows:
用.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>
Number two, follow the code above.第二,按照上面的代码。
SimpleCursorAdapter
doesnt support image in to
. SimpleCursorAdapter
不支持图像to
And in your code在你的代码中
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);
you are passing 3 columns to 2 textviews which shows in UI without any exception.您将 3 列传递给 2 个文本视图,这些文本视图毫无例外地显示在 UI 中。 If you want to show image + textViews you need to create your own custom Adapter.
如果要显示图像 + 文本视图,则需要创建自己的自定义适配器。
Or try to set it as below:或者尝试如下设置:
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;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.