测试get方法时返回null的Getter

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

我试图从我的数据库中获取数据以显示在列表视图中。我遇到的问题是,似乎获取器不能正常工作。当我测试它们返回的数据时,它返回的是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;
}

这里是我尝试使用getters的地方:

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

}

java android arraylist android-sqlite getter
1个回答
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
投票

在你的模型类中,使用constrtuctor初始化变量。我想这就是问题所在。因为你没有初始化模型类的属性,所以获取器会返回 "null "或任何垃圾值。


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

Else通过以下方式初始化每个变量 setter() 方法。

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