繁体   English   中英

测试get方法时getter返回null

[英]Getter returning null when testing get methods

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

任何见解都将不胜感激,因为我在这里迷路了。 提前致谢。

这是我初始化 class 的地方:

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

这是 model class:

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

}

请如下更改构造函数,看看是否有效,

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

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

您将值传递给 model 构造函数,但您没有将其分配给 model 变量。 您需要更改代码如下,

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()方法初始化每个变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM