[英]show results of web service rest in a listview android
我想在帶有圖片的列表視圖中顯示我的Web服務的結果,所以我嘗試創建一個item_veiw:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:maxHeight="80dp"
android:maxWidth="80dp"
android:src="@drawable/enseigne_pharma" />
<TextView
android:id="@+id/item_txtCondition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/item_txtYear"
android:layout_alignParentRight="true"
android:text="Condition shown here"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/item_txtYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/item_icon"
android:layout_alignLeft="@+id/item_txtMake"
android:layout_marginBottom="16dp"
android:text="2000" />
<TextView
android:id="@+id/item_txtMake"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/item_txtCondition"
android:layout_toRightOf="@+id/item_icon"
android:text="Make Shown Here"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
而且我已經准備好只包含一個listView的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ListActivity" >
<ListView
android:id="@+id/listRes"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<Button
android:id="@+id/rtrRech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/listRes"
android:text=" Retour " />
</RelativeLayout>
在我的活動中,我有:
public class ListActivity extends Activity implements OnClickListener{
private ListView lv;
private Button btnRet;
private View itemView;
private String[] phars;
private int index;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Intent intent= getIntent();
Bundle b = intent.getExtras();
lv = (ListView) findViewById(R.id.listRes);
itemView=getLayoutInflater().inflate(R.layout.item_view, null, false);
btnRet = (Button) findViewById(R.id.rtrRech);
btnRet.setOnClickListener(this);
String vi = b.getString("ville");
String se = b.getString("secteur");
StringBuilder reponseHTTP = new StringBuilder();
HttpClient client = new DefaultHttpClient();
Toast.makeText(getApplicationContext(), "Output: " + vi + " - " + se,Toast.LENGTH_LONG).show();
HttpGet httpGet = new HttpGet
("http://10.0.2.2:8080/loou/get/"+vi+"/"+se);
try {
//Toast.makeText(getApplicationContext(), "We are inside try block.. httpget: " + httpGet,Toast.LENGTH_LONG).show();
HttpResponse response = client.execute(httpGet);
//Toast.makeText(getApplicationContext(), "Response " + response.toString(),Toast.LENGTH_LONG).show();
StatusLine statusLine=response.getStatusLine();
//Toast.makeText(getApplicationContext(), "status line" + statusLine,Toast.LENGTH_LONG).show();
int code=statusLine.getStatusCode();
Toast.makeText(getApplicationContext(), "Code" + code,Toast.LENGTH_LONG).show();
if(code==200){
Toast.makeText(getApplicationContext(), "Chargement des donnÈes",Toast.LENGTH_LONG).show();
HttpEntity httpEntity=response.getEntity();
InputStream is=httpEntity.getContent();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String s;
while((s=br.readLine())!=null){
reponseHTTP.append(s);
}
JSONArray jsonArray=new JSONArray(reponseHTTP.toString());
phars=new String[(jsonArray.length()+1)];
index=-1;
phars[++index]="Liste";
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
phars[++index]=jsonObject.getString("nom");
populateListView();
}
}
else{
Toast.makeText(getApplicationContext(), "Err "+code,
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Exception " + e.toString(),
Toast.LENGTH_LONG).show();
}
}
@Override
public void onClick(View v) {
if(v==btnRet){
Intent intent = new Intent(ListActivity.this, RecherchActivity.class);
startActivity(intent);
}
}
private class MyListAdapter extends ArrayAdapter<String> {
public MyListAdapter() {
super(ListActivity.this, R.layout.item_view, phars);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Make sure we have a view to work with (may have been given null)
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
}
// Make:
TextView makeText = (TextView) itemView.findViewById(R.id.item_txtMake);
makeText.setText(phars[++index]);
return itemView;
}
}
private void populateListView() {
ArrayAdapter<String> adapter = new MyListAdapter();
lv.setAdapter(adapter);
}
}
如果您有解決我問題的方法,請幫助我和tnks
您應該使用AsyncTask,因為您的操作需要時間(下載,Web服務,從Internet獲取圖像等)。
您需要這樣的東西:
public class RestWebService extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
//to do whatever you want before execute webservice
//like progress bar or something like that
}
@Override
protected String doInBackground(String... arg0) {
//Use code for get Rest web Service
}
@Override
protected void onPostExecute(String resultList) {
//populate your listview
//maybe you need create custom adapter
}
}
您可以通過以下鏈接在Android上閱讀有關AsyncTask的其他信息: http : //androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/
希望對您有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.