简体   繁体   中英

How to create dynamic horizontal table row in android?

My current code is:

for(int i=0;i<jArray.length();i++){
            JSONObject json_data = jArray.getJSONObject(i);
            String b = json_data.getString("examnames");
            JSONObject getexamnamesobject = new JSONObject(b);
            getexamnames=getexamnamesobject.getString("types");
            TableRow tr = new TableRow(this);
            TextView tv1 = new TextView(this);
            createView(tr,tv1,getexamnames);
            t1.addView(tr);
        }
public void createView(TableRow tr, TextView t, String viewdata) {
        t.setText(viewdata);
        //adjust the porperties of the textView
        t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        //t.setTextColor(Color.DKGRAY);
        //t.setBackgroundColor(Color.CYAN);
        t.setPadding(20, 0, 0, 0); 
        tr.setPadding(0, 1, 0, 1); 
        //tr.setBackgroundColor(Color.BLACK);
        tr.addView(t); // add TextView to row.
        }

My output for above code is, I'm getting textviews vertically like this:

English
Maths
Science

What must be the change in the above coding to get the horizontal output like this:

English Maths Science

Please help me. This very important for me Thanks in advance

You are creating a TableRow and adding it to your TableLayout once for each item in jArray. Your createView method adds one string to that row: the value for the "types" key on the json object from the corresponding item in jArray ( getexamnamesobject.getString("types") ).

You might have to just loop through and collect all the "types" strings with a StringBuilder and put them in a single TableRow.

Your new LayoutParams should be new TableRow.LayoutParams. Views with "naked" LayoutParams could not be stacked one after another.

Set setStretchAllColumns( ) and setShrinkAllColumns( ) to the table layout. and make it orientation "horizontal" for the table layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM