自定义listview无法显示所有数据,android studio

问题描述 投票:0回答:1

我正在考虑如何在自定义列表视图页面上显示数据。你能帮助我找出错误在哪里吗?

这是作业

Model txn;
        public SQLiteHelper mSQLiteHelper;
        ListView mListView;
        ArrayList<Model> mList;
        RecordListAdapter mAdapter = null;

这是初始化

 mListView = findViewById(R.id.listView);
        mList = new ArrayList<>();
        mAdapter = new RecordListAdapter(this, R.layout.row, mList);
        mListView.setAdapter(mAdapter);
        mSQLiteHelper = new SQLiteHelper(this);
        mList = new ArrayList<>();

这是显示所有数据的页面,我将调试器调试。它可以获取包含15列的所有数据,但它无法在listview页面显示。这段代码有什么不对吗?

try {
            SQLiteDatabase db = mSQLiteHelper.getReadableDatabase();
            Cursor cursor = db.rawQuery("select * from Table1", null);
            mList.clear();
            while (cursor.moveToNext()) {
                txn = new Model();
                txn.setId(cursor.getInt(cursor.getColumnIndex("id")));
                txn.setName(cursor.getString(cursor.getColumnIndex("name")));
                txn.setAddress(cursor.getString(cursor.getColumnIndex("address")));
                txn.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
                mList.add(new Model());
            }

            mAdapter.notifyDataSetChanged();
            if (mList.size() == 0) {
                Toast.makeText(this, "No record found...", Toast.LENGTH_SHORT).show();
            }

            mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int i, long l) {
                    return false;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
android listview android-sqlite crud
1个回答
1
投票

请检查以下代码

mListView = findViewById(R.id.listView);

mSQLiteHelper = new SQLiteHelper(this);
mList = new ArrayList<>();

mAdapter = new RecordListAdapter(this, R.layout.row, mList);
mListView.setAdapter(mAdapter);

数据绑定功能代码

try {
        SQLiteDatabase db = mSQLiteHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from Table1", null);
        mList.clear();
        while (cursor.moveToNext()) {
            txn = new Model();
            txn.setId(cursor.getInt(cursor.getColumnIndex("id")));
            txn.setName(cursor.getString(cursor.getColumnIndex("name")));
            txn.setAddress(cursor.getString(cursor.getColumnIndex("address")));
            txn.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
            mList.add(txn);
        }

        mAdapter.notifyDataSetChanged();
        if (mList.size() == 0) {
            Toast.makeText(this, "No record found...", Toast.LENGTH_SHORT).show();
        }

        mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int i, long l) {
                return false;
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }

我还有一个建议,不要在数据绑定函数中添加listview click listener。

© www.soinside.com 2019 - 2024. All rights reserved.