android studio에서 android studio로 작업하고 있습니다. 웹 서비스를 사용하고 있습니다. 데이터베이스에서 데이터를 가져와이 데이터를 표시하는 데 문제가 있습니다. (백엔드 및 나는 "getProspectServlet"을 가지고있다 prospecti라는 테이블을 잠재 고객 목록을 얻어 expandble 목록에 잠재 고객에게 보여줄 필요가있다. 여기에 잠재 고객을 데이터베이스에서 가져 와서 목록에 저장하는 클래스의 코드가있다 " 내가 그럴 필요시 liste "ListView에서 데이터베이스의 데이터 표시
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();
}*/
}
// }
}
내가이 목록 객체를 얻을이 목록을 보여주고 싶은하지만 난 목록 (내가 건배와 함께 테스트) 비어 있지 getProspect에서 ListProspect에서 빈 목록을 가지고 계속하지만, 여기에 목록보기
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);
}
}
코드입니다) Y는 내가 전망을 얻을도 ListProspect의 정적 목록을 사용하여 볼 수있는 난 아무것도 을 얻을 ListProspect의 목록을 얻을뿐만 아니라 나는 또한 getListe을 (사용 비어
내가 어떻게이 문제를 해결할 수있는 "getProspect"내가 건배에서 내 잠재 고객을 보여줄 수 있지만 내가 "ListeProspect"에 갈 때 나는 빈 목록을 가지고있다.
팁 : AsyncTasks를 작성하고 Volley의 JsonArrayRequest를 사용하는 작업을 자제하십시오. –
또는 정적 변수를 사용하지 말고 asynctask의 콜백을 사용하십시오 (http://stackoverflow.com/questions/9963691/android-asynctask-sending- callbacks-to-ui) –