【问题标题】:Dynamic Android Table动态安卓表
【发布时间】:2013-12-14 07:07:05
【问题描述】:

我遇到了另一个问题。找到了关于如何创建动态表的教程,按照它,但我的似乎不起作用,添加动态行时。静态列标题工作正常。

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

        }

    });
}


}

我在 Eclipse "The constructor TableRow(new AsyncHttpResponseHandler(){}) is undefined" 中收到此错误。请指导我。

【问题讨论】:

    标签: android android-tablelayout anonymous-inner-class


    【解决方案1】:

    TableRow 需要上下文作为输入参数

    但是,当在里面

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

    当前的 this 表示 AsyncHttpResponseHandler 的对象.. 因为您创建了 Anonymous Inner ClassAsyncHttpResponseHandler

    试试

    TableRow tr = new TableRow(Leaders.this);
    

    改为。

    这也不用说所有需要上下文的实例,你必须通过 yourActivity.this ,在你的情况下Leaders.this..

    【讨论】:

    • 谢谢,它没有显示任何错误。但是现在表格没有出现。知道是什么原因造成的吗?
    • 你能添加硬编码tableRows吗?即注释掉你的AsyncHttpResponseHandler类部分……如果你的表出现了,那么你的匿名类有问题……我的猜测。因为它需要网络连接..
    • 谢谢,是文字颜色,和我的背景一样。
    • 干杯!!编码快乐!
    【解决方案2】:

    在您的AsyncHttpResponseHandler 中将TableRow tr = new TableRow(this); 更改为TableRow tr = new TableRow(Leaders.this);

    【讨论】:

    • 谢谢@Apoorv。真的很感激。
    • 我的表不再显示。知道是什么原因造成的吗?但我在 logcat 中没有收到任何错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 2023-03-05
    相关资源
    最近更新 更多