I am new to android. I am developing an app. I am have data in json format which I wan to show in listview. I am using baseadapter and setters and getters. But getter function returns the same value which was last set. Here is my code.
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);
}
}
This is how i am setting adapter in postExecute() method
myAdapter = new HistoryAdapter(HistoryActivity.this, history);
list.setAdapter(myAdapter);
My setter and getter functions
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;
}
And this adapter
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;
}
}
This is my 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"}]
UPDATE
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);
}
Please help me... Thanks
It is because here:
pojo = new Pojo();
for (int k = 0; k < arr.length(); k++) {
you are simply changing over and over the same Pojo
reference, while you want to add a new instance at every iteration:
for (int k = 0; k < arr.length(); k++) {
pojo = new Pojo();
Try this it will work:
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);
}
It may be happening as you are not creating or reassigning your pojo object in the for loop. for each new set of values you should create a new object of your getter setter class that is your POJO
class.
You use pojo = new Pojo();
once and use history.add(pojo);
,so history
only contains a reference list to same pojo.
In you Pojo class , add 3 arguments constructor and use below code .
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.