从HTTP请求更新列表视图

问题描述:

我试图显示我从web服务器上的mysql数据库获取的项目列表。这是我为该页创建的。从HTTP请求更新列表视图

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.getlist); 
    showBusyDialog(); 
    new GetData() 
    .execute(picdetails); 
} 

这是我的GetData类。这里的问题是将项目添加到列表视图的行。我传递一个JSON数组作为文本结果。

public class GetData extends AsyncTask<String, Void, String> { 
     @Override 
     protected String doInBackground(String... pic) { 
      // You might be tempted to display the busy dialog here, but you 
      // CAN'T update the UI from this method! 
      return loadImageFromNetwork(pic[0]); 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      // Our long-running process is done, and we can safely access the UI thread 
      int ct_id; 
      String item_make; 
      String item_model; 
      String item_price; 
      String item_desc; 
      String item_location; 
      String item_image; 
      dismissBusyDialog(); 

      TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout1); 
      try{ 
       JSONArray jArray = new JSONArray(result); 
       JSONObject json_data=null; 
       for(int i=0;i<jArray.length();i++){ 
        json_data = jArray.getJSONObject(i); 
         ct_id=json_data.getInt("id"); 
         item_make=json_data.getString("make"); 
         item_model=json_data.getString("model"); 
         item_price = json_data.getString("price"); 
         item_desc = json_data.getString("cardesc"); 
         item_location = json_data.getString("location"); 
         item_image = json_data.getString("image"); 
         String image_URL = "http://www.myurl.com/images/" + car_image; 

        TableRow tr = new TableRow(null); 
        tr.setId(ct_id); 
        tr.setClickable(true); 

        tr.setOnClickListener(new OnClickListener() { 

          @Override 
          public void onClick(final View v) { 

           String sdet_id; 
           int det_id; 
        //   v.setBackgroundColor(Color.GRAY); 
           det_id = v.getId(); 
           sdet_id = String.valueOf(det_id); 
           final Intent i = new Intent(); 
           i.setClassName("demo.example.com", "demo.example.com.viewdetails"); 
           i.putExtra("Det_id", sdet_id); 
           startActivity(i); 

         //  v.setBackgroundColor(Color.TRANSPARENT); 
           // TODO Auto-generated method stub 
          } 
         }); 
        TableLayout.LayoutParams tableRowParams= 
          new TableLayout.LayoutParams 
          (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT); 

         int leftMargin=20; 
         int topMargin=10; 
         int rightMargin=15; 
         int bottomMargin=20; 

         tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin); 

         tr.setLayoutParams(tableRowParams); 

          /* Create a Button to be the row-content. */ 
        ImageView myimage = new ImageView(R.layout.getlist); 

        BitmapFactory.Options bmOptions; 
        bmOptions = new BitmapFactory.Options(); 
        bmOptions.inSampleSize = 1; 
        Bitmap bm = LoadImage(image_URL, bmOptions); 
        myimage.setImageBitmap(bm); 
        tr.addView(myimage); 
        TextView tmake=new TextView(R.layout.getlist); 
        // tmake.setText(item_make); 
        tmake.setText(Html.fromHtml("<H1>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + item_location + "</H1>" + "<br />&nbsp;&nbsp;&nbsp;;" + item_desc 
       )); 
        tr.addView(tmake); 

        TextView tmodel=new TextView(R.layout.getlist); 
        tmodel.setText(Html.fromHtml("<b><H1>" + "&nbsp;&nbsp;" + "€" + item_price + "</b></H1></br></br>")); 
        tr.addView(tmodel); 



       /* Add row to TableLayout. */ 
       // tr.setPadding(50, 0, 0, 0); 
        tl.addView(tr); 
        View v = new View(R.layout.getlist); 
        v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1)); 
        v.setBackgroundColor(Color.rgb(51, 51, 51)); 
        tl.addView(v); 
         } 

       } 

       catch(JSONException e1){ 
        Toast.makeText(getBaseContext(), "No Category Found" ,Toast.LENGTH_LONG).show(); 
       } catch (ParseException e1) { 
        e1.printStackTrace(); 
      } 



     } 
    } 

的问题似乎是与创建新的项目添加到视图,这样

View v = new View(R.layout.getlist); 

线是我的总体结构确定或做我需要做的另一种方式?

1.通常我们会在 asyncTask的onPreExecute中显示繁忙的对话框。

2.你需要感谢inflater充实你的意见。

LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
View v = li.inflate(R.layout.view, null); 

希望这会帮助你。

+0

谢谢,这项工作像TextView? tmodel =新的TextView(R.layout.getlist); – user1197941 2012-07-28 16:41:44

+0

当然。如果你的textview是一个xml的根:TextView v =(TextView)li.inflate(R.layout.textview,null);但如果你的textView是在一个布局内:TextView v =(TextView)mLayout.findViewById(R.id.textView);我希望这很清楚 – AMerle 2012-07-28 17:29:28