我正在使用 android studio 开发 android 项目 我正在使用网络服务 我在从我的数据库中获取数据并显示这些数据时遇到问题 (测试我正在使用后端并且我有“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 中一直有一个空列表一无所获 如您所见,我也使用了 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”类(class)中所说的那样,我可以 toast 展示我的前景,但是当我去“ListeProspect”时,我的列表是空的
最佳答案
检查时列表显示为空的原因是因为您在后台线程上运行的 asyncTask 中填充它,并在主线程上调用 asyncTask 后立即检查它。因此,简而言之,在尝试显示或打印列表之前,您还没有让后台任务完成填充列表。 onPostExecute
在主线程上运行并在 doInBackground
完成后运行,因此在后台任务完成后执行任何您想在 onPostExecute
中执行的操作>.
另外 Cricket 是正确的,没有必要为这样的事情使用静态变量。如果您需要从另一个 Activity 访问列表值,则有意地传递它们。
关于android - 在 ListView 中显示来自数据库的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37840892/