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();
}
Related
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();
}
});
I have successfully added a TableRow to the TableLayout,now I would like to programmatically remove the TableRow and add the original row to the TableLayout.Here is the code that I use to generate the TableRow:
public TableRow getTableRow(String text,String hint,boolean addCollapseOption)
{
TableRow tr=new TableRow(getBaseContext());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
TextView temp=new TextView(getBaseContext());
temp.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
temp.setText(text);
tr.addView(temp);
tempId="edit_"+count;
count++;
EditText edit_temp=new EditText(getBaseContext());
TableRow.LayoutParams edit_params=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT,1f);
edit_params.setMargins(10, 0, 10, 0);
edit_temp.setLayoutParams(edit_params);
edit_temp.setHint(hint);
tr.addView(edit_temp);
if(addCollapseOption)
{
ImageButton btn_less=new ImageButton(getBaseContext());
btn_less.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
btn_less.setImageDrawable(getResources().getDrawable(R.drawable.icon_less));
btn_less.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
shouldCollapse=true;
}
});
tr.addView(btn_less);
}
return tr;
}
The Activity crashes when I try to add this TableRow to the TableLayout:
TableRow tr=getTableRow("First Name","Enter first name here",true);
I guess you cannot remove a View from the Layout and still be able to use the click event in it(that crashes the activity),layout and shouldCollapse are global variables,if set to true:
if(shouldCollapse)
{
layout.removeAllViews();
layout.addView(originalRow);
}
EDIT:Changing the implementation to use removeChildAt does not work:
if(shouldCollapse)
{
int childCount=layout.getChildCount();
for(int i=1;i<childCount;i++)
layout.removeViewAt(i);
originalRow.setVisibility(TableRow.VISIBLE);
shouldCollapse=false;
}
Now,clicking on the btn_less does absolutely nothing.This is the Logcat's opinion of the problem:
Less clicked
The value of shouldCollapse true
So,this means that even though shouldCollapse is set to true but this method is never called...strange.
You can use this code to remove the programmatically generated TableRows:
public void onClick(View v)
{
final TableRow parent = (TableRow) v.getParent();
tr.removeView(parent);
}
Or if you want to use this out side the onClick, you must pass the value of View v to whatever function it is.
My android app is populating a table layout which is created programmatically from the sqlite database.the last column of the table layout has two buttons. those two buttons are assigned to one column by a linear layout.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
35, 35);
params.setMargins(10, 0, 10, 0);
seventhCol1=new Button(this);
seventhCol1.setText("A");
seventhCol1.setLayoutParams(params);
//seventhCol1.setLayoutParams(new LayoutParams(30, 30));
seventhCol1.setId(71);
seventhCol1.setOnClickListener(mListener);
seventhCol2=new Button(this);
seventhCol2.setText("C");
seventhCol2.setLayoutParams(params);
seventhCol2.setId(72);
seventhCol2.setOnClickListener(nListener);
btn_linear=new LinearLayout(this);
btn_linear.setOrientation(LinearLayout.HORIZONTAL);
btn_linear.addView(seventhCol1);
btn_linear.addView(seventhCol2);
btn_linear.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
btn_linear.setGravity(Gravity.CENTER);
tr.addView(btn_linear);
on click of the button seventhCol1 i want to get the data from the particular row from where that button was clicked...how can I do that????
I tried doing it by this method:
tr.setOnClickListener(rowclickListner);
private OnClickListener rowclickListner=new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TableLayout tl=(TableLayout) v.getParent();
/*TableRow tr=(TableRow) tl.getParent();*/
TableRow tr=(TableRow) tl.getParent();
TextView tv_mobile=(TextView)tr.getChildAt(2);
String mobile=tv_mobile.getText().toString();
Toast.makeText(LeadManagement.this, mobile, Toast.LENGTH_LONG).show();
/*ArrayList<String> leaddetails=mDbHelper.getTableDetails("lead_table", mobile);
prefEditor.putBoolean("leadbundle", true);
prefEditor.commit();
startActivity(new Intent(LeadManagement.this,CreateLead.class).putExtra("leadlist", leaddetails));*/
}
};
but it is showing an error!! how can i do it?
Don't register an onClickListener as a class member, but rather as an anonymous object. Something like:
Button tr = // your button corresponding to N-th row
final RowData rowData = ... // get the data from row N
tr.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
someMethodCall(rowData)
}
});
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();
}
});
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
}
});