ListView中显示相同的数据 [英] ListView displaying same data
本文介绍了ListView中显示相同的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个大的JSON阵列。我读从JSON数组中的数据并将其存储在一个列表视图,但列表视图重复相同的数据。
I have a large JSON array. I am reading the data from JSON array and storing it in a list view, But the list view repeats the same data.
即从JSON,而不是显示所有的数据最后的数据。
i.e the last data from the JSON instead of displaying all the data.
下面是我的code。
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"
}
]
}
]
这有什么错吗?请帮我。
Is there anything wrong here? Please help me.
推荐答案
一些变化,应该在工作的情况下
Few changes which should work in your case
第一 [POJO类]
更改POJO类定义存储在列表中的纬度和经度
Change Pojo class definition to store latitude and longitude in list
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;
}
二 [For循环逻辑]
Second[For loop logic]
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]
Third [ Inside getview]
访问这些列表如下方式
List<String> lat = pojo.getLat();
List<String> longg = pojo.getLongi();
希望它帮助!
这篇关于ListView中显示相同的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文