Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

show data in table view in android

I want to get data from database in my android table view.

Should I use loop? Is static good for this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This may be useful for you..

try{
    JSONArray jArray = new JSONArray(result);

    TableLayout tv=(TableLayout) findViewById(R.id.table);
    tv.removeAllViewsInLayout();
    int flag=1;

    // when i=-1, loop will display heading of each column
    // then usually data will be display from i=0 to jArray.length()
    for(int i=-1;i<jArray.length();i++){

        TableRow tr=new TableRow(Yourclassname.this);

        tr.setLayoutParams(new LayoutParams(
        LayoutParams.MATCH_PARENT,
        LayoutParams.WRAP_CONTENT));

        // this will be executed once
        if(flag==1){

            TextView b3=new TextView(Yourclassname.this);
            b3.setText("column heading 1");
            b3.setTextColor(Color.BLUE);
            b3.setTextSize(15);
            tr.addView(b3);

            TextView b4=new TextView(Yourclassname.this);
            b4.setPadding(10, 0, 0, 0);
            b4.setTextSize(15);
            b4.setText("column heading 2");
            b4.setTextColor(Color.BLUE);
            tr.addView(b4);

            TextView b5=new TextView(Yourclassname.this);
            b5.setPadding(10, 0, 0, 0);
            b5.setText("column heading 3");
            b5.setTextColor(Color.BLUE);
            b5.setTextSize(15);
            tr.addView(b5);
            tv.addView(tr);

            final View vline = new View(Yourclassname.this);
            vline.setLayoutParams(new       
            TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 2));
            vline.setBackgroundColor(Color.BLUE);
            tv.addView(vline); // add line below heading
            flag=0;
        } else {
            JSONObject json_data = jArray.getJSONObject(i);

            TextView b=new TextView(Yourclassname.this);
            String str=String.valueOf(json_data.getInt("column1"));
            b.setText(str);
            b.setTextColor(Color.RED);
            b.setTextSize(15);
            tr.addView(b);

            TextView b1=new TextView(Yourclassname.this);
            b1.setPadding(10, 0, 0, 0);
            b1.setTextSize(15);
            String str1=json_data.getString("column2");
            b1.setText(str1);
            b1.setTextColor(Color.WHITE);
            tr.addView(b1);

            TextView b2=new TextView(Yourclassname.this);
            b2.setPadding(10, 0, 0, 0);
            String str2=String.valueOf(json_data.getInt("column3"));
            b2.setText(str2);
            b2.setTextColor(Color.RED);
            b2.setTextSize(15);
            tr.addView(b2);
            tv.addView(tr);
            final View vline1 = new View(Yourclassname.this);
            vline1.setLayoutParams(new                
            TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 1));
            vline1.setBackgroundColor(Color.WHITE);
            tv.addView(vline1);  // add line below each row   
        }
    }
}catch(JSONException e){
    Log.e("log_tag", "Error parsing data " + e.toString());
    Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...