陷入困境。我是android开发的新手。我的问题是我有一个SQLite数据库,我在其中保存图像和一些数据,但是当我检索该数据图像时,列表视图中没有显示该图像。其他数据正在消失。
这是我的自定义列表布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/petImageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginTop="0dp"
android:src="@drawable/cat1"/>
<TextView
android:id="@+id/petNameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="50dp"
android:layout_marginTop="22dp"
android:layout_marginRight="50dp"
android:layout_toEndOf="@id/petImageView"
android:layout_toRightOf="@id/petImageView"
android:background="@android:color/background_light"
android:textAlignment="center"
android:textSize="35sp"
android:textStyle="bold" />
</RelativeLayout>
这是我的自定义适配器
public class CustomAdapter extends BaseAdapter {
private int layout;
private ArrayList<DataList> recordList;
private Context context;
public CustomAdapter(Context context, int layout, ArrayList<DataList> recordList) {
this.context = context;
this.recordList = recordList;
this.layout=layout;
}
public int getCount() {
return recordList.size();
}
public Object getItem(int position) {
return recordList.get(position);
}
public long getItemId(int position) {
return position;
}
private class ViewHolder{
ImageView petImageView;
TextView petNameTextView;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder = new ViewHolder();
if (v == null) {
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = layoutInflater.inflate(layout, null);
holder.petImageView = v.findViewById(R.id.petImageView);
holder.petNameTextView = v.findViewById(R.id.petNameTextView);
v.setTag(holder);
}else{
holder = (ViewHolder)v.getTag();
}
DataList datalist = recordList.get(position);
holder.petNameTextView.setText(datalist.getName());
byte[] recordImage = datalist.getImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(recordImage, 0, recordImage.length);
holder.petImageView.setImageBitmap(bitmap);
return v;
}
}
这是活动
public class myPetsActivity extends AppCompatActivity {
ListView myPetList;
ArrayList<DataList> petList = new ArrayList<DataList>();
CustomAdapter customAdapter;
ImageView petImageView;
DatabaseHelper mDatabaseHelper;
String name;
byte[] image;
int id;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_pets);
myPetList = findViewById(R.id.petsListView);
petList = new ArrayList<>();
customAdapter = new CustomAdapter(this, R.layout.custom_list_layout, petList);
myPetList.setAdapter(customAdapter);
mDatabaseHelper = new DatabaseHelper(this);
Cursor data = mDatabaseHelper.getData("SELECT * FROM pet_table");
petList.clear();
while(data.moveToNext()) {
id = data.getInt(0);
name = data.getString(1);
image = data.getBlob(2);
petList.add(new DataList(id, name, image));
Log.i("image",String.valueOf(image));
}
customAdapter.notifyDataSetChanged();
}
}
这是DataList
public class DataList {
private int id;
private byte[] image ;
private String name;
public DataList(int id, String name, byte[] image){
this.id=id;
this.name=name;
this.image=image;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
答案 0 :(得分:0)
尝试使用recyclerview而不是已弃用的listview:developer.android.com/guide/topics/ui/layout/recyclerview如果显示的是图像,但没有显示正确的图像,则可能在适配器代码中。
您也可以在while循环之后实例化适配器。
删除此行代码:
View v = convertView;
在Java中,当您将对象彼此分配时,并不意味着它们是同一对象,它们只是指向内存中的相同引用。 Java处理原始对象,其他对象则有所不同。
遵循以下示例: https://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html
答案 1 :(得分:0)
您是否考虑过将图像作为字符串保存到数据库(Base64)?从数据库读取后,可以简单地将其转换回去。在这里,您可以执行以下操作:How to convert a captured Image or image selected from gallary to base64?
答案 2 :(得分:0)
本质上,您的代码没有出现任何问题,如图所示。因此,问题可能出在将图像存储到数据库中。
以下工作示例,仅对代码进行了很小的更改,然后实际上只是建议(一个例外,就是将 myPetsActivity 命名为 MyPetsActivity 以遵循通用约定) 。但是, DatabaseHelper.java 是完整编写的,可能无法完全反映您的 DatabaseHelper.java
一个区别是图像本身已放置在资产文件夹中,并且已从该文件夹中检索以存储到 pet_table 表中,请参见DatabaseHelper中的 addPetWithImageFromImageset 方法.java类。
另一个区别是上述方法已用于用一些测试数据填充数据库(请参见 MyPetsActivity.java 中的 addSomeData 方法)。一排 为名为 先生的宠物。已为不可见的宠物 提供了不存在的图像(这将导致BLOB使用图像的默认值x'00',因为它不显示,所以很好。) / p>
custom_list_layout.xml 已删除了不必要的RelativeLayout(如果未删除它仍然可以使用)。为了适当地适合我的测试(使用Android 4.1.1设备),它还添加了2个属性marginLeft和alignParentLeft。
我只是将一些可用的JPG小图像复制到资产文件夹中,该文件夹如下所示:-
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mypets.db";
public static final int DBVERSION = 1;
public static final String TABLE_PET = "pet_table";
public static final String COLUMN_PET_ID = BaseColumns._ID;
public static final String COLUMN_PET_NAME = "name";
public static final String COLUMN_PET_IMAGE = "image";
public static final String COLUMN_PET_IMAGEPATH = "imagepath";
SQLiteDatabase mDB;
Context mContext;
public DatabaseHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
mContext = context;
mDB = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
String crt_pet_table = "CREATE TABLE IF NOT EXISTS " + TABLE_PET + "(" +
COLUMN_PET_ID + " INTEGER PRIMARY KEY, " +
COLUMN_PET_NAME + " TEXT," +
COLUMN_PET_IMAGE + " BLOB DEFAULT x'00'," +
COLUMN_PET_IMAGEPATH + " TEXT DEFAULT ''" +
")";
db.execSQL(crt_pet_table);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
/**
* NOT USED
* @param petname
* @return
*/
public long addPet(String petname) {
ContentValues cv = new ContentValues();
cv.put(COLUMN_PET_NAME,petname);
return mDB.insert(TABLE_PET,null,cv);
}
public long addPetWithImageFromAssets(String petname, String petimagename) {
byte[] petimage = new byte[0];
int image_size = 0;
try {
InputStream is = mContext.getAssets().open(petimagename);
image_size = is.available();
petimage = new byte[image_size];
is.read(petimage);
is.close();
} catch (IOException e) {
}
ContentValues cv = new ContentValues();
cv.put(COLUMN_PET_NAME,petname);
if (image_size > 0) {
cv.put(COLUMN_PET_IMAGE,petimage);
}
return mDB.insert(TABLE_PET,null,cv);
}
public Cursor getData(String query) {
return mDB.rawQuery(query,null);
}
}
DataList.java
public class DataList {
/**
* NOTE changed id to use long rather than int
*/
private long id;
private byte[] image ;
private String name;
public DataList(int id, String name, byte[] image){
this.id=id;
this.name=name;
this.image=image;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MyPetsActivity.java
public class MyPetsActivity extends AppCompatActivity {
ListView myPetList;
ArrayList<DataList> petList = new ArrayList<DataList>();
CustomAdapter customAdapter;
ImageView petImageView;
DatabaseHelper mDatabaseHelper;
String name;
byte[] image;
int id;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_pets);
myPetList = findViewById(R.id.petsListView);
petList = new ArrayList<>();
customAdapter = new CustomAdapter(this, R.layout.custom_list_layout, petList);
myPetList.setAdapter(customAdapter);
mDatabaseHelper = new DatabaseHelper(this);
addSomeData(); //<<<<<<<<<< FOR DEMO
Cursor data = mDatabaseHelper.getData("SELECT * FROM pet_table");
petList.clear();
while(data.moveToNext()) {
id = data.getInt(0);
name = data.getString(1);
image = data.getBlob(2);
petList.add(new DataList(id, name, image));
Log.i("image",String.valueOf(image));
}
data.close(); //<<<<<<<<<< SHOULD ALWAYS CLOSE CURSOR WHEN DONE WITH IT
customAdapter.notifyDataSetChanged();
}
private void addSomeData() {
mDatabaseHelper.getWritableDatabase().delete(DatabaseHelper.TABLE_PET,null,null); //<<<<<<<<<< Delete all pets
mDatabaseHelper.addPetWithImageFromAssets("Fluffy","mypet001.JPG");
mDatabaseHelper.addPetWithImageFromAssets("Not Fluffy","mypet002.JPG");
mDatabaseHelper.addPetWithImageFromAssets("Petty","mypet003.JPG");
mDatabaseHelper.addPetWithImageFromAssets("Mr. Invisible Pet","noimageforthispet"); //<<<<<<< ooops!!!!!
}
}
CustomAdapter.java
activity_my_pets.xml
。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<ListView
android:id="@+id/petsListView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
custom_list_layout.xml
微小的变化
<TextView
android:id="@+id/petNameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="50dp"
android:layout_marginTop="22dp"
android:layout_marginRight="50dp"
android:layout_toEndOf="@id/petImageView"
android:layout_toRightOf="@id/petImageView"
android:background="@android:color/background_light"
android:textAlignment="center"
android:textSize="35sp"
android:textStyle="bold" />