Android: 无法将 jsonobject 显示到 listView
Android: failed to Display jsonobject into a listView
我正在使用 android studio 开发一个 android 应用程序,该应用程序正在使用 restful 网络服务,我无法将结果显示到我的 listView 中,但是当我尝试我总是得到一个列表视图,其中包含我认为我得到这种格式的地址:
com.example.projetmonitoringapplication.listez$Dabl@d593975
请注意,结果返回时我有 4 个元素,在我的日志中我可以看到我得到了对象,我什至可以从 ArrayList 中得到一个元素。
我认为问题出在适配器上,或者我不知道,谁能告诉我该怎么做。
我尝试了两件事,一个 returns 结果的 arrayAdapter 和一个 returns 空列表视图的普通适配器。
你可以在我的代码中看到
这是 Activity ,我尝试了很多东西,甚至是评论中的代码:
package com.example.projetmonitoringapplication;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.methods.HttpGet;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
import cz.msebera.android.httpclient.util.EntityUtils;
/**
* Created by Emel on 24/04/2017.
*/
public class listez extends AppCompatActivity {
ListView myListView; // BaseAdapter2 adapter;
ArrayList<Dabl> arrayOfWebData = new ArrayList<Dabl>();
//this is our result object
class Dabl {
int id ;
String libdab;
String etat;
String des ;
String zone ;
public Dabl() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLibdab() {
return libdab;
}
public void setLibdab(String libdab) {
this.libdab = libdab;
}
public String getEtat() {
return etat;
}
public void setEtat(String etat) {
this.etat = etat;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public String getZone() {
return zone;
}
public void setZone(String zone) {
this.zone = zone;
}
public Dabl(int id, String libdab, String etat, String des, String zone) {
this.id = id;
this.libdab = libdab;
this.etat = etat;
this.des = des;
this.zone = zone;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listezonelayout);
// list = (ListView) findViewById(R.id.lstdab);
new TheTask().execute();
/* ListView myListView = (ListView)findViewById(R.id.lstdab);
Dabl d= new Dabl(3,"gg","hh","uu","xx");
arrayOfWebData.add(d);
ArrayAdapter<Dabl> arrayAdapter = new ArrayAdapter<Dabl>(listez.this, android.R.layout.simple_list_item_1,arrayOfWebData );
myListView.setAdapter(arrayAdapter);*/
//String s =myListView.getItemAtPosition(1).toString();
//Log.e("element ", s);
}
adapter aa ;
class TheTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String str = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(
"http://10.0.2.2:8180/ProjetMonitoring/RestApp/DA/RechercherDABzTunis");
HttpResponse response = httpclient.execute(httppost);
str = EntityUtils.toString(response.getEntity());
Log.e("test", "----" + str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
String response = result.toString();
try {
//JSONArray jArray = new JSONArray(result);
// Log.i("result ",result.toString()+" res1");
JSONArray jArray = new JSONArray(response);
for (int i = 0, count = jArray.length(); i < count; i++) {
String string = jArray.getString(i);
//get our object, this is one person's worth of data
// JSONObject json_data=new JSONObject(result.toString().substring(result.indexOf("{"), result.toString().lastIndexOf("}") + 1));
JSONObject json_data = new JSONObject(string);
// JSONObject json_data = jArray.getJSONObject(i);
Log.i("json data ",json_data.toString()+" jjj");
int id = json_data.getInt("id_DAB");
String libdab = json_data.getString("Libelle_DAB");
String etat = json_data.getString("etat");
String des= json_data.getString("description");
String zone= json_data.getString("zone");
Log.d("Success", "id: " + id + "\nlibdab: " + libdab + "\netat: " + etat +
"\ndes: " + des + "\nzone: " + zone);
Log.i("result2 ", String.valueOf(id));
// Dabl resultRow = new Dabl(id,libdab,etat,des,zone);
Dabl resultRow = new Dabl(id,libdab,etat,des,zone);
resultRow.setId( json_data.getInt("id_DAB"));
resultRow.setLibdab( json_data.getString("Libelle_DAB"));
resultRow.setEtat( json_data.getString("etat"));
resultRow.setDes( json_data.getString("description"));
resultRow.setZone(json_data.getString("zone"));
arrayOfWebData.add(resultRow);
Log.i("zone",arrayOfWebData.get(0).getZone()+" get");
}
ListView myListView = (ListView)findViewById(R.id.lstdab);
aa=new adapter();
myListView.setAdapter(aa);
//String s=myListView.getItemAtPosition(1).toString();
// Log.i("ITEM",s);
// lis( arrayOfWebData);
}
catch (JSONException e) {
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
e.printStackTrace();
}
}
/*public void lis( ArrayList<Dabl> arrayOfWebData){
ListView myListView = (ListView)findViewById(R.id.lstdab);
ArrayAdapter<Dabl> arrayAdapter = new ArrayAdapter<Dabl>(listez.this, android.R.layout.simple_list_item_1, arrayOfWebData);
myListView.setAdapter(arrayAdapter);}*/
}
class adapter extends ArrayAdapter<Dabl> {
adapter() {
super(listez.this, android.R.layout.simple_list_item_1, arrayOfWebData);
}
public View getView(int position, View convertView,
ViewGroup parent) {
ViewHolder holder;
if (convertView==null) {
LayoutInflater inflater=getLayoutInflater();
convertView=inflater.inflate(R.layout.listdabzitem, null);
holder=new listez.ViewHolder(convertView);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.populateFrom(arrayOfWebData.get(position));
return(convertView);
}
}
class ViewHolder {
public TextView idd;
public TextView lib;
public TextView et;
public TextView des;
public TextView zone;
ViewHolder(View row) {
idd=(TextView)row.findViewById(R.id.id);
lib=(TextView)row.findViewById(R.id.libdab);
et=(TextView)row.findViewById(R.id.tet);
des=(TextView)row.findViewById(R.id.libe);
zone=(TextView)row.findViewById(R.id.zone);
}
void populateFrom(Dabl r) {
idd.setText(r.getId());
lib.setText(r.getLibdab());
et.setText(r.getEtat());
des.setText(r.getDes());
zone.setText(r.getZone());
}
}
}
我有两个布局,一个包含列表视图,另一个包含列表视图的元素:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<TextView
android:id="@+id/id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#43bd00"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/libdab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#43bd00"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#43bd00"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/libe"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textColor="#acacac" />
<TextView
android:id="@+id/zone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" />
</LinearLayout>
和
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="La liste des DAB" />
<ListView
android:id="@+id/lstdab"
style="@style/Widget.AppCompat.ListView.DropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" />
</LinearLayout>
这是我的日志,你可以看到我从我的 WS 中获取对象:
04-26 17:06:11.998 5274-5274/? I/art: Not late-enabling -Xcheck:jni (already on)
04-26 17:06:11.998 5274-5274/? W/art: Unexpected CPU variant for X86 using defaults: x86
04-26 17:06:12.118 5274-5274/com.example.projetmonitoringapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.projetmonitoringapplication-2/lib/x86
04-26 17:06:12.171 5274-5274/com.example.projetmonitoringapplication W/gralloc_ranchu: Gralloc pipe failed
[ 04-26 17:06:12.172 5274: 5274 D/ ]
HostConnection::get() New Host Connection established 0xa594af80, tid 5274
[ 04-26 17:06:12.256 5274: 5291 D/ ]
HostConnection::get() New Host Connection established 0xa28ff580, tid 5291
04-26 17:06:12.266 5274-5291/com.example.projetmonitoringapplication I/OpenGLRenderer: Initialized EGL, version 1.4
04-26 17:06:12.266 5274-5291/com.example.projetmonitoringapplication D/OpenGLRenderer: Swap behavior 1
04-26 17:06:12.267 5274-5291/com.example.projetmonitoringapplication W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
04-26 17:06:12.267 5274-5291/com.example.projetmonitoringapplication D/OpenGLRenderer: Swap behavior 0
04-26 17:06:15.290 5274-5274/com.example.projetmonitoringapplication W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-26 17:06:15.395 5274-5344/com.example.projetmonitoringapplication D/NetworkSecurityConfig: No Network Security Config specified, using platform default
04-26 17:06:15.876 5274-5291/com.example.projetmonitoringapplication D/OpenGLRenderer: endAllActiveAnimators on 0xa28f8580 (RippleDrawable) with handle 0x968610b0
04-26 17:06:16.019 5274-5344/com.example.projetmonitoringapplication E/test: ----["{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":22,\"etat\":\"En marche\",\"Libelle_DAB\":\"\"}","{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":26,\"etat\":\"En marche\",\"Libelle_DAB\":\"ooo\"}","{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":27,\"etat\":\"En marche\",\"Libelle_DAB\":\"\"}","{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":6660,\"etat\":\"En marche\",\"Libelle_DAB\":\"yyy\"}"]
04-26 17:06:16.022 5274-5274/com.example.projetmonitoringapplication I/json data: {"zone":"Tunis","description":"le DAB fonctionne correctement.","id_DAB":22,"etat":"En marche","Libelle_DAB":""} jjj
04-26 17:06:16.022 5274-5274/com.example.projetmonitoringapplication D/Success: id: 22
libdab:
etat: En marche
des: le DAB fonctionne correctement.
zone: Tunis
04-26 17:06:16.022 5274-5274/com.example.projetmonitoringapplication I/result2: 22
04-26 17:06:16.022 5274-5274/com.example.projetmonitoringapplication I/zone: Tunis get
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/json data: {"zone":"Tunis","description":"le DAB fonctionne correctement.","id_DAB":26,"etat":"En marche","Libelle_DAB":"ooo"} jjj
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication D/Success: id: 26
libdab: ooo
etat: En marche
des: le DAB fonctionne correctement.
zone: Tunis
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/result2: 26
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/zone: Tunis get
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/json data: {"zone":"Tunis","description":"le DAB fonctionne correctement.","id_DAB":27,"etat":"En marche","Libelle_DAB":""} jjj
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication D/Success: id: 27
libdab:
etat: En marche
des: le DAB fonctionne correctement.
zone: Tunis
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/result2: 27
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/zone: Tunis get
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/json data: {"zone":"Tunis","description":"le DAB fonctionne correctement.","id_DAB":6660,"etat":"En marche","Libelle_DAB":"yyy"} jjj
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication D/Success: id: 6660
libdab: yyy
etat: En marche
des: le DAB fonctionne correctement.
zone: Tunis
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/result2: 6660
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/zone: Tunis get
activity 代码包含许多方法,不用担心我刚刚尝试了很多东西,但所有这些代码 sheet 都没有用。
无法在列表视图中获取数据的原因是您试图将 ArrayList 分配给默认数组适配器而不是您的适配器。
此外,将适配器和 asyncTask 放在 activity.It 中并不是标准方法,最好为适配器和异步任务创建单独的 java 文件。
让我们来写代码。
在适配器中,始终建议传递上下文、布局和 ArrayList 以提高可读性。
在适配器中
放置此代码
Context context;
ArrayList<listez.Dabl> list;
adapter(Context context,int resId,ArrayList<listez.Dabl> list) {
super(context, resId, list);
this.context=context;
this.list=list;
}
而不是
adapter() {
super(listez.this, android.R.layout.simple_list_item_1, arrayOfWebData);
}
并且也在 适配器 class populateFrom 函数中
您正试图在文本视图中显示整数。它可能会引发错误。
放置这个
idd.setText(String.valueOf(r.getId()));
而不是
idd.setText(r.getId());
在AsyncTask中onPost执行函数
在ArrayList中添加数据后将此代码放在最后
list( ArrayList<Dabl> arrayOfWebData);
然后将此函数插入 listez class
public void list( ArrayList<Dabl> arrayOfWebData){
ListView myListView = (ListView)findViewById(R.id.lstdab);
adapter arrayAdapter = new adapter(MainActivity.this, android.R.layout.simple_list_item_1,arrayOfWebData );
myListView.setAdapter(arrayAdapter);
}
好吧,这就是解决方案!希望对某人有所帮助
package com.example.projetmonitoringapplication;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.methods.HttpGet;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
import cz.msebera.android.httpclient.util.EntityUtils;
/**
* Created by Emel on 24/04/2017.
*/
public class listez extends AppCompatActivity {
ListView myListView; // BaseAdapter2 adapter;
ArrayList<Dabl> arrayOfWebData = new ArrayList<Dabl>();
//this is our result object
class Dabl {
int id ;
String libdab;
String etat;
String des ;
String zone ;
public Dabl() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLibdab() {
return libdab;
}
public void setLibdab(String libdab) {
this.libdab = libdab;
}
public String getEtat() {
return etat;
}
public void setEtat(String etat) {
this.etat = etat;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public String getZone() {
return zone;
}
public void setZone(String zone) {
this.zone = zone;
}
public Dabl(int id, String libdab, String etat, String des, String zone) {
this.id = id;
this.libdab = libdab;
this.etat = etat;
this.des = des;
this.zone = zone;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listezonelayout);
// list = (ListView) findViewById(R.id.lstdab);
new TheTask().execute();
/* ListView myListView = (ListView)findViewById(R.id.lstdab);
Dabl d= new Dabl(3,"gg","hh","uu","xx");
arrayOfWebData.add(d);
ArrayAdapter<Dabl> arrayAdapter = new ArrayAdapter<Dabl>(listez.this, android.R.layout.simple_list_item_1,arrayOfWebData );
myListView.setAdapter(arrayAdapter);*/
//String s =myListView.getItemAtPosition(1).toString();
//Log.e("element ", s);
}
adapter aa ;
class TheTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String str = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(
"http://10.0.2.2:8180/ProjetMonitoring/RestApp/DA/RechercherDABzTunis");
HttpResponse response = httpclient.execute(httppost);
str = EntityUtils.toString(response.getEntity());
Log.e("test", "----" + str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
String response = result.toString();
try {
//JSONArray jArray = new JSONArray(result);
// Log.i("result ",result.toString()+" res1");
JSONArray jArray = new JSONArray(response);
for (int i = 0, count = jArray.length(); i < count; i++) {
String string = jArray.getString(i);
//get our object, this is one person's worth of data
// JSONObject json_data=new JSONObject(result.toString().substring(result.indexOf("{"), result.toString().lastIndexOf("}") + 1));
JSONObject json_data = new JSONObject(string);
// JSONObject json_data = jArray.getJSONObject(i);
Log.i("json data ",json_data.toString()+" jjj");
int id = json_data.getInt("id_DAB");
String libdab = json_data.getString("Libelle_DAB");
String etat = json_data.getString("etat");
String des= json_data.getString("description");
String zone= json_data.getString("zone");
Log.d("Success", "id: " + id + "\nlibdab: " + libdab + "\netat: " + etat +
"\ndes: " + des + "\nzone: " + zone);
Log.i("result2 ", String.valueOf(id));
// Dabl resultRow = new Dabl(id,libdab,etat,des,zone);
Dabl resultRow = new Dabl(id,libdab,etat,des,zone);
resultRow.setId( json_data.getInt("id_DAB"));
resultRow.setLibdab( json_data.getString("Libelle_DAB"));
resultRow.setEtat( json_data.getString("etat"));
resultRow.setDes( json_data.getString("description"));
resultRow.setZone(json_data.getString("zone"));
arrayOfWebData.add(resultRow);
Log.i("zone",arrayOfWebData.get(0).getZone()+" get");
}
ListView myListView = (ListView)findViewById(R.id.lstdab);
aa=new adapter();
myListView.setAdapter(aa);
//String s=myListView.getItemAtPosition(1).toString();
// Log.i("ITEM",s);
// lis( arrayOfWebData);
}
catch (JSONException e) {
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
e.printStackTrace();
}
}
/*public void lis( ArrayList<Dabl> arrayOfWebData){
ListView myListView = (ListView)findViewById(R.id.lstdab);
ArrayAdapter<Dabl> arrayAdapter = new ArrayAdapter<Dabl>(listez.this, android.R.layout.simple_list_item_1, arrayOfWebData);
myListView.setAdapter(arrayAdapter);}*/
}
class adapter extends ArrayAdapter<Dabl> {
adapter() {
super(listez.this, android.R.layout.simple_list_item_1, arrayOfWebData);
}
public View getView(int position, View convertView,
ViewGroup parent) {
ViewHolder holder;
if (convertView==null) {
LayoutInflater inflater=getLayoutInflater();
convertView=inflater.inflate(R.layout.listdabzitem, null);
holder=new listez.ViewHolder(convertView);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.populateFrom(arrayOfWebData.get(position));
return(convertView);
}
}
class ViewHolder {
public TextView idd;
public TextView lib;
public TextView et;
public TextView des;
public TextView zone;
ViewHolder(View row) {
idd=(TextView)row.findViewById(R.id.id);
lib=(TextView)row.findViewById(R.id.libdab);
et=(TextView)row.findViewById(R.id.tet);
des=(TextView)row.findViewById(R.id.libe);
zone=(TextView)row.findViewById(R.id.zone);
}
void populateFrom(Dabl r) {
idd.setText(String.valueOf(r.getId()));
lib.setText(r.getLibdab());
et.setText(r.getEtat());
des.setText(r.getDes());
zone.setText(r.getZone());
}
}
}
我正在使用 android studio 开发一个 android 应用程序,该应用程序正在使用 restful 网络服务,我无法将结果显示到我的 listView 中,但是当我尝试我总是得到一个列表视图,其中包含我认为我得到这种格式的地址:
com.example.projetmonitoringapplication.listez$Dabl@d593975
请注意,结果返回时我有 4 个元素,在我的日志中我可以看到我得到了对象,我什至可以从 ArrayList 中得到一个元素。 我认为问题出在适配器上,或者我不知道,谁能告诉我该怎么做。
我尝试了两件事,一个 returns 结果的 arrayAdapter 和一个 returns 空列表视图的普通适配器。 你可以在我的代码中看到
这是 Activity ,我尝试了很多东西,甚至是评论中的代码:
package com.example.projetmonitoringapplication;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.methods.HttpGet;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
import cz.msebera.android.httpclient.util.EntityUtils;
/**
* Created by Emel on 24/04/2017.
*/
public class listez extends AppCompatActivity {
ListView myListView; // BaseAdapter2 adapter;
ArrayList<Dabl> arrayOfWebData = new ArrayList<Dabl>();
//this is our result object
class Dabl {
int id ;
String libdab;
String etat;
String des ;
String zone ;
public Dabl() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLibdab() {
return libdab;
}
public void setLibdab(String libdab) {
this.libdab = libdab;
}
public String getEtat() {
return etat;
}
public void setEtat(String etat) {
this.etat = etat;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public String getZone() {
return zone;
}
public void setZone(String zone) {
this.zone = zone;
}
public Dabl(int id, String libdab, String etat, String des, String zone) {
this.id = id;
this.libdab = libdab;
this.etat = etat;
this.des = des;
this.zone = zone;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listezonelayout);
// list = (ListView) findViewById(R.id.lstdab);
new TheTask().execute();
/* ListView myListView = (ListView)findViewById(R.id.lstdab);
Dabl d= new Dabl(3,"gg","hh","uu","xx");
arrayOfWebData.add(d);
ArrayAdapter<Dabl> arrayAdapter = new ArrayAdapter<Dabl>(listez.this, android.R.layout.simple_list_item_1,arrayOfWebData );
myListView.setAdapter(arrayAdapter);*/
//String s =myListView.getItemAtPosition(1).toString();
//Log.e("element ", s);
}
adapter aa ;
class TheTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String str = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(
"http://10.0.2.2:8180/ProjetMonitoring/RestApp/DA/RechercherDABzTunis");
HttpResponse response = httpclient.execute(httppost);
str = EntityUtils.toString(response.getEntity());
Log.e("test", "----" + str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
String response = result.toString();
try {
//JSONArray jArray = new JSONArray(result);
// Log.i("result ",result.toString()+" res1");
JSONArray jArray = new JSONArray(response);
for (int i = 0, count = jArray.length(); i < count; i++) {
String string = jArray.getString(i);
//get our object, this is one person's worth of data
// JSONObject json_data=new JSONObject(result.toString().substring(result.indexOf("{"), result.toString().lastIndexOf("}") + 1));
JSONObject json_data = new JSONObject(string);
// JSONObject json_data = jArray.getJSONObject(i);
Log.i("json data ",json_data.toString()+" jjj");
int id = json_data.getInt("id_DAB");
String libdab = json_data.getString("Libelle_DAB");
String etat = json_data.getString("etat");
String des= json_data.getString("description");
String zone= json_data.getString("zone");
Log.d("Success", "id: " + id + "\nlibdab: " + libdab + "\netat: " + etat +
"\ndes: " + des + "\nzone: " + zone);
Log.i("result2 ", String.valueOf(id));
// Dabl resultRow = new Dabl(id,libdab,etat,des,zone);
Dabl resultRow = new Dabl(id,libdab,etat,des,zone);
resultRow.setId( json_data.getInt("id_DAB"));
resultRow.setLibdab( json_data.getString("Libelle_DAB"));
resultRow.setEtat( json_data.getString("etat"));
resultRow.setDes( json_data.getString("description"));
resultRow.setZone(json_data.getString("zone"));
arrayOfWebData.add(resultRow);
Log.i("zone",arrayOfWebData.get(0).getZone()+" get");
}
ListView myListView = (ListView)findViewById(R.id.lstdab);
aa=new adapter();
myListView.setAdapter(aa);
//String s=myListView.getItemAtPosition(1).toString();
// Log.i("ITEM",s);
// lis( arrayOfWebData);
}
catch (JSONException e) {
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
e.printStackTrace();
}
}
/*public void lis( ArrayList<Dabl> arrayOfWebData){
ListView myListView = (ListView)findViewById(R.id.lstdab);
ArrayAdapter<Dabl> arrayAdapter = new ArrayAdapter<Dabl>(listez.this, android.R.layout.simple_list_item_1, arrayOfWebData);
myListView.setAdapter(arrayAdapter);}*/
}
class adapter extends ArrayAdapter<Dabl> {
adapter() {
super(listez.this, android.R.layout.simple_list_item_1, arrayOfWebData);
}
public View getView(int position, View convertView,
ViewGroup parent) {
ViewHolder holder;
if (convertView==null) {
LayoutInflater inflater=getLayoutInflater();
convertView=inflater.inflate(R.layout.listdabzitem, null);
holder=new listez.ViewHolder(convertView);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.populateFrom(arrayOfWebData.get(position));
return(convertView);
}
}
class ViewHolder {
public TextView idd;
public TextView lib;
public TextView et;
public TextView des;
public TextView zone;
ViewHolder(View row) {
idd=(TextView)row.findViewById(R.id.id);
lib=(TextView)row.findViewById(R.id.libdab);
et=(TextView)row.findViewById(R.id.tet);
des=(TextView)row.findViewById(R.id.libe);
zone=(TextView)row.findViewById(R.id.zone);
}
void populateFrom(Dabl r) {
idd.setText(r.getId());
lib.setText(r.getLibdab());
et.setText(r.getEtat());
des.setText(r.getDes());
zone.setText(r.getZone());
}
}
}
我有两个布局,一个包含列表视图,另一个包含列表视图的元素:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<TextView
android:id="@+id/id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#43bd00"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/libdab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#43bd00"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#43bd00"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/libe"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textColor="#acacac" />
<TextView
android:id="@+id/zone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" />
</LinearLayout>
和
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="La liste des DAB" />
<ListView
android:id="@+id/lstdab"
style="@style/Widget.AppCompat.ListView.DropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" />
</LinearLayout>
这是我的日志,你可以看到我从我的 WS 中获取对象:
04-26 17:06:11.998 5274-5274/? I/art: Not late-enabling -Xcheck:jni (already on)
04-26 17:06:11.998 5274-5274/? W/art: Unexpected CPU variant for X86 using defaults: x86
04-26 17:06:12.118 5274-5274/com.example.projetmonitoringapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.projetmonitoringapplication-2/lib/x86
04-26 17:06:12.171 5274-5274/com.example.projetmonitoringapplication W/gralloc_ranchu: Gralloc pipe failed
[ 04-26 17:06:12.172 5274: 5274 D/ ]
HostConnection::get() New Host Connection established 0xa594af80, tid 5274
[ 04-26 17:06:12.256 5274: 5291 D/ ]
HostConnection::get() New Host Connection established 0xa28ff580, tid 5291
04-26 17:06:12.266 5274-5291/com.example.projetmonitoringapplication I/OpenGLRenderer: Initialized EGL, version 1.4
04-26 17:06:12.266 5274-5291/com.example.projetmonitoringapplication D/OpenGLRenderer: Swap behavior 1
04-26 17:06:12.267 5274-5291/com.example.projetmonitoringapplication W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
04-26 17:06:12.267 5274-5291/com.example.projetmonitoringapplication D/OpenGLRenderer: Swap behavior 0
04-26 17:06:15.290 5274-5274/com.example.projetmonitoringapplication W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-26 17:06:15.395 5274-5344/com.example.projetmonitoringapplication D/NetworkSecurityConfig: No Network Security Config specified, using platform default
04-26 17:06:15.876 5274-5291/com.example.projetmonitoringapplication D/OpenGLRenderer: endAllActiveAnimators on 0xa28f8580 (RippleDrawable) with handle 0x968610b0
04-26 17:06:16.019 5274-5344/com.example.projetmonitoringapplication E/test: ----["{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":22,\"etat\":\"En marche\",\"Libelle_DAB\":\"\"}","{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":26,\"etat\":\"En marche\",\"Libelle_DAB\":\"ooo\"}","{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":27,\"etat\":\"En marche\",\"Libelle_DAB\":\"\"}","{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":6660,\"etat\":\"En marche\",\"Libelle_DAB\":\"yyy\"}"]
04-26 17:06:16.022 5274-5274/com.example.projetmonitoringapplication I/json data: {"zone":"Tunis","description":"le DAB fonctionne correctement.","id_DAB":22,"etat":"En marche","Libelle_DAB":""} jjj
04-26 17:06:16.022 5274-5274/com.example.projetmonitoringapplication D/Success: id: 22
libdab:
etat: En marche
des: le DAB fonctionne correctement.
zone: Tunis
04-26 17:06:16.022 5274-5274/com.example.projetmonitoringapplication I/result2: 22
04-26 17:06:16.022 5274-5274/com.example.projetmonitoringapplication I/zone: Tunis get
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/json data: {"zone":"Tunis","description":"le DAB fonctionne correctement.","id_DAB":26,"etat":"En marche","Libelle_DAB":"ooo"} jjj
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication D/Success: id: 26
libdab: ooo
etat: En marche
des: le DAB fonctionne correctement.
zone: Tunis
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/result2: 26
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/zone: Tunis get
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/json data: {"zone":"Tunis","description":"le DAB fonctionne correctement.","id_DAB":27,"etat":"En marche","Libelle_DAB":""} jjj
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication D/Success: id: 27
libdab:
etat: En marche
des: le DAB fonctionne correctement.
zone: Tunis
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/result2: 27
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/zone: Tunis get
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/json data: {"zone":"Tunis","description":"le DAB fonctionne correctement.","id_DAB":6660,"etat":"En marche","Libelle_DAB":"yyy"} jjj
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication D/Success: id: 6660
libdab: yyy
etat: En marche
des: le DAB fonctionne correctement.
zone: Tunis
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/result2: 6660
04-26 17:06:16.023 5274-5274/com.example.projetmonitoringapplication I/zone: Tunis get
activity 代码包含许多方法,不用担心我刚刚尝试了很多东西,但所有这些代码 sheet 都没有用。
无法在列表视图中获取数据的原因是您试图将 ArrayList 分配给默认数组适配器而不是您的适配器。
此外,将适配器和 asyncTask 放在 activity.It 中并不是标准方法,最好为适配器和异步任务创建单独的 java 文件。
让我们来写代码。
在适配器中,始终建议传递上下文、布局和 ArrayList 以提高可读性。
在适配器中
放置此代码
Context context;
ArrayList<listez.Dabl> list;
adapter(Context context,int resId,ArrayList<listez.Dabl> list) {
super(context, resId, list);
this.context=context;
this.list=list;
}
而不是
adapter() {
super(listez.this, android.R.layout.simple_list_item_1, arrayOfWebData);
}
并且也在 适配器 class populateFrom 函数中
您正试图在文本视图中显示整数。它可能会引发错误。
放置这个
idd.setText(String.valueOf(r.getId()));
而不是
idd.setText(r.getId());
在AsyncTask中onPost执行函数
在ArrayList中添加数据后将此代码放在最后
list( ArrayList<Dabl> arrayOfWebData);
然后将此函数插入 listez class
public void list( ArrayList<Dabl> arrayOfWebData){
ListView myListView = (ListView)findViewById(R.id.lstdab);
adapter arrayAdapter = new adapter(MainActivity.this, android.R.layout.simple_list_item_1,arrayOfWebData );
myListView.setAdapter(arrayAdapter);
}
好吧,这就是解决方案!希望对某人有所帮助
package com.example.projetmonitoringapplication;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.methods.HttpGet;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
import cz.msebera.android.httpclient.util.EntityUtils;
/**
* Created by Emel on 24/04/2017.
*/
public class listez extends AppCompatActivity {
ListView myListView; // BaseAdapter2 adapter;
ArrayList<Dabl> arrayOfWebData = new ArrayList<Dabl>();
//this is our result object
class Dabl {
int id ;
String libdab;
String etat;
String des ;
String zone ;
public Dabl() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLibdab() {
return libdab;
}
public void setLibdab(String libdab) {
this.libdab = libdab;
}
public String getEtat() {
return etat;
}
public void setEtat(String etat) {
this.etat = etat;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public String getZone() {
return zone;
}
public void setZone(String zone) {
this.zone = zone;
}
public Dabl(int id, String libdab, String etat, String des, String zone) {
this.id = id;
this.libdab = libdab;
this.etat = etat;
this.des = des;
this.zone = zone;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listezonelayout);
// list = (ListView) findViewById(R.id.lstdab);
new TheTask().execute();
/* ListView myListView = (ListView)findViewById(R.id.lstdab);
Dabl d= new Dabl(3,"gg","hh","uu","xx");
arrayOfWebData.add(d);
ArrayAdapter<Dabl> arrayAdapter = new ArrayAdapter<Dabl>(listez.this, android.R.layout.simple_list_item_1,arrayOfWebData );
myListView.setAdapter(arrayAdapter);*/
//String s =myListView.getItemAtPosition(1).toString();
//Log.e("element ", s);
}
adapter aa ;
class TheTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String str = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(
"http://10.0.2.2:8180/ProjetMonitoring/RestApp/DA/RechercherDABzTunis");
HttpResponse response = httpclient.execute(httppost);
str = EntityUtils.toString(response.getEntity());
Log.e("test", "----" + str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
String response = result.toString();
try {
//JSONArray jArray = new JSONArray(result);
// Log.i("result ",result.toString()+" res1");
JSONArray jArray = new JSONArray(response);
for (int i = 0, count = jArray.length(); i < count; i++) {
String string = jArray.getString(i);
//get our object, this is one person's worth of data
// JSONObject json_data=new JSONObject(result.toString().substring(result.indexOf("{"), result.toString().lastIndexOf("}") + 1));
JSONObject json_data = new JSONObject(string);
// JSONObject json_data = jArray.getJSONObject(i);
Log.i("json data ",json_data.toString()+" jjj");
int id = json_data.getInt("id_DAB");
String libdab = json_data.getString("Libelle_DAB");
String etat = json_data.getString("etat");
String des= json_data.getString("description");
String zone= json_data.getString("zone");
Log.d("Success", "id: " + id + "\nlibdab: " + libdab + "\netat: " + etat +
"\ndes: " + des + "\nzone: " + zone);
Log.i("result2 ", String.valueOf(id));
// Dabl resultRow = new Dabl(id,libdab,etat,des,zone);
Dabl resultRow = new Dabl(id,libdab,etat,des,zone);
resultRow.setId( json_data.getInt("id_DAB"));
resultRow.setLibdab( json_data.getString("Libelle_DAB"));
resultRow.setEtat( json_data.getString("etat"));
resultRow.setDes( json_data.getString("description"));
resultRow.setZone(json_data.getString("zone"));
arrayOfWebData.add(resultRow);
Log.i("zone",arrayOfWebData.get(0).getZone()+" get");
}
ListView myListView = (ListView)findViewById(R.id.lstdab);
aa=new adapter();
myListView.setAdapter(aa);
//String s=myListView.getItemAtPosition(1).toString();
// Log.i("ITEM",s);
// lis( arrayOfWebData);
}
catch (JSONException e) {
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
e.printStackTrace();
}
}
/*public void lis( ArrayList<Dabl> arrayOfWebData){
ListView myListView = (ListView)findViewById(R.id.lstdab);
ArrayAdapter<Dabl> arrayAdapter = new ArrayAdapter<Dabl>(listez.this, android.R.layout.simple_list_item_1, arrayOfWebData);
myListView.setAdapter(arrayAdapter);}*/
}
class adapter extends ArrayAdapter<Dabl> {
adapter() {
super(listez.this, android.R.layout.simple_list_item_1, arrayOfWebData);
}
public View getView(int position, View convertView,
ViewGroup parent) {
ViewHolder holder;
if (convertView==null) {
LayoutInflater inflater=getLayoutInflater();
convertView=inflater.inflate(R.layout.listdabzitem, null);
holder=new listez.ViewHolder(convertView);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.populateFrom(arrayOfWebData.get(position));
return(convertView);
}
}
class ViewHolder {
public TextView idd;
public TextView lib;
public TextView et;
public TextView des;
public TextView zone;
ViewHolder(View row) {
idd=(TextView)row.findViewById(R.id.id);
lib=(TextView)row.findViewById(R.id.libdab);
et=(TextView)row.findViewById(R.id.tet);
des=(TextView)row.findViewById(R.id.libe);
zone=(TextView)row.findViewById(R.id.zone);
}
void populateFrom(Dabl r) {
idd.setText(String.valueOf(r.getId()));
lib.setText(r.getLibdab());
et.setText(r.getEtat());
des.setText(r.getDes());
zone.setText(r.getZone());
}
}
}