2013-12-14 51 views
0

我遇到了另一個問題。在添加動態行時,找到了關於如何創建動態表的教程,並遵循它,但似乎並不適用。靜態列標題工作正常。動態Android表

public class Leaders extends Activity { 
TableLayout tl; 
ProgressDialog mProgressDialog; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 

    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.leaderboard); 
    init(); 
} 

@SuppressWarnings("deprecation") 
private void init() { 

tl = (TableLayout) findViewById(R.id.main_table); 
    TableRow tr_head = new TableRow(this); 
    tr_head.setId(10); 
    tr_head.setBackgroundColor(Color.RED); 
    tr_head.setLayoutParams(new LayoutParams(
    LayoutParams.FILL_PARENT, 
    LayoutParams.WRAP_CONTENT)); 

    TextView label_name = new TextView(this); 
    label_name .setId(20); 
    label_name .setText("NAME"); 
    label_name .setTextColor(Color.WHITE); 
    label_name .setPadding(5, 5, 5, 5); 
    tr_head.addView(label_name);// add the column to the table row here 

    TextView label_predictions = new TextView(this); 
    label_predictions.setId(22);// define id that must be unique 
    label_predictions.setText("PREDICTIONS"); // set the text for the header 
    label_predictions.setTextColor(Color.WHITE); // set the color 
    label_predictions.setPadding(5, 5, 5, 5); // set the padding (if required) 
    tr_head.addView(label_predictions); // add the column to the table row here 


    TextView label_crrect = new TextView(this); 
    label_crrect.setId(23);// define id that must be unique 
    label_crrect.setText("Correct Predictions"); // set the text for the header 
    label_crrect.setTextColor(Color.WHITE); // set the color 
    label_crrect.setPadding(5, 5, 5, 5); // set the padding (if required) 
    tr_head.addView(label_crrect); // add the column to the table row here 

    TextView label_points = new TextView(this); 
    label_points.setId(21);// define id that must be unique 
    label_points.setText("Points"); // set the text for the header 
    label_points.setTextColor(Color.WHITE); // set the color 
    label_points.setPadding(5, 5, 5, 5); // set the padding (if required) 
    tr_head.addView(label_points); // add the column to the table row here 




    tl.addView(tr_head, new TableLayout.LayoutParams(
      LayoutParams.FILL_PARENT, 
      LayoutParams.WRAP_CONTENT)); 



    // TODO Auto-generated method stub 
    AsyncHttpClient getleaders = new AsyncHttpClient(); 
    getleaders.get("http://10.0.2.2/fanaticmobile/leaders.php", new AsyncHttpResponseHandler(){ 

     @Override 
     public void onFailure(int arg0, Header[] arg1, byte[] arg2, 
       Throwable arg3) { 
      // TODO Auto-generated method stub 
      super.onFailure(arg0, arg1, arg2, arg3); 

      Log.d("error", arg3.toString()); 
     } 

     @Override 
     public void onStart() { 
      mProgressDialog = ProgressDialog.show(Leaders.this, "Loading...", "Loading Data..."); 
      // TODO Auto-generated method stub 
      super.onStart(); 
     } 
     @Override 
     public void onFinish() { 
      mProgressDialog.dismiss(); 
      // TODO Auto-generated method stub 
      super.onFinish(); 
     } 

     @Override 
     @Deprecated 
     public void onSuccess(String content) { 
      // TODO Auto-generated method stub 
      super.onSuccess(content); 
      try { 
       JSONObject json = new JSONObject(content); 
       JSONArray leaders= json.getJSONArray("rows"); 
       //Log.d("leaders",leaders.toString()); 
       for(int i=0;i<leaders.length(); i++){ 
        JSONObject jsonas = leaders.getJSONObject(i); 
        String fname = jsonas.getString("Fname"); 
        String lname= jsonas.getString("Lname"); 
        String predictions = jsonas.getString("Predictions"); 
        String cp = jsonas.getString("Cpredictions"); 
        String points = jsonas.getString("Points"); 

        Integer count=0; 

        TableRow tr = new TableRow(this); 
        if(count%2!=0) tr.setBackgroundColor(Color.GRAY); 
        tr.setId(100+count); 
        tr.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 

        TextView name = new TextView(this); 
        name.setId(200+count); 
        name.setText(fname+ " " + lname); 
        name.setPadding(2, 0, 5, 0); 
        name.setTextColor(Color.WHITE); 
        tr.addView(name); 

        //second row 

        TextView prdiction_lbl = new TextView(this); 
        prdiction_lbl.setId(200+count); 
        prdiction_lbl.setText(predictions); 
        prdiction_lbl.setPadding(2, 0, 5, 0); 
        prdiction_lbl.setTextColor(Color.WHITE); 
        tr.addView(prdiction_lbl); 
        //3rd row 
        TextView c_predi = new TextView(this); 
        c_predi.setId(200+count); 
        c_predi.setText(cp); 
        c_predi.setPadding(2, 0, 5, 0); 
        c_predi.setTextColor(Color.WHITE); 
        tr.addView(c_predi); 
        //4th 
        TextView points_lbl = new TextView(this); 
        points_lbl.setId(200+count); 
        points_lbl.setText(points); 
        points_lbl.setPadding(2, 0, 5, 0); 
        points_lbl.setTextColor(Color.WHITE); 
        tr.addView(points_lbl); 





        tl.addView(tr, new TableLayout.LayoutParams(
          LayoutParams.FILL_PARENT, 
          LayoutParams.WRAP_CONTENT)); 
        count++; 
       } 

      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 

    }); 
} 


} 

我得到這個錯誤在我的月食"The constructor TableRow(new AsyncHttpResponseHandler(){}) is undefined"。請指導我。

回答

0

TableRow需要上下文作爲輸入參數

但是,當內

AsyncHttpClient getleaders = new AsyncHttpClient(); 
    getleaders.get("http://10.0.2.2/fanaticmobile/leaders.php", new AsyncHttpResponseHandler(){ 
...... 
} 

當前意味着AsyncHttpResponseHandler的對象..因爲你創建Anonymous Inner ClassAsyncHttpResponseHandler

嘗試

TableRow tr = new TableRow(Leaders.this); 

代替。

這也不用說在需要情況下,你必須通過yourActivity.this,你的情況Leaders.this所有的實例..

+0

謝謝,它沒有顯示任何錯誤。但是現在桌子沒有出現。任何想法是什麼造成的? – altoin

+0

你可以添加硬編碼tableRows ..即註釋掉你'AsyncHttpResponseHandler'類部分...如果你的表顯示出來,然後有什麼錯在你的匿名類...我的胡亂猜測。因爲它需要網絡連接.. – CRUSADER

+0

謝謝你,這是文字顏色,它與我的背景相同。 – altoin

0

變化TableRow tr = new TableRow(this);TableRow tr = new TableRow(Leaders.this);AsyncHttpResponseHandler

+0

感謝@Apoorv。萬分感激。 – altoin

+0

不客氣。如果它有幫助,你可以接受答案。 – Apoorv

+0

我的表格不再顯示。任何想法是什麼導致它?但我沒有得到任何錯誤logcat – altoin