2012-07-28 66 views
0

我試圖顯示我從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); 

線是我的總體結構確定或做我需要做的另一種方式?

回答

2

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