How to click an specific TableRow within a TableLayout - android

I made my own compound control that display a data grid using TableLayout and adding programmatically Tablerows within a loop depending of the Array of Object that im binding to it and now i want to select an specific row with its specific data in order to be used by a method. So how can i select an specific row retrieving its data to delegate a method?

hi you can try something like this,
// create a new TableRow
TableRow row = new TableRow(this);
row.setClickable(true); //allows you to select a specific row
row.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
v.setBackgroundColor(Color.GRAY);
System.out.println("Row clicked: " + v.getId());
//get the data you need
TableRow tablerow = (TableRow)v.getParent();
TextView sample = (TextView) tablerow.getChildAt(2);
String result=sample.getText().toString();
}
});
For more info refer Android TableRow

I tried Parth Doshi's answer and found it to be not quite correct. The view parameter in onClick is a TableRow, so when v.getParent() is called, it returns a TableLayout object, and so an exception is thrown when casting it to TableRow. As such the code that works for me is:
tableRow.setClickable(true); //allows you to select a specific row
tableRow.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
TableRow tablerow = (TableRow) view;
TextView sample = (TextView) tablerow.getChildAt(1);
String result=sample.getText().toString();
Toast toast = Toast.makeText(myActivity, result, Toast.LENGTH_LONG);
toast.show();
}
});

Related

Table with clickable lines?

Is it possible to do a table like the sudoku's one and make the lines which divide the holes clickable? If it's not, what view should I use to make a grid with clickable lines?
Thanks!
Try this:
// create a new TableRow
TableRow row = new TableRow(this);
row.setClickable(true); //allows you to select a specific row
row.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
v.setBackgroundColor(Color.GRAY);
System.out.println("Row clicked: " + v.getId());
//get the data you need
TableRow tablerow = (TableRow)v.getParent();
TextView sample = (TextView) tablerow.getChildAt(2);
String result=sample.getText().toString();
}
});

How to get the row id of a particular selected row?

I have added two TableRows to TableLayout that is in a for loop. In every first TableRow there are 6 TextViews and 1 ImageView & 1 Toggle Button. I have added an OnClickListener to the first row & Toggle Button. My question is in the onclick method for the First Row, how should I get the row id of that particular selected row?
Can someone help me?
Plz help me.....
I want to get the row id of a particular selected row......
Plz anyone reply me....
Navigate through the View hierarchy in the ImageView OnCLickListener like this:
imageView.setOnClickListener(new OnClickListener() {
public void onCLick(View v) {
// v represents the view that was clicked(the ImageView)
// if the ImageView is added directly to the TableRow then you could simply do
TableRow tr = (TableRow) v.getParent(); // if the ImageView isn't a direct child of the TableRow then walk through all the parent with getParent().
// do stuff
}
});
Edit :
Your code doesn't help and by looking at what you're doing I would advise you to start with something a little simpler to learn about android. Also, tabLayout.addView(openedRow1, n_D); doesn't add a tag to the openedRow1 View(if this is what you want to do), it just position that View to the specific position in the parent(like 0 to be the first child of the parent).
I would guess that you're trying to get the text from the second TextView( tv_vin_val ?!?!) when it's parent is clicked. If this is all you want to do then try something like this:
public void onClick(View v) {
// you set the listener on the TableRow so v is the TableRow that was clicked
TableRow lTableRow = ((TableRow) v);
// to get the TextView use getChildAt or findViewById
TextView lTextView = (TextView)lTableRow.getChildAt(1);
//get the text from the TextView
vinNum = lTextView.getText().toString();
// the tag
int theTag = (Integer) v.getTag();
Intent intent = new Intent(DeliveryInspectionActivity.this, ExceptionsActivity.class);
//intent.putExtra("row id value", theTag);
startActivityForResult(intent, 0);
}
I'm not certain how you would get the correct row number for that, but what I would suggest instead is to use the getParent() method of View to get a reference to the TableRow containing the image and work directly from that. Something like this:
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TableRow row = (TableRow)v.getParent();
//handle your stuff here
}
});

TableRow OnClickListener's getId returns -1 - What gives?

I'm creating tablerows programmatically, and I'm trying to implement an OnClickListener:
final TableRow tr = new TableRow(this);
tr.setId(tourney.getColumnIndex("_id"));
tr.setClickable(true);
tr.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
v.setBackgroundColor(Color.GRAY);
System.out.println("Row clicked: " + v.getId());
}
});
But everytime I click on the row, I get a value of -1. Why am I not getting the id that I set?
Your TableRow object is conforming to OOP standards, it should only be aware of it´s own state, not it´s position in another object. Instead, you should grab a reference to your TableLayout, and then call indexOfChild(View child) on it:
TableLayout tblLayout = (TableLayout) findViewById(R.id.yourTableLayout);
int tableRowIndex = tblLayout.indexOfChild(tr);
The reason why I was getting -1 on getId was because of this line:
tr.setId(tourney.getColumnIndex("_id"));
"_id" was not a part of the query, so I was not getting the column index. Even then, it wasn't the value I really wanted, but this fixed my issue:
tr.setId(tourney.getInt(tourney.getColumnIndex("_id"));

How to retrieve TextView value when we Touch on that and The TextView is added into LinearLayout?

Here is my code for how i make adding Dynamically TextView in to Table Layout using LinearLayout for it.
table_personalData.removeAllViews();
int i=0;
for(String header : headerDetail){
TextView label=new TextView(this);
TextView value=new TextView(this);
label.setTextAppearance(this, R.style.TextBase);
value.setTextAppearance(this, R.style.TextBase);
label.setText(String.format("%s:", header));
value.setText(String.format("%s", lead.data[i]==null?"":lead.data[i]));
i++;
LinearLayout rowview=new LinearLayout(this);
rowview.setOrientation(0);
rowview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
rowview.addView(label);
rowview.addView(value);
table_personalData.addView(rowview);
}
By Above code i get my table layout with dynamically added textview in that .
here i use Liner Layout for added two textview in One Row .
headerDetail is a ArrayList<String> headerDetail
lead.data[i] i used this to get my values for particular label.
All this work fine for me now what my problem is
Now i want to do one thing that is when we click one of the TextView of my LinearLayout i want that TextView in dynamically.
So finally my question is about to get retrieve textview value when we touch or click any textview which is in Linearlayout and LinearLayout is in TableLayout.
Anyone have idea what should i have to do with this .
i also try something like below but i don't get i can i get TextView Value from below code.
table_personalData.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<table_personalData.getChildCount();i++){
//CharSequence desc=table_personalData.getChildAt(i).getContentDescription();
//Log.v("log_tag", "the values are"+ desc);
}
}
});
EDIT:
if you want the Value of TextView when we Touch on that There is Two way that i write Below :
value.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView txt;
txt=(TextView)findViewById(v.getId());
String tempEmail;
tempEmail=txt.getText().toString().trim();
String aEmailList[]={tempEmail};
pattern=Pattern.compile(EMAIL_PATTERN);
if(validate(tempEmail)){
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,aEmailList );
startActivity(emailIntent);
}else{
pattern=Pattern.compile(PHONE_No_PATTERN);
if(validate(tempEmail)){
tempEmail=tempEmail.replaceAll("[^\\d]", "");
String uri="tel:" +tempEmail;
Intent mIntent=new Intent(Intent.ACTION_CALL);
mIntent.setData(Uri.parse(uri));
startActivity(mIntent);
}else{
//Toast.makeText(LeadDetailActivity.this, "Please Touch on Email or Mo:Number", Toast.LENGTH_SHORT).show();
}
}
}
});
1)Create how many text view u want,but set id for each one,and aslo set same click listener for all
TextView value1=new TextView(this);
value1.setid(1);
value1.setOnClickListener(clickListener);
TextView value2=new TextView(this);
value1.setid(2);
value2.setOnClickListener(clickListener);
TextView value3=new TextView(this);
value3.setid(3);
value3.setOnClickListener(clickListener);
2)second create one onclick listener,inside this listener u will get all cliks. so based on view id u can dynamically do anything
OnClickListener clickListener=new OnClickListener() {
public void onClick(View v) {
int id=v.getId();
}
};

How to delete a row in TableLayout dynamically

I am having a TableLayout for which I added rows dynamically.In each of the row there are 2 elements of which one is TextView other is Button.When I click the button that is present in a row,that row should be deleted.How can this be done in Android ? How to find rowid and how to delete a row dynamically.
can anyone help me in sorting out this issue.
Thanks in Advance,
Try this kind of approach:
TableRow tableRow;
TextView tv;
Button button;
//...
tableRow.addView(tv)
tableRow.addView(button);
//somewhere where you want to delete
ViewGroup parent=button.getParent();
if(parent instanceof TableRow)
//do deleting task
Try this:
public void onClick(View v) {
// TODO Auto-generated method stub
TableRow t = (TableRow) v.getParent();
TextView firstTextView = (TextView) t.getChildAt(0);
code = firstTextView.getText().toString();
System.out.println("code>>>>>>" + code);
View row = (View) v.getParent();
// container contains all the rows, you could keep a
// variable somewhere else to the container which you
// can refer to here
ViewGroup container = ((ViewGroup) row.getParent());
// delete the row and invalidate your view so it gets
// redrawn
container.removeView(row);
container.invalidate();
}
You can assign tags or id's when you adding a row. Then just use that tag/id to delete that row.
TableLayout table; // global access, probably initialized in onCreate()
// initialization, etc.
Create element that will be added to the TableLayout, a TableRow with the TextView and the Button then call addDeleteClick(yourButton, uniqueTag) before adding it to the TableLayout.
// example of adding a text view and a button the the TableLayout
void addToTableLayout(String text, String uniqueTag) {
TableRow tr = new TableRow(yourActivity);
// set the unique tag that will be used when deleting this row
tr.setTag(uniqueTag);
// do what you need with the button and textview
TextView tv = new TextView(yourActivity);
tv.setText(text);
Button bt = new Button(yourActivity);
// add delete click capability to the button
addDeleteClick(bt, uniqueTag);
table.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
// Adds the delete on click capability to a button
// to delete a row from TableLayout based on its tag
void addDeleteClick(Button bt, final String tag) {
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.d("TableLayout", " deleting row with tag " + tag);
deleteRow(tag);
}
});
}
// delete a row from TableLayout based on its tag
void deleteRow(String tag) {
View removedRow = table.findViewWithTag(tag);
table.removeView(removedRow);
table.invalidate();
}

Categories

Resources