简体   繁体   English

在列表视图中显示Web服务的结果

[英]show results of web service rest in a listview android

I want to show the result of my webservice rest in a listview with a picture so I tried to create a item_veiw: 我想在带有图片的列表视图中显示我的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>

and I have allready a layout wich contain just a listView: 而且我已经准备好只包含一个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>

And in my activity I have : 在我的活动中,我有:

    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);
    }


}

If you have a solution for my problem help me and tnks 如果您有解决我问题的方法,请帮助我和tnks

You should use AsyncTask because your operation take time(download, web service, getting image from internet, etc). 您应该使用AsyncTask,因为您的操作需要时间(下载,Web服务,从Internet获取图像等)。

You need something like this: 您需要这样的东西:

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

    }

}

You can read something else about AsyncTask on Android in this link: http://androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/ 您可以通过以下链接在Android上阅读有关AsyncTask的其他信息: http : //androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/

I hope that help you Have great coding! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM