Tableview in android [duplicate] - android

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.

Related

How to add multiple table row?

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

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.

How to make sure if all the radio buttons are checked by the user?

So I am trying to make a simple quiz application and I want to make sure that the user answers all the question before continuing to the next activity. At start the button I have set is unavailable and I want to make it available ONLY if all the question have been answered. I cant figure out where and how to check if all the question have an answer.
public class Quiz extends Activity
{
Button buttHole;
String countryName[] = { "India", "Pakistan", "China", "Nepal", "Pichmaala", "Blah" };
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
buttHole = (Button)findViewById(R.id.button1);
buttHole.setEnabled(false);
//Creating the list of Questions
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
for (int i = 1; i <= 3; i++)
{
//create text button
TextView title = new TextView(this);
title.setText("Question Number:" + i);
title.setTextColor(Color.GREEN);
mLinearLayout.addView(title);
// create radio button
final RadioButton[] rb = new RadioButton[countryName.length];
RadioGroup radGr = new RadioGroup(this);
radGr.setOrientation(RadioGroup.VERTICAL);
radGr.setOnClickListener(l)
for (int j = 0; j < countryName.length; j++)
{
rb[j] = new RadioButton(this);
radGr.addView(rb[j]);
rb[j].setText(countryName[j]);
}
mLinearLayout.addView(radGr);
}
}
}
give radiobuttons id's as well and when you are going to submit this form(for example) mean on button click , get radio button object by id and then check radiobutton.isChecked().
Try following code to check whether user has given answer to all question or not
First Solution
int count = mLinearLayout.getChildCount();
for(int i=1;i<count ;i+=2){
RadioGroup rg = (RadioGroup) mLinearLayout.getChildAt(i);
if(rg.getCheckedRadioButtonId()==-1){
//Answer is not selected
//Do something to prevent execution
break;
}
}
Second Solution
public class Quiz extends Activity {
Button buttHole;
String countryName[] = { "India", "Pakistan", "China", "Nepal",
"Pichmaala", "Blah" };
ArrayList<RadioGroup> arrGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
buttHole = (Button) findViewById(R.id.button1);
buttHole.setEnabled(false);
// Creating the list of Questions
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
arrGroup = new ArrayList<RadioGroup>();
for (int i = 1; i <= 3; i++) {
// create text button
TextView title = new TextView(this);
title.setText("Question Number:" + i);
title.setTextColor(Color.GREEN);
mLinearLayout.addView(title);
// create radio button
final RadioButton[] rb = new RadioButton[countryName.length];
RadioGroup radGr = new RadioGroup(this);
// take a reference of RadioGroup view
arrGroup.add(radGr);
radGr.setOrientation(RadioGroup.VERTICAL);
radGr.setOnClickListener(l);
for (int j = 0; j < countryName.length; j++) {
rb[j] = new RadioButton(this);
radGr.addView(rb[j]);
rb[j].setText(countryName[j]);
}
mLinearLayout.addView(radGr);
}
}
public boolean checkQuestionsAnswer() {
int count = arrGroup.size();
for (int i = 0; i < count; i++) {
if (arrGroup.get(i).getCheckedRadioButtonId() == -1) {
//Return false, answer is remaining to given
//You can pass here position also to know question number
return false;
}
}
//Return true if all answer are given
return true;
}
}

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);
}
}

Categories

Resources