我有一个大型的JSON数组。我正在从JSON数组中读取数据并将其存储在列表视图中,但列表视图会重复相同的数据。
,即来自JSON的最后一个数据,而不是显示所有数据。
这是我的代码。
public class HistoryActivity extends AppCompatActivity {
private Toolbar toolbar;
String strServerResponse = null;
ProgressDialog nDialog;
Pojo pojo;
ArrayList<Pojo> history;
HistoryAdapter myAdapter;
ListView list;
public String date, inTime, outTime, inLat, inLong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
toolbar = (Toolbar) findViewById(R.id.app_bar);
toolbar.setTitle("History");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
list = (ListView) findViewById(R.id.historyList);
history = new ArrayList<Pojo>();
new NetCheck().execute();
}
private class NetCheck extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
nDialog.dismiss();
// TODO Auto-generated method stub
myAdapter = new HistoryAdapter(HistoryActivity.this, history);
list.setAdapter(myAdapter);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpRequest = new HttpPost(
"http://myurl");
httpRequest.setHeader("Content-Type", "application/json");
SharedPreferences mmm = getSharedPreferences(
"MyPref", MODE_PRIVATE);
String logempid = mmm.getString("id", null);
JSONObject json = new JSONObject();
json.put("empid", logempid);
Log.e("JSON Object", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding("UTF-8");
se.setContentType("application/json");
httpRequest.setEntity(se);
HttpResponse httpRes = httpClient.execute(httpRequest);
java.io.InputStream inputStream = httpRes.getEntity()
.getContent();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inputStream.close();
strServerResponse = sb.toString();
Log.e("Server Response", "" + strServerResponse.toString());
if (strServerResponse != null) {
try {
JSONArray arr = new JSONArray(strServerResponse);
for (int k = 0; k < arr.length(); k++) {
JSONObject jsonObj1 = arr.getJSONObject(k);
pojo = new Pojo();
date = jsonObj1.optString("login_date");
inTime = jsonObj1.optString("login_time");
outTime = jsonObj1.optString("logout_time");
Log.e("login time from server",""+inTime);
Log.e("login out from server",""+outTime);
Log.e("login date from server",""+date);
pojo.setDate(date);
pojo.setLoginTime(inTime);
pojo.setLogoutTime(outTime);
JSONArray subArrayLat = jsonObj1.getJSONArray("lati_long");
for (int i = 0; i < subArrayLat.length(); i++) {
String lat = subArrayLat.getJSONObject(i).getString("Latitude").toString();
String loong = subArrayLat.getJSONObject(i).getString("Longitude").toString();
Log.e("jsonarray lat", "" + lat);
Log.e("jsonarray longg", "" + loong);
pojo.setLat(lat);
pojo.setLong(loong);
}
history.add(pojo);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
这是适配器。
public class HistoryAdapter extends BaseAdapter {
private Context activity;
TextView tv_date;
TextView tv_loginTime;
TextView tv_logoutTime;
TextView tv_details;
Pojo pojo;
ArrayList<Pojo> list;
private ArrayList<Pojo> arraylist = null;
public static LayoutInflater inflater;
private Context context;
public HistoryAdapter(Context context) {
this.context = context;
}
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);
tv_date = (TextView) v.findViewById(R.id.historyDate);
tv_loginTime = (TextView) v.findViewById(R.id.historyLoginTime);
tv_logoutTime = (TextView) v.findViewById(R.id.historyLogoutTime);
tv_details = (TextView) v.findViewById(R.id.historyDetails);
tv_date.setText(pojo.getDate());
tv_loginTime.setText(pojo.getLoginTime());
tv_logoutTime.setText(pojo.getLogoutTime());
final String lat = pojo.getLat().toString();
String longg = pojo.getLong().toString();
Log.e("adapter latitude", "" + lat);
Log.e("adapter longitude", "" + longg);
tv_details.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(context, MapActivity.class);
i.putExtra("lat", "" + lat);
i.putExtra("longg", "+longg");
context.startActivity(i);
}
});
return v;
}
}
这是JSON数据
[
{
"login_time": "10:30:28",
"logout_time": "10:31:47",
"login_date": "2015- 09-30",
"lati_long": [
{
"date_time": "2015:09:30 11:14:53",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:01",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:15",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:14:53",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:01",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:15",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:52",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:17:16",
"Latitude": "21.121776",
"Longitude": "79.047563"
}
]
},
{
"login_time": "10:42:56",
"logout_time": "10:44:41",
"login_date": "2015-09-30",
"lati_long": [
{
"date_time": "2015:09:30 11:14:53",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:01",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:15",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:14:53",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:01",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:15",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:52",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:17:16",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:14:53",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:01",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:15",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:14:53",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:01",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:15",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:52",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:17:16",
"Latitude": "21.121776",
"Longitude": "79.047563"
}
]
}
]
这里有什么不对吗?请帮帮我。
答案 0 :(得分:0)
您已声明您的CustomObject Pojo's
变量是全局变量。将其声明为局部变量
for (int k = 0; k < arr.length(); k++) {
JSONObject jsonObj1 = arr.getJSONObject(k);
Pojo pojo = new Pojo();
date = jsonObj1.optString("login_date");
inTime = jsonObj1.optString("login_time");
outTime = jsonObj1.optString("logout_time");
Log.e("login time from server",""+inTime);
Log.e("login out from server",""+outTime);
Log.e("login date from server",""+date);
pojo.setDate(date);
pojo.setLoginTime(inTime);
pojo.setLogoutTime(outTime);
JSONArray subArrayLat = jsonObj1.getJSONArray("lati_long");
for (int i = 0; i < subArrayLat.length(); i++) {
String lat = subArrayLat.getJSONObject(i).getString("Latitude").toString();
String loong = subArrayLat.getJSONObject(i).getString("Longitude").toString();
Log.e("jsonarray lat", "" + lat);
Log.e("jsonarray longg", "" + loong);
pojo.setLat(lat);
pojo.setLong(loong);
}
history.add(pojo);
}
答案 1 :(得分:0)
pojo = new Pojo();
应该在Pojo pojo = new Pojo();
答案 2 :(得分:0)
首先在此行myAdapter = new HistoryAdapter(HistoryActivity.this, history);
检查历史记录是否有正确的数据(不相同等)。
其次,编辑适配器的getView
功能
替换它:
View v = convertView;
v = inflater.inflate(R.layout.history_item, parent, false);
对此:
View v = convertView;
if(convertView == null) {
v = inflater.inflate(R.layout.history_item, parent, false);
}
<强> UPD:强> 替换这个:
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
对此:
inflater = LayoutInflater.from(a);
请回复结果
答案 3 :(得分:0)
我发现了你的问题。
初始/外部JSONArray
只有 1 JSONObject,如下所示
[
{ // object 1 start
"login_time": "10:30:28",
"logout_time": "10:31:47",
"login_date": "2015- 09-30",
"lati_long": [
{
"date_time": "2015:09:30 11:14:53",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:01",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:15",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:14:53",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:01",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:15",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:15:52",
"Latitude": "21.121776",
"Longitude": "79.047563"
},
{
"date_time": "2015:09:30 11:17:16",
"Latitude": "21.121776",
"Longitude": "79.047563"
}
]
} // object 1 end
]
因此,您的外部循环for (int k = 0; k < arr.length(); k++) {
只会被调用一次,因此您的列表视图中只有 1 项。
此外,您仅查看最新的长期和纬度值的原因如下所示
for (int i = 0; i < subArrayLat.length(); i++) {
String lat = subArrayLat.getJSONObject(i).getString("Latitude").toString();
String loong = subArrayLat.getJSONObject(i).getString("Longitude").toString();
Log.e("jsonarray lat", "" + lat);
Log.e("jsonarray longg", "" + loong);
// These two are called everytime but set the same pojo field
// The result is that at the end of the loop, only the last set value is retained
pojo.setLat(lat);
pojo.setLong(loong);
}
所以你有一个具有最新long和lat值的pojo对象。
按照我认为你想要达到的目标,我会说使用解析服务器respone中内部json数组的&#39; sub&#39; arraylist来填充你的列表视图。
更新 - 更具体的答案
在内部for
循环
if (strServerResponse != null) {
try {
JSONArray arr = new JSONArray(strServerResponse);
for (int k = 0; k < arr.length(); k++) {
JSONObject jsonObj1 = arr.getJSONObject(k);
// Comment these out:
//1 pojo = new Pojo();
date = jsonObj1.optString("login_date");
inTime = jsonObj1.optString("login_time");
outTime = jsonObj1.optString("logout_time");
Log.e("login time from server",""+inTime);
Log.e("login out from server",""+outTime);
Log.e("login date from server",""+date);
//2 pojo.setDate(date);
//3 pojo.setLoginTime(inTime);
//4 pojo.setLogoutTime(outTime);
JSONArray subArrayLat = jsonObj1.getJSONArray("lati_long");
for (int i = 0; i < subArrayLat.length(); i++) {
// And put them here
pojo = new Pojo();
pojo.setDate(date);
pojo.setLoginTime(inTime);
pojo.setLogoutTime(outTime);
String lat = subArrayLat.getJSONObject(i).getString("Latitude").toString();
String loong = subArrayLat.getJSONObject(i).getString("Longitude").toString();
Log.e("jsonarray lat", "" + lat);
Log.e("jsonarray longg", "" + loong);
pojo.setLat(lat);
pojo.setLong(loong);
// Same with this too
history.add(pojo);
}
// history.add(pojo);
}
}
答案 4 :(得分:0)
在您的案例中应该有一些变化
首先 [Pojo Class]
更改Pojo类定义以在列表中存储纬度和经度
class Pojo{
List<String> lat;
List<String> longi;
private List<String> getLat(){
return this.lat;
}
private List<String> getLongi(){
return this.longi;
}
private void setLat(List<String> lat){
this.lat = lat;
}
private void setLongi(List<String> longi){
this.longi = longi;
}
第二 [对于循环逻辑]
List<String> lati = new ArrayList<String>();
List<String> longi = new ArrayList<String>();
for (int i = 0; i < subArrayLat.length(); i++) {
String lat = subArrayLat.getJSONObject(i).getString("Latitude").toString();
String loong = subArrayLat.getJSONObject(i).getString("Longitude").toString();
lati.add(lat);
longi.add(loong);
}
pojo.setLat(lati);//adding latitude list
pojo.setLongi(longi); //adding longitude list
第三次 [在getview内部]
按以下方式访问这些列表
List<String> lat = pojo.getLat();
List<String> longg = pojo.getLongi();
希望它有所帮助!