How to add multiple table row? - android

Only show one row(last data)
I did not get multiple row.There is showing only one row. How to solve this problem.
//Activity
public class TableViewActivity extends AppCompatActivity {
TableLayout tableLayout;
TableRow tableRow;
TextView textView1, textView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table_view);
tableLayout = (TableLayout) findViewById(R.id.tableLayout);
tableRow = (TableRow) findViewById(R.id.tableRow2);
textView1 = (TextView) findViewById(R.id.TextViewRowTwo1);
textView2 = (TextView) findViewById(R.id.TextViewRowTwo2);
List<Student> students = new ArrayList<>();
students.add(new Student(1, "Mahmud"));
students.add(new Student(2, "Saiful"));
students.add(new Student(3, "Monir"));
students.add(new Student(4, "Milton"));
for (int j = 0; j< students.size(); j++) {
textView1.setText(students.get(j).getId()+"");
textView2.setText(students.get(j).getName());
// TEXTVIEW
if(textView1.getParent()!=null)
((ViewGroup)textView1.getParent()).removeView(textView1); // <- fix
tableRow.addView(textView1);
if(textView2.getParent()!=null)
((ViewGroup)textView2.getParent()).removeView(textView2); // <- fix
tableRow.addView(textView2);
if(tableRow.getParent()!=null)
((ViewGroup)tableRow.getParent()).removeView(tableRow); // <- fix
tableLayout.addView(tableRow);
}
setContentView(tableLayout);
}
}

You need to create the TableRow Dynamically and whenever you put the values in tablerow you need to add that tablerow to tablelayut.
for (int j = 0; j< students.size(); j++) {
TableRow tableRow = new TableRow(this);
textView1.setText(students.get(j).getId()+"");
textView2.setText(students.get(j).getName());
// TEXTVIEW
if(textView1.getParent()!=null)
((ViewGroup)textView1.getParent()).removeView(textView1); // <- fix
tableRow.addView(textView1);
tableLayout.addView(tableRow);
if(textView2.getParent()!=null)
((ViewGroup)textView2.getParent()).removeView(textView2); // <- fix
tableRow.addView(textView2);
tableLayout.addView(tableRow);
if(tableRow.getParent()!=null)
((ViewGroup)tableRow.getParent()).removeView(tableRow); // <- fix
tableLayout.addView(tableRow);
}
Hope this will work for you.

I think that in your case is better for you to use a ListView. It's more easy and is designed to show a list of objects like your students.
https://developer.android.com/guide/topics/ui/layout/listview.html

Related

How to give a dynamic generated button an id?

Hello i used a code to generate some buttons in a tablelayout. The buttons are generated by this layout, so none of them has an id yet. So here is my question. How can i give a specific button an id? (For example the button in the first row, second colum should have the id "button2")
public class MainActivity extends ActionBarActivity {
private static final int NUM_ROWS = 3;
private static final int NUM_COLS = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateButtons();
}
private void populateButtons() {
TableLayout table = (TableLayout) findViewById(R.id.TableLayout);
for (int row =0; row < NUM_ROWS; row++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,
1.0f));
table.addView(tableRow);
for (int col = 0; col < NUM_COLS; col++) {
Button button = new Button(this);
tableRow.addView(button);
}
}
}
You can use View.setId(), however I guess you don't have to find the Views by id because you already know them.

addView() method throwing Nullpointer Exception

I am trying to instantiate a row of Buttons Programatically using Table Layout
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private final int gridSize = 3;
private TableRow rowArr[] = new TableRow[gridSize];
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create the layout
TableLayout MainLayout = new TableLayout(this);
MainLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT));
//MainLayout.setStretchAllColumns(true);
for ( int i =0 ; i < gridSize ; i++){
for(int j = 0; j < gridSize ; j++){
Button button = new Button(this);
button.setText(Integer.toString(i)+","+Integer.toString(j));
rowArr[i].addView(button);
}
MainLayout.addView(rowArr[i]);
}
//Set the view
setContentView(MainLayout);
}
However this line seems to throw a nullpointerexception
rowArr[i].addView(button);
What am i doing wrong?
you have initialized the rowArr array but not the individual TableRow element of it. so rowArr[i] will be null. in the for loop, put the following line in it:-
rowArr[i] = new TableRow(this);
Your TableRow is null as you haven't instantiate it.
try to instantiate it like this rowArr[i]=new TableRow();
for ( int i =0 ; i < gridSize ; i++){
rowArr[i]=new TableRow();
for(int j = 0; j < gridSize ; j++){
Button button = new Button(this);
button.setText(Integer.toString(i)+","+Integer.toString(j));
rowArr[i].addView(button);
}
MainLayout.addView(rowArr[i]);
}

tablelayout delete tablerow in android

final TableLayout table = (TableLayout) findViewById(R.id.tableLayout);
TableRow row = new TableRow(this);
TextView t2 = new TextView(this);
t2.setText("test");
row.addView(t2);
Button bu = new Button(this);
bu.setBackgroundResource(R.drawable.del);
bu.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//I need to delete the tablerow
//how to do?
}
});
row.addView(bu);
table.addView(row, new TableLayout.LayoutParams(WC, WC));
**
i want to delete tablerow in bu.setOnClickListener
how to do removeViewAt() ,i cant find indexId
**
use removeView for removing tablerow as:
table.removeView(row);
NOTE: If they don't have unique id then use:
table.removeView(rowIndex);
and by using removeViewAt
for(int i = 0, j < table.getChildCount(); i < j; i++){
// then, you can remove the the row you want...
// for instance...
TableRow row = getChildAt(i);
if( something you want to check ) {
removeViewAt(i);
// or...
removeView(row);
}
}

Checkbox selectAll\DeselectAll in tableLayout

I have List of route in tablelayout.
CheckBox & TextView created dynamically.
I set the checkbox id.
I want to do the selectall & deselectAll . How we can implement?
I created checkBox using coding.then How can call this CheckBox cb = (CheckBox)view.findViewById(R.id.????);
tl = (TableLayout) findViewById(R.id.dailyDRoute);
ArrayList<SalesRoutes> routeList = getSalesRoute();
for (int i = 0; i < routeList.size(); i++) {
TableRow tr = new TableRow(this);
CheckBox ch = new CheckBox(this);
ch.setHeight(1);
ch.setId(i);
TextView tv2 = new TextView(this);
tr.addView(ch);
tv2.setText(routeList.get(i).getDescription());
tv2.setTextColor(Color.BLACK);
tr.addView(tv2);
tl.addView(tr);
}
}
public void deselectAll(View view){
// CheckBox cb = (CheckBox)view.findViewById(R.id.);
}
public void selectAll(View view){
}
Please help me ...
Thanks in advance..
R.id.* are integer constants generated during build process. Actually, you need to pass an integer id to findViewById (same integer as you set us as id in init block).
Like this:
for (int i = 0; i < tl.getChildCount(); i++) {
CheckBox cb = (CheckBox)view.findViewById(i);
cb.setChecked(true);
}
But as for me this is not very reliable because id set in runtime can be not unique and I think this loop is better:
for (int i = 0; i < tl.getChildCount(); i++) {
CheckBox cb = (CheckBox)((TableRow)tl.getChildAt(i)).getChildAt(0);
cb.setChecked(true);
}

Tableview in android [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
show data in table view in android
how to data set in tableview.
how to show data in tableview.
A quick example:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TableRow.LayoutParams params = new TableRow.LayoutParams(100, 100);
TableLayout table = new TableLayout(this);
for (int i = 0; i < 3; i++) {
TableRow row = new TableRow(this);
for (int j = 0; j < 3; j++) {
TextView text = new TextView(this);
text.setLayoutParams(params);
text.setText(String.format("(%d, %d)", i, j));
row.addView(text);
}
table.addView(row);
}
setContentView(table);
}
}
If you can get away with it, GridView and ListView can be much easier to work with using adapters such as ArrayAdapter and CursorAdapter.

Categories

Resources