0

Hi im trying to make the code go through an arraylist i have in an XML then put them in a tablelayout but im having trouble calling the arraylist from the XML

im trying somethin like this

ArrayList list = Collection(R.array.arraylist);
    int total = list.size();

for (int current = 0; current < total; current++)
    {
        // Create a TableRow and give it an ID
        TableRow tr = new TableRow(this);
        tr.setId(100+current);
        tr.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

 TextView labelTV = new TextView(this);
        labelTV.setId(200+current);
        labelTV.setText(list);
        labelTV.setTextSize(dip, 14);
        labelTV.setGravity(Gravity.CENTER);
        labelTV.setTextColor(Color.WHITE);
        labelTV.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        tr.addView(labelTV);

i dont think its actually going through the arraylist data and the textview isnt displaying the text so please any help

1
  • You're probably not adding the TableRow to the actual view Commented Jun 15, 2017 at 6:29

3 Answers 3

1

Create Table Layout using java file or xml file

Step1:(Step 1 has two ways follow any one)

/*create tablelayout in java no need for create activity that can be handle in java file if u want xml file use or condition loop*/

 TableLayout tableLayout=new TableLayout(this);
    tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
    setContentView(tableLayout);
/*Follow java code only then add the activity in manifest file then Go to step 2*/
  • --------------------or--------------------

    /create tablelayout in xml and getView in java/

XML:

<?xml version="1.0" encoding="utf-8"?>
  <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/table"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

 </TableLayout>

Java:

       setContentView(R.layout.table_layout);
 TableLayout tableLayout= (TableLayout) findViewById(R.id.table);

Step2:

    int colors[]=getResources().getIntArray(R.array.value);
    List<String> str=new ArrayList<String>();
    Collections.addAll(str, getResources().getStringArray(R.array.arrayvalues));
    Log.d(TAG,str.toString());
    for(int i=0;i<str.size();i++){
        TableRow row=new TableRow(this);
        row.setId(101+i);
        row.setBackgroundColor(Color.BLACK);
        row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));

        TextView text=new TextView(this);
        text.setId(201+i);
        text.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
        text.setText(str.get(i));
        text.setPadding(0,10,0,10);
        text.setTextColor(colors[i]);


        row.addView(text);
        tableLayout.addView(row);
    }       

Strings.xml

  <resources>
   <string-array name="arrayvalues">
    <item>Red</item>
    <item>Orange</item>
    <item>Yellow</item>
    <item>Green</item>
    <item>Blue</item>
    <item>Indigo</item>
    <item>Violet</item>
   </string-array>

   <integer-array name="value">
    <item>@color/Red</item>
    <item>@color/Orange</item>
    <item>@color/Yellow</item>
    <item>@color/Green</item>
    <item>@color/Blue</item>
    <item>@color/Indigo</item>
    <item>@color/Violet</item>
   </integer-array>
</resources>

colors.xml

<resources>
<color name="Red">#FF0000</color>
<color name="Orange">#FF7F00</color>
<color name="Yellow">#FFFF00</color>
<color name="Green">#00FF00</color>
<color name="Blue">#0000FF</color>
<color name="Indigo">#4B0082</color>
<color name="Violet">#9400D3</color>
</resources>

OutPut:

enter image description here

-------------------------------------------------------------------------Finish------------------------------------------------

Only For getting Array from string.xml file(Dont Follow this its only for getting array from string file)

    ArrayList<String> str=new ArrayList<String>();
    String[] val=getResources().getStringArray(R.array.value);
    str.addAll(Arrays.asList(val));
    Log.d(TAG,str.toString());
Sign up to request clarification or add additional context in comments.

Comments

0

instead of labelTV.setText(list); you need labelTV.setText(list.get(current)); I guess

Comments

0

Reference the particular element of the ArrayList that you want to use to set the text, presumably:

labelTV.setText(list.get(current)); // no idea what type you're dealing with, .toString() or a similar accessor may be necessary

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.