Android 在列表视图中显示数据库中的数据
我正在和android studio一起进行android项目我正在使用web服务我在从数据库获取数据和显示这些数据时遇到了问题 (为了测试,我正在使用后端,我有“getProspectServlet” 所以我有一个名为prospect的表,我需要得到prospect的列表,并在一个可扩展的列表中显示prospect。这里是从数据库中获取prospect并将其存储在列表“liste”中的类的代码Android 在列表视图中显示数据库中的数据,android,web-services,listview,arraylist,Android,Web Services,Listview,Arraylist,我正在和android studio一起进行android项目我正在使用web服务我在从数据库获取数据和显示这些数据时遇到了问题 (为了测试,我正在使用后端,我有“getProspectServlet” 所以我有一个名为prospect的表,我需要得到prospect的列表,并在一个可扩展的列表中显示prospect。这里是从数据库中获取prospect并将其存储在列表“liste”中的类的代码 导入android.app.Activity; 导入android.content.Context;
导入android.app.Activity;
导入android.content.Context;
导入android.os.AsyncTask;
导入android.util.Log;
导入android.view.view;
导入android.widget.ProgressBar;
导入android.widget.Toast;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.io.UnsupportedEncodingException;
导入java.net.HttpURLConnection;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.util.ArrayList;
导入java.util.List;
/**
*由DKTIQUE于2016年5月19日创建。
*/
公共类getProspect扩展异步任务{
私人语境;
列表列表;
公共列表getListe(){
返回列表;
}
//ProgressBar-pb;
公共getProspect(上下文){
this.context=上下文;
}
/*公共getProspect(上下文,列表列表){
this.context=上下文;
this.liste=liste;
}*/
/*@覆盖
受保护的void onPreExecute(){
pb=(ProgressBar)((活动)上下文);
pb.setVisibility(View.VISIBLE);
}*/
@凌驾
受保护的字符串doInBackground(Void…params){//c un tableau
字符串数据;
StringBuilder结果=新建StringBuilder();
试一试{
//URL=新URL(“http://10.0.2.163:8080/Prospects/GET");
URL=新URL(“http://192.168.43.249:8080/getProspect");
//URL=新URL(“http://10.0.19.196:8080/getProspect");
//URL=新URL(“http://10.0.19.196:8080/Prospects/GET”);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setConnectTimeout(5000);
InputStream=httpURLConnection.getInputStream();
BufferedReader=新的BufferedReader(新的InputStreamReader(即“UTF-8”));
而((data=reader.readLine())!=null){
结果:追加(数据);
}
}捕获(格式错误){
e、 printStackTrace();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
Log.v(“resut”,result.toString());
返回result.toString();
}
@凌驾
受保护的void onPostExecute(字符串s){//afficher le resultat
//名单
liste=newarraylist();
//pb.setVisibility(View.VISIBLE);
//Gson Gson=新的Gson();
//如果(!s.equals(“”){
试一试{
JSONArray JSONArray=新JSONArray;
for(int i=0;i /*为了(int i=0;i检查列表时,列表显示为空的原因是,在后台线程上运行的asyncTask中填充列表,并在主线程上调用asyncTask后立即对其进行检查。因此,简而言之,在尝试显示或打印列表之前,您没有让后台任务完成填充ut.onPostExecute
在主线程上运行,并在doInBackground
完成后运行,因此在onPostExecute
中完成后台任务后,可以执行任何您想做的操作
同样,Cricket是正确的,对这样的事情使用静态变量是不必要的。如果你需要从另一个活动访问列表值,那么就有目的地传递它们。提示:省去编写异步任务的工作,使用Volley的JsonArrayRequestOr,或者试着不使用静态变量,而使用Rob85来完成任务解释,但我不太明白,所以我应该使用这个“p=newgetprospect(this);p.onPostExecute();”我试过了,但没有成功,加上我不能发送一个意图列表我点击一个按钮我进入expandbleList的活动那里我需要我的列表我看不出我如何使用意图在这里你能给我解释更多我如何解决这个问题我不确定我能说得更清楚,你在后台线程上运行一个进程(填充列表)这个过程需要时间。但是在开始这个过程后,您会立即检查主线程(onCreate)上的结果你的后台线程还没有完成,所以你的列表当然是空的。记住onCreate不是一个循环,它只会被调用onceso我怎么知道BackgroundTask何时完成?因为onPostExecute方法被调用,它在主线程上运行,这意味着你可以在后台任务完成后放置所有你想要运行的代码ishedi在我第一次执行后得到你说的,现在列表是空的,因为它还没有填充,但几秒钟后,当我回到活动时,列表填充了谢谢
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();
}*/
}
// }
}
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);
}
}