gpt4 book ai didi

java - Getter 函数没有返回正确的值

转载 作者:搜寻专家 更新时间:2023-11-01 08:40:35 26 4
gpt4 key购买 nike

我是安卓新手。我正在开发一个应用程序。我有 json 格式的数据,我想在 ListView 中显示这些数据。我正在使用 baseadapter 和 setter 和 getter。但是 getter 函数返回的值与上次设置的值相同。这是我的代码。

    JSONArray arr = new JSONArray(strServerResponse);
pojo = new Pojo();
for (int k = 0; k < arr.length(); k++) {
JSONObject jsonObj1 = arr.getJSONObject(k);
date = jsonObj1.optString("login_date");
inTime = jsonObj1.optString("login_time");
outTime = jsonObj1.optString("logout_time");
pojo.setDate(date);
pojo.setLoginTime(inTime);
pojo.setLogoutTime(outTime);
Log.e("history activity date", "" + date);
Log.e("history intime", "" + inTime);
Log.e("history outtime", "" + outTime);
history.add(pojo);
}
}

这就是我在 postExecute() 方法中设置适配器的方式

     myAdapter = new HistoryAdapter(HistoryActivity.this, history);
list.setAdapter(myAdapter);

我的 setter 和 getter 函数

  public void setLoginTime(String loginTime) {
this.loginTime = loginTime;
Log.e("Settter loginTime",""+loginTime);
}

public String getLoginTime() {
Log.e("getter loginTime",""+loginTime);
return loginTime;

}

public void setLogoutTime(String logoutTime) {

this.logoutTime = logoutTime;
Log.e("Settter logoutTime",""+logoutTime);
}

public String getLogoutTime() {
Log.e("getter loginTime",""+logoutTime);
return logoutTime;
}

public void setDate(String date) {
this.date = date;
Log.e("Settter date",""+date);
}

public String getDate() {
Log.e("getter date",""+date);
return date;
}

还有这个适配器

  public class HistoryAdapter extends BaseAdapter {
private Context activity;
TextView datetime;
TextView inTime;
TextView outTime;


Pojo pojo;
ArrayList<Pojo> list;
private ArrayList<Pojo> arraylist=null;
public static LayoutInflater inflater;

public HistoryAdapter(Context a, ArrayList<Pojo> history){
// TODO Auto-generated constructor stub
activity=a;
list=history;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.arraylist = new ArrayList<Pojo>();
this.arraylist.addAll(list);

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
v = inflater.inflate(R.layout.history_item, parent, false);
pojo = list.get(position);
datetime = (TextView)v.findViewById(R.id.historyDate);
datetime.setText(pojo.getDate());
inTime = (TextView)v.findViewById(R.id.historyLoginTime);
inTime.setText(pojo.getLoginTime());
outTime = (TextView)v.findViewById(R.id.historyLogoutTime);
outTime.setText(pojo.getLogoutTime());
return v;

}

}

这是我的json

  [{"login_time":"16:18:39","logout_time":"16:24:45","login_date":"2015-10-01"},{"login_time":"11:46:56","logout_time":"11:52:08","login_date":"2015-10-03"},{"login_time":"16:10:34","logout_time":"16:10:53","login_date":"2015-10-06"},{"login_time":"16:26:26","logout_time":"16:26:59","login_date":"2015-10-06"},{"login_time":"17:13:54","logout_time":"17:14:16","login_date":"2015-10-06"},{"login_time":"18:58:08","logout_time":"19:05:03","login_date":"2015-10-06"},{"login_time":"16:20:49","logout_time":"20:12:57","login_date":"2015-10-06"}]

更新

JSONArray arr = new JSONArray(strServerResponse);
for (int k = 0; k < arr.length(); k++) {
Pojo pojo = new Pojo();
JSONObject jsonObj1 = arr.getJSONObject(k);
date = jsonObj1.optString("login_date");
inTime = jsonObj1.optString("login_time");
outTime = jsonObj1.optString("logout_time");

pojo.setDate(date);
pojo.setLoginTime(inTime);
pojo.setLogoutTime(outTime);
Log.e("history activity date", "" + date);
Log.e("history intime", "" + inTime);
Log.e("history outtime", "" + outTime);
history.add(pojo);
}

请帮帮我...谢谢

最佳答案

是因为这里:

  pojo = new Pojo();
for (int k = 0; k < arr.length(); k++) {

您只是一遍又一遍地更改相同的 Pojo 引用,而您希望在每次迭代时添加一个新实例:

  for (int k = 0; k < arr.length(); k++) {
pojo = new Pojo();

关于java - Getter 函数没有返回正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32988224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com