Problem with TableRow (Please Help)

Quickly share your Android Code Snippets here...

Problem with TableRow (Please Help)

Postby ramesh_bh » Thu Oct 07, 2010 5:16 am

Hi,

My requirement is like i have to display multiple rows, and each row should contain three buttons as columns.
here is my code :

TableLayout contactLayout;
TableRow tr[] = null;
Button b1 = null;
Button b2 = null;
Button b3 = null;

contactLayout = (TableLayout) findViewById(R.id.ContactsLayout);

for (int i = 0; i < 10; i++) {

b1= new Button(this);
b2 = new Button(this);
b3= new Button(this);

tr[i] = new TableRow(this);
b1.setText("Ramesh");
b2.setText("R@infy.com");
b3.setText("9591504000");
tr[i].addView(b1, new TableRow.LayoutParams(1));
tr[i].addView(b2, new TableRow.LayoutParams(2));
tr[i].addView(b3, new TableRow.LayoutParams(3));
contactLayout.addView(tr[i], new TableLayout.LayoutParams());
}

But after running the application, i got null pointer exception at tr[i] = new TableRow(this);
Can any one help me to solve this,
If any similar example providing can also be useful to me.
Please Respond.

Ramesh BH
ramesh_bh
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Oct 07, 2010 4:58 am

Top

Re: Problem with TableRow (Please Help)

Postby Sathasivan » Thu Oct 07, 2010 6:13 am

Hi Ramesh,

you declared your TableRow array is null, so you are getting null pointer exception. you have to initialize your array.

I changed your declaration as follows and its works fine here,

TableLayout contactLayout;
TableRow tr[] = new TableRow[10];
Button b1 = null;
Button b2 = null;
Button b3 = null;

contactLayout = (TableLayout) findViewById(R.id.ContactsLayout);

for (int i = 0; i < 10; i++) {

b1= new Button(this);
b2 = new Button(this);
b3= new Button(this);

tr[i] = new TableRow(this);
b1.setText("Ramesh");
b2.setText("R@infy.com");
b3.setText("9591504000");
tr[i].addView(b1, new TableRow.LayoutParams(1));
tr[i].addView(b2, new TableRow.LayoutParams(2));
tr[i].addView(b3, new TableRow.LayoutParams(3));
contactLayout.addView(tr[i], new TableLayout.LayoutParams());
User avatar
Sathasivan
Experienced Developer
Experienced Developer
 
Posts: 67
Joined: Wed Oct 14, 2009 7:51 am
Location: Chennai

Re: Problem with TableRow (Please Help)

Postby ramesh_bh » Thu Oct 07, 2010 6:54 am

Yes ,Thanks for the help,
But i want to implement onclick event to every row in my table,
i have implemented the same code as like this :
TableLayout contactLayout;
TableRow tr[] = new TableRow[10];
Button b1 = null;
Button b2 = null;
Button b3 = null;

contactLayout = (TableLayout) findViewById(R.id.ContactsLayout);
for (int i = 0; i < 10; i++) {

b1= new Button(this);
b2 = new Button(this);
b3= new Button(this);

tr[i] = new TableRow(this);
b1.getBackground().setColorFilter(R.color.important_text,PorterDuff.Mode.MULTIPLY);
b2.getBackground().setColorFilter(R.color.important_text,PorterDuff.Mode.MULTIPLY);
b3.getBackground().setColorFilter(R.color.important_text,PorterDuff.Mode.MULTIPLY);
b1.setTextColor(Color.WHITE);
b2.setTextColor(Color.WHITE);
b3.setTextColor(Color.WHITE);
b1.setText("Ramesh");
b2.setText("R@infy.com");
b3.setText("9591504000");
tr[i].addView(b1, new TableRow.LayoutParams(1));
tr[i].addView(b2, new TableRow.LayoutParams(2));
tr[i].addView(b3, new TableRow.LayoutParams(3));
tr[i].setClickable(true);
tr[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Under Onclick");
if(tr[0].isPressed()) {
System.out.println("Cliked t.... CADSFaslkfjsf <<<<<"+tr[0].getChildAt(0));
System.out.println("Cliked t.... CADSFaslkfjsf <<<<<"+tr[0].getChildAt(1));
System.out.println("Cliked t.... CADSFaslkfjsf <<<<<"+tr[0].getChildAt(2));
}

}
});
contactLayout.addView(tr[i], new TableLayout.LayoutParams());
}

But am not getting any output when click any row, can you let me know where could i done mistake ?
ramesh_bh
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Oct 07, 2010 4:58 am

Re: Problem with TableRow (Please Help)

Postby Sathasivan » Thu Oct 07, 2010 7:45 am

hi,

me too got same issue, because of each three buttons are covers each tablerows. so we are just clicking buttons but not tablerows. you can see this by setting invisible to all the buttons as follow,

TableLayout contactLayout;
final TableRow tr[] = new TableRow[10];
Button b1 = null;
Button b2 = null;
Button b3 = null;

contactLayout = (TableLayout) findViewById(R.id.ContactsLayout);
for (int i = 0; i < 10; i++) {

b1= new Button(this);
b2 = new Button(this);
b3= new Button(this);

tr[i] = new TableRow(this);
b1.getBackground().setColorFilter(R.color.important_text,PorterDuff.Mode.MULTIPLY);
b2.getBackground().setColorFilter(R.color.important_text,PorterDuff.Mode.MULTIPLY);
b3.getBackground().setColorFilter(R.color.important_text,PorterDuff.Mode.MULTIPLY);
b1.setTextColor(Color.WHITE);
b2.setTextColor(Color.WHITE);
b3.setTextColor(Color.WHITE);
b1.setText("Ramesh");
b2.setText("R@infy.com");
b3.setText("9591504000");
b1.setVisibility(View.INVISIBLE);
b2.setVisibility(View.INVISIBLE);
b3.setVisibility(View.INVISIBLE);

tr[i].addView(b1, new TableRow.LayoutParams(1));
tr[i].addView(b2, new TableRow.LayoutParams(2));
tr[i].addView(b3, new TableRow.LayoutParams(3));
tr[i].setBackgroundColor(R.color.a1);
tr[i].setClickable(true);
tr[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Under Onclick");
if(tr[0].isPressed()) {
System.out.println("Cliked t.... CADSFaslkfjsf <<<<<"+tr[0].getChildAt(0));
System.out.println("Cliked t.... CADSFaslkfjsf <<<<<"+tr[0].getChildAt(1));
System.out.println("Cliked t.... CADSFaslkfjsf <<<<<"+tr[0].getChildAt(2));

}
}
});
contactLayout.addView(tr[i], new TableLayout.LayoutParams());
}
}

if you change the LayoutParams for tablerows and buttons you may resolve it.
User avatar
Sathasivan
Experienced Developer
Experienced Developer
 
Posts: 67
Joined: Wed Oct 14, 2009 7:51 am
Location: Chennai

Re: Problem with TableRow (Please Help)

Postby ramesh_bh » Thu Oct 07, 2010 10:15 am

HI,
Thanks again :)
My actual intention is not to add the buttons for the row, i have to display the deails in the table,
if i click any row i want to get the corresponding cicked values, for this i have added buttons there, if it is
textview also no problem with me, Finally i want to capture the selected value,
Do u have any idea on this ?
ramesh_bh
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Oct 07, 2010 4:58 am

Re: Problem with TableRow (Please Help)

Postby Sathasivan » Thu Oct 07, 2010 11:11 am

oh., ok anyway try to use ListView.
User avatar
Sathasivan
Experienced Developer
Experienced Developer
 
Posts: 67
Joined: Wed Oct 14, 2009 7:51 am
Location: Chennai

Top

Re: Problem with TableRow (Please Help)

Postby ramesh_bh » Thu Oct 07, 2010 11:49 am

HI,

If possible can you provide any related example to workout my requirement.
ramesh_bh
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Oct 07, 2010 4:58 am

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 4 guests