java - SQLite 数据库未向自定义 ListView 显示数据
问题描述
我正在尝试将保存在我的 SQLite 数据库中的数据放入我的应用程序中的自定义 ListView 中。似乎适配器工作正常,因为它膨胀了布局,但是,视图中没有数据。我希望这是一个简单的修复,我很感激任何输入:)
将显示数据的活动:
public class SavedGameScreen extends AppCompatActivity {
ListView lv1;
ArrayList<GameStats> arrayList;
MyAdaptor myAdaptor;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saved_game_screen);
lv1 = findViewById(R.id.lv1);
databaseHelper = new DatabaseHelper(this);
arrayList = new ArrayList<>();
loadData();
}
private void loadData() {
arrayList = databaseHelper.getAllData();
myAdaptor = new MyAdaptor(this, arrayList);
lv1.setAdapter(myAdaptor);
myAdaptor.notifyDataSetChanged();
}
}
数据库助手:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "savedGames.db";
public static final String TABLE_NAME = "savedGamesTable";
public static final String COL_1 = "ID";
public static final String COL_2 = "lName";
public static final String COL_3 = "lScore";
public static final String COL_4 = "rName";
public static final String COL_5 = "rScore";
public static final String COL_6 = "notes";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table "+TABLE_NAME+" (ID INTEGER PRIMARY KEY AUTOINCREMENT, lName TEXT," +
"lScore INTEGER, rName TEXT, rScore INTEGER, notes TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);
onCreate(sqLiteDatabase);
}
public boolean insertData(String lName, int lScore, String rName, int rScore, String notes) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, lName);
contentValues.put(COL_3, lScore);
contentValues.put(COL_4, rName);
contentValues.put(COL_5, rScore);
contentValues.put(COL_6, notes);
long result = sqLiteDatabase.insert(TABLE_NAME, null, contentValues);
if(result == -1) {
return false;
}
else{
return true;
}
}
public ArrayList<GameStats> getAllData() {
ArrayList<GameStats> arrayList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM savedGamesTable", null);
while(cursor.moveToNext()){
int id = cursor.getInt(0);
String lName = cursor.getString(1);
int lScore = cursor.getInt(2);
String rName = cursor.getString(3);
int rScore = cursor.getInt(4);
String notes = cursor.getString(5);
GameStats gameStats = new GameStats(id, lName, lScore, rName, rScore, notes);
arrayList.add(gameStats);
}
return arrayList;
}
}
我的适配器:
public class MyAdaptor extends BaseAdapter {
Context context;
ArrayList<GameStats> arrayList;
public MyAdaptor(Context context, ArrayList<GameStats> arrayList){
this.context = context;
this.arrayList = arrayList;
}
@Override
public int getCount() {
return this.arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.activity_saved_games, null);
TextView lName = convertView.findViewById(R.id.lName);
TextView lScore = convertView.findViewById(R.id.lScore);
TextView rName = convertView.findViewById(R.id.rName);
TextView rScore = convertView.findViewById(R.id.rScore);
TextView notes = convertView.findViewById(R.id.notes);
GameStats gameStats = arrayList.get(position);
lName.setText(gameStats.getlName());
lScore.setText(String.valueOf(gameStats.getlScore()));
rName.setText(gameStats.getrName());
rScore.setText(String.valueOf(gameStats.getrScore()));
notes.setText(gameStats.getNotes());
return convertView;
}
}
型号类:
public class GameStats {
int id, lScore, rScore;
String lName, rName, notes;
public GameStats(int id, String lName, int lScore, String rName, int rScore, String notes) {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getlScore() {
return lScore;
}
public void setlScore(int lScore) {
this.lScore = lScore;
}
public int getrScore() {
return rScore;
}
public void setrScore(int rScore) {
this.rScore = rScore;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public String getrName() {
return rName;
}
public void setrName(String rName) {
this.rName = rName;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
}
解决方案
推荐阅读
- ios - Swift - 出现在上下文中的 ViewController 的深色背景
- reporting-services - 如何根据查询返回的数据填充 rdl 表?
- c# - C#如何消费REST Web服务
- azure-api-management - Azure API 管理和微服务
- bash - 如何从变量中删除回车符并将其用于bash中的字符串比较?
- ios - Swift 4.0 - 无法在 iOS 中创建空文本文件
- youtube-dl - 如何使用 Youtube-dl 从 youtube 播放列表中下载特定数量的视频?
- javascript - Bootstrap轮播更改幻灯片时防止浏览器向上滚动
- excel - 仅从第一个工作簿复制粘贴标题
- go - 为什么 unsafe.Sizeof 被认为是不安全的?