Getter函数没有返回正确的值

时间:2015-10-07 09:10:35

标签: java android baseadapter getter-setter

我是android新手。我正在开发一个应用程序。我有json格式的数据,我想在listview中显示。我正在使用baseadapter和setter和getters。但是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);

我的二传手和吸气功能

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

请帮帮我......谢谢

4 个答案:

答案 0 :(得分:2)

这是因为:

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

您只是在同一个Pojo引用中进行切换,而您希望在每次迭代时添加一个新实例:

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

答案 1 :(得分:1)

试试这个会起作用:

JSONArray arr = new JSONArray(strServerResponse);
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 = new Pojo();
    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);
}

可能会发生这种情况,因为您没有在for循环中创建或重新分配pojo对象。对于每个新的值集,您应该创建getter setter类的新对象,即POJO类。

答案 2 :(得分:0)

您使用pojo = new Pojo();一次并使用history.add(pojo);,因此history仅包含相同pojo的参考列表。

答案 3 :(得分:0)

Pojo 类中,添加3个参数构造函数并使用下面的代码。

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

   history.add(new Pojo(jsonObj1.optString("login_date"),jsonObj1.optString("login_time"),jsonObj1.optString("logout_time")));
   history.add(pojo);
 }