测试get方法时,getter返回null

时间:2020-05-31 16:03:45

标签: java android arraylist android-sqlite getter

我正在尝试从数据库中获取数据以显示在列表视图中。我遇到的问题是吸气剂似乎无法正常工作。当我测试他们返回的内容时,它返回null。

任何见识将不胜感激,因为我在这里迷路了。预先感谢。

这是我上课的地方:

    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 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);
    testVar = gameStats.getlName();
    Log.d("MyAdaptor","gameStats = " + var);
    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;
}

}

这是我调用方法的地方:

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

}

3 个答案:

答案 0 :(得分:1)

请按如下所示更改构造函数,看看是否可行

public GameStats(int id, String lName, int lScore, String rName, int rScore, String notes) {
    this.id = id;
    this.lName = IName;
    this.lScore = IScore;
    this.rName = rName;
    this.rScore = rScore;
    this.notes = notes;
    }

答案 1 :(得分:1)

在模型类中,使用constrtuctor初始化变量。我想这就是问题所在。由于您没有初始化模型类的属性,因此getter将返回“ null”或任何垃圾值

答案 2 :(得分:1)

您正在将值传递给模型构造函数,但没有将其分配给模型变量。您需要按以下方式更改代码,

public GameStats(int id, String lName, int lScore, String rName, int rScore, String notes) {
    this.id = id;
    this.lName = IName;
    this.lScore = IScore;
    this.rName = rName;
    this.rScore = rScore;
    this.notes = notes;
    }

否则,通过setter()方法初始化每个变量。