我的列表视图无法显示数据库中的所有元素

时间:2019-04-02 11:34:01

标签: android database sqlite listview

我有一个将名称和图像存储在数据库中的应用程序。然后我在列表视图上打印图像的名称。但是只是我能够从数据库中看到前三个元素。它向我显示窗口是完全错误的。数据库没有问题,我通过sqlitebrowser检查了数据库,所有元素都在那里。我的arrayadapter可能有问题。什么问题?图像也很小30kb。 尝试在ListView上查看所有图像时出错

错误

W/CursorWindow: Window is full: requested allocation 5397663 bytes, free space 1143664 bytes, window size 2097152 bytes
W/CursorWindow: Window is full: requested allocation 5397663 bytes, free space 2023760 bytes, window size 2097152 bytes
Window is full: requested allocation 5397663 bytes, free space 2096700 bytes, window size 2097152 bytes
E/SQLiteCursor: onMove() return false. RequiredPos: 3 WindowStartPos: 3 WindowRowCount: 0(original count of query: 6) 

OnCreate方法

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView = (ListView) findViewById(R.id.listView);

        final ArrayList<String> artName = new ArrayList<String>();
        artImage = new ArrayList<Bitmap>();

        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,artName);
        listView.setAdapter(arrayAdapter);

        try {

            Main2Activity.database = this.openOrCreateDatabase("Arts", MODE_PRIVATE, null);
            Main2Activity.database.execSQL("CREATE TABLE IF NOT EXISTS arts (name VARCHAR, image BLOB)");

            Cursor cursor = Main2Activity.database.rawQuery("SELECT * FROM arts", null);//cursor is for data recieving

            int nameIx = cursor.getColumnIndex("name");
            int imageIx = cursor.getColumnIndex("image");

            cursor.moveToFirst();

            while (cursor != null) {

                artName.add(cursor.getString(nameIx));
                //adding bitmap into artImage
                byte[] byteArray = cursor.getBlob(imageIx);
                //decoding bitmaps
                Bitmap image = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
                artImage.add(image);

                cursor.moveToNext();

                arrayAdapter.notifyDataSetChanged();//data degistiyse arrayadapter'a haber veriyor ve guncelleyip kullaniciya gosterir

            }


        } catch (Exception e) {
            e.printStackTrace();
        }

         //selecting saved images
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
                intent.putExtra("info", "old");
                intent.putExtra("name", artName.get(position));
                intent.putExtra("position", position);

                startActivity(intent);

            }
        });

    }
}

来自XML的ListView

 <ListView
        android:id="@+id/listView"
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp" />

1 个答案:

答案 0 :(得分:1)

一个Cursor窗口的限制为2MB,一行必须适合一个Cursor窗口,不能在多个Cursor窗口中拆分,因此不能通过Cursor检索5Mb图像(CursorWindow实际上是Cursor的缓冲区)。

处理此类图像的最佳方法是将图像存储为文件并将图像的路径存储在数据库中。 This answer显示了存储图像路径(以及图像尺寸小于一定尺寸(100k)时的图像)的示例,并且还包含有关主体的一些基础细节。

How to use images in Android SQLite that are larger than the limitations of a CursorWindow?

中描述了一种笨拙的存储大图像的方式,尽管不推荐