【问题标题】:show results of web service rest in a listview Android在列表视图 Android 中显示 Web 服务的结果
【发布时间】:2014-06-05 00:33:50
【问题描述】:

我想在带有图片的列表视图中显示我的 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);
    }
    

}

【问题讨论】:

    标签: java android android-layout listview android-listview


    【解决方案1】:

    您应该使用 AsyncTask,因为您的操作需要时间(下载、网络服务、从互联网获取图像等)。

    你需要这样的东西:

    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/

    希望对你有帮助 编码很棒!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-15
      • 2015-02-04
      相关资源
      最近更新 更多