Domanda

I ran into another problem. Found a tutorial on how to create a dynamic table,followed it but mine doesnt seems to work,when adding the dynamic rows. The static column headings works fine.

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

        }

    });
}


}

i get this error in my eclipse "The constructor TableRow(new AsyncHttpResponseHandler(){}) is undefined". Please guide me.

È stato utile?

Soluzione

TableRow requires context as an input param

But, when inside

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

the current this means object of AsyncHttpResponseHandler.. since you created Anonymous Inner Class of AsyncHttpResponseHandler

try

TableRow tr = new TableRow(Leaders.this);

instead.

This also goes without saying all instance where context is required you have to pass yourActivity.this , in your case Leaders.this..

Altri suggerimenti

Change TableRow tr = new TableRow(this); to TableRow tr = new TableRow(Leaders.this); in your AsyncHttpResponseHandler

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top