我正在考虑如何在自定义列表视图页面上显示数据。你们可以帮我找出错误在哪里吗?
这是作业
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();
}
答案 0 :(得分: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监听器。