我正在使用Android工作室安装android项目我正在使用Web服务我在从数据库中获取数据并显示此数据时遇到问题 (测试我使用后端,我有“getProspectServlet” 所以我有一个名为prospecti的表需要获取潜在客户列表并在展开的列表中显示潜在客户,这是从数据库中获取潜在客户并将其存储在“liste”列表中的类的代码
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* Created by DKTIQUE on 19/05/2016.
*/
public class getProspect extends AsyncTask<Void,Void,String> {
private Context context;
List<Prospect> liste;
public List<Prospect> getListe() {
return liste;
}
// ProgressBar pb;
public getProspect(Context context) {
this.context = context;
}
/* public getProspect(Context context, List<Prospect> liste) {
this.context = context;
this.liste = liste;
}*/
/* @Override
protected void onPreExecute() {
pb = (ProgressBar) ((Activity)context).findViewById(R.id.progressBar);
pb.setVisibility(View.VISIBLE);
}*/
@Override
protected String doInBackground(Void... params) { // c un tableau
String data;
StringBuilder result= new StringBuilder();
try {
// URL url = new URL ("http://10.0.2.163:8080/Prospects/GET");
URL url = new URL ("http://192.168.43.249:8080/getProspect");
// URL url = new URL ("http://10.0.19.196:8080/getProspect");
// URL url = new URL ("http:// 10.0.19.196:8080/Prospects/GET");
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(5000);
InputStream is = httpURLConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((data = reader.readLine()) != null) {
result.append(data);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.v("resut", result.toString());
return result.toString();
}
@Override
protected void onPostExecute(String s) {//afficher le resultat
// List<Prospect>
liste = new ArrayList<>();
// pb.setVisibility(View.VISIBLE);
// Gson gson = new Gson();
// if (!s.equals("")) {
try {
JSONArray jsonArray = new JSONArray(s);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Prospect prospect= new Prospect();
prospect.setNom(jsonObject.get("nom").toString());
prospect.setPrenom(jsonObject.get("prenom").toString());
// prospect.setTitle(jsonObject.get("idProspect").toString());
ListeProspect.listee.add(prospect);
liste.add(prospect);
}
} catch (JSONException e) {
e.printStackTrace();
}
if (ListeProspect.listee.size()==0){Toast.makeText(context,"emptyyyyyyyyyyy",Toast.LENGTH_SHORT).show();}
// TextView textView = (Activity)context.findViewbyId();
/* for(int i=0;i<liste.size();i++){
Toast.makeText(context,liste.get(i).getNom(), Toast.LENGTH_SHORT).show();
}*/
}
// }
}
我想得到这个列表对象并在这个列表中显示它们但是我在ListProspect中有一个空列表在getProspect中列表不是空的(我用吐司测试)但是当我尝试在ListProspect中获取列表时一无所获 你可以看到我也使用了ListProspect中的静态列表来获取前景,但它也是空的我也使用了getListe()这里是listView的代码
public class ListeProspect extends AppCompatActivity {
static List<Prospect> listee = new ArrayList();
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_liste_prospect);
//******************************************************
// getProspect p= new getProspect(this).execute();
// getProspect p= new getProspect(this);
new getProspect(this).execute();
//p = new getProspect(this);
// p.execute();
// p.onPostExecute();
if (ListeProspect.listee.size()==0){Toast.makeText(this,"empty",Toast.LENGTH_SHORT).show();}
else{Toast.makeText(this,"not empty",Toast.LENGTH_SHORT).show();
for(int i=0;i<ListeProspect.listee.size();i++){
Toast.makeText(this,ListeProspect. listee.get(i).getPrenom(), Toast.LENGTH_SHORT).show();
}
}
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Prospect 1");
listDataHeader.add("Prospect 2");
listDataHeader.add("Prospect 3");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("Age1");
top250.add("Assuré1");
top250.add("Type de stomie1");
top250.add("Nombre de poche par jour");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("Age2");
nowShowing.add("Assuré");
nowShowing.add("Type de stomie");
nowShowing.add("Nombre de poche par jour");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("Age");
comingSoon.add("Assuré");
comingSoon.add("Type de stomie");
comingSoon.add("Nombre de poche par jour");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), top250); // Header, Child data
listDataChild.put(listDataHeader.get(2), top250); // Header, Child data
// listDataChild.put(listDataHeader.get(1), nowShowing);
// listDataChild.put(listDataHeader.get(2), comingSoon);
}
}
我怎么能解决这个问题就像我在课堂上说的“getProspect”我可以在祝酒词中展示我的前景但是当我去“ListeProspect”时我有空列表
答案 0 :(得分:1)
检查它时列表显示为空的原因是因为您在后台线程上运行的asyncTask中填充它,并在调用主线程上的asyncTask后立即检查它。因此,在您尝试显示或打印出来之前,您无需让后台任务完成填充列表。 onPostExecute
在主线程上运行,并在doInBackground
完成后运行,因此在后台任务完成onPostExecute
后,您可以执行任何操作。
Cricket也是正确的,使用静态变量来做这样的事情是没有必要的。如果您需要从另一个活动访问列表值,则通过意图传递它们。