首页 > 解决方案 > 从 SQLite 数据库中检索图像并在 ListView 中显示

问题描述

卡住了一个问题。我是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();
    }
    }

这是数据列表

    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;
}
}

标签: javaandroidandroid-sqliteandroid-custom-view

解决方案


尝试使用 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


推荐阅读