[TinyTut] - Dynamically add rows to TableLayout

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Problem with TAble Layout

Postby qpile » Sat Mar 27, 2010 9:15 am

Hello every one. I have some problem woth useing this template http://www.warriorpoint.com/blog/2009/0 ... matically/

I had wrote this within Oncreate :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String[] provinces = new String[13];
  2.  
  3.      provinces[0] = "Alberta";
  4.  
  5.      provinces[1] = "British Columbia";
  6.  
  7.      provinces[2] = "Manitoba";
  8.  
  9.      provinces[3] = "New Brunswick";
  10.  
  11.      provinces[4] = "Newfoundland and Labrador";
  12.  
  13.      provinces[5] = "Northwest Territories";
  14.  
  15.      provinces[6] = "Nova Scotia";
  16.  
  17.      provinces[7] = "Nunavut";
  18.  
  19.      provinces[8] = "Ontario";
  20.  
  21.      provinces[9] = "Prince Edward Island";
  22.  
  23.      provinces[10] = "Quebec";
  24.  
  25.      provinces[11] = "Saskatchewan";
  26.  
  27.      provinces[12] =  "Yukon";
  28.  
  29.      
  30.  
  31.      TableLayout tl = (TableLayout) findViewById(R.id.TableLayout01);
  32.  
  33.  
  34.  
  35.      // Go through each item in the array
  36.  
  37.      for (int current = 0; current < 13; current++)
  38.  
  39.      {
  40.  
  41.          // Create a TableRow and give it an ID
  42.  
  43.          TableRow tr = new TableRow(this);
  44.  
  45.          tr.setId(100+current);
  46.  
  47.          tr.setLayoutParams(new LayoutParams(
  48.  
  49.                  LayoutParams.FILL_PARENT,
  50.  
  51.                  LayoutParams.WRAP_CONTENT));  
  52.  
  53.  
  54.  
  55.          // Create a TextView to house the name of the province
  56.  
  57.          TextView labelTV = new TextView(this);
  58.  
  59.          labelTV.setId(200+current);
  60.  
  61.          labelTV.setText(provinces[current]);
  62.  
  63.          labelTV.setTextColor(Color.BLACK);
  64.  
  65.          labelTV.setLayoutParams(new LayoutParams(
  66.  
  67.                  LayoutParams.FILL_PARENT,
  68.  
  69.                  LayoutParams.WRAP_CONTENT));
  70.  
  71.          tr.addView(labelTV);
  72.  
  73.  
  74.  
  75.          // Create a TextView to house the value of the after-tax income
  76.  
  77.          TextView valueTV = new TextView(this);
  78.  
  79.          valueTV.setId(current);
  80.  
  81.          valueTV.setText("$0");
  82.  
  83.          valueTV.setTextColor(Color.BLACK);
  84.  
  85.          valueTV.setLayoutParams(new LayoutParams(
  86.  
  87.                  LayoutParams.FILL_PARENT,
  88.  
  89.                  LayoutParams.WRAP_CONTENT));
  90.  
  91.          tr.addView(valueTV);
  92.  
  93.  
  94.  
  95.          // Add the TableRow to the TableLayout
  96.  
  97.          tl.addView(tr, new TableLayout.LayoutParams(
  98.  
  99.                  LayoutParams.FILL_PARENT,
  100.  
  101.                  LayoutParams.WRAP_CONTENT));
  102.  
  103.      }
  104.  
  105.      
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


And this in main.xml :
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.         <TableLayout android:id="@+id/TableLayout01"
  2.  
  3.             android:layout_width="fill_parent"
  4.  
  5.             android:layout_height="fill_parent"
  6.  
  7.             android:stretchColumns="1">          
  8.  
  9.           </TableLayout>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


But no rows are added then i run the program.. Help me with this..
qpile
Freshman
Freshman
 
Posts: 2
Joined: Sat Mar 27, 2010 9:10 am

Top

Does this work with API level 2.1 ?

Postby ddudley3 » Fri Apr 16, 2010 2:27 pm

SO..... I have followed the code on this posting and I cannot get it to show these controls on the screen.
the ones I put in the xml do show.

I started my project with the API 2.1 and minsdkVersion of 7

Is this going to work with this newest level of the api ?

code I'm using is here..


TableLayout tl = (TableLayout)findViewById(R.id.TableLayout01);

/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setId(100+current);
tr.setOrientation(TableRow.VERTICAL);

/* Create controls to be the row-content. */
TextView textvw01 = new TextView(this);
textvw01.setId(200+current);
textvw01.setText("blah");
tr.addView(textvw01);

TextView textvw02 = new TextView(this);
textvw02.setId(300+current);
textvw02.setText("Blah2");
tr.addView(textvw02);

ImageView imgvw01 = new ImageView(this);
imgvw01.setId(400+current);
if (dto.statusCode == 0)
imgvw01.setImageDrawable(bmdComplete);
else if (dto.statusCode == 17)
imgvw01.setImageDrawable(bmdBE);
else if (dto.statusCode == 18)
imgvw01.setImageDrawable(bmdAE);
else
imgvw01.setImageDrawable(bmd);
tr.addView(imgvw01);

/* Add row to TableLayout. */
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
Thanks,
David
ddudley3
Freshman
Freshman
 
Posts: 6
Joined: Fri Apr 16, 2010 2:20 pm

it does seem to be something to do with API level

Postby ddudley3 » Fri Apr 16, 2010 2:52 pm

If I start a new Android project and make it use 1.5 instead of 2.1 it works fine.
Thanks,
David
ddudley3
Freshman
Freshman
 
Posts: 6
Joined: Fri Apr 16, 2010 2:20 pm

Re: [TinyTut] - Dynamically add rows to TableLayout

Postby alewis2005 » Sun Aug 15, 2010 2:46 pm

I'm having a bit of a problem with making the new views appear.

This is my code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     void createDynamicViews(String str){
  2.         Toast.makeText(this, "Point C", Toast.LENGTH_SHORT).show();
  3.         TableRow tr = new TableRow(context);
  4.         tr.setId(tableRowIncrementer);
  5.        
  6.         TextView tv = new TextView(context);
  7.         tv.setId(textViewIncrementer);
  8.         tv.setText(str);
  9.         tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
  10.         tv.setGravity(Gravity.LEFT);
  11.         tv.setMaxLines(1);
  12.         tv.setSingleLine(true);
  13.         tv.setTextSize(10f);
  14.         tr.addView(tv, 0);
  15.        
  16.         Button b = new Button(context);
  17.         b.setText("Del");
  18.         b.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
  19.         b.setGravity(Gravity.CENTER_HORIZONTAL);
  20.        
  21.         b.setOnClickListener(new View.OnClickListener() {
  22.                 int i = tableRowIncrementer;
  23.                 int j = textViewIncrementer;
  24.             public void onClick(View v) {
  25.                 currentNameslist.removeElement(((TextView)findViewById(j)).getText().toString());
  26.                 dynamicNames.removeView((TableRow)findViewById(i));
  27.                
  28.             }
  29.         });
  30.         tr.addView(b, 1);
  31.  
  32.         Toast.makeText(this, "Point D", Toast.LENGTH_SHORT).show();
  33.         dynamicNames.addView(tr, new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
  34.         dynamicNames.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  35.         dynamicNames.invalidate();
  36.         Toast.makeText(this, "Point E", Toast.LENGTH_SHORT).show();
  37.         tableRowIncrementer++;
  38.         textViewIncrementer++;
  39.     }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

When I run the method, it all executes (all the Toasts pop up in order) but nothing appears. dynamicNames is a TableLayout that I want the new views to be created in. The button is a delete button that removes the relevant string from an array and then removes the row that it appears in.

Anyone know whats going wrong here?

Edit: I didn't put notifyDataSetChanged() in my adapter. Urgh.
alewis2005
Freshman
Freshman
 
Posts: 2
Joined: Sun Aug 15, 2010 2:42 pm

Dynamically add rows at bottom of the screen in TableLayout

Postby anjaneyabt » Sat Aug 28, 2010 10:15 am

Hello ,

I want to develop a small game with button motion .
SO i want to add a bottom of the screen and for that button i want to add listener such that ,
by pressing "<-" this key button should move to the right similarly for "->"..

Help me out



Thanks .
anjaneyabt
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Aug 28, 2010 10:11 am

Re: [TinyTut] - Dynamically add rows to TableLayout

Postby NoneTheWiser » Tue Mar 15, 2011 7:13 am

I was having the same problem as several others, and found the answer to my problem on StackOverflow. Make sure that your import for LayoutParams is "import android.widget.TableRow.LayoutParams;". I had "import android.view.ViewGroup.LayoutParams;", and I would not see any of the TableRows I added.
NoneTheWiser
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Mar 15, 2011 7:05 am

Top
Previous

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 9 guests