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
}
});
Related
I am doing bubble display of selected contacts. Each telephone bubble is a LinearLayout, which contains ImageView and TextView. These bubbles are then displayed in another LinearLayout which is child of HorizontalScrollView.
It child/parent tree looks like this:
- HorizontalScrollView
|- LinearLayout (id="#+id/telField")
|- LinearLayout (id="#+id/telBox") <- is programmatically added to parent
|- TextView (id="#+id/telNumber")
|- ImageView (id="#+id/delNumber")
In my .java class I call this method to display "telBox" LinearLayout in "telField" LinearLayout:
public void createAdapter() {
telList = new ArrayAdapter<>(this, R.layout.text_buble, R.id.telNumber, telNumList);
telField = (LinearLayout) findViewById(R.id.telField);
telField.removeAllViews();
final int adapterCount = telNumList.size();
for (ik = 0; ik < adapterCount; ik++) {
final View item = telList.getView(ik, null, null);
telField.addView(item);
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
telField.removeView(item);
telNumList.remove(ik-1);
telList.notifyDataSetChanged();
refresh();
}
});
}
}
Method refresh(); – is custom method which helps to "reload" Activity: it gets App values, refreshes warning ImageViews and cals createAdapter() method.
Big button "SELECT" calls an Intent which returns a selected phone number from a contacts book. I call this code to update my LinearLayout with id "telField":
telNumList.add(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)));
createAdapter();
Problem I face is:
After I click on LinearLayout with id "telNumber" it one by one deletes every bubble (no matter which I clicked) until it reaches first added bubble. It also crashes 50/50 when reaches first added element, I have not figured out a dependency. The error it returns is "out of bounds error", so I think it is connected with ik - 1 line.
My question is: How do I better construct my ArrayAdapter?
In your code you are trying to remove the view by ik which is getting change continuously because of which your coding is removing last view I have modified your code as given below
for (ik = 0; ik < adapterCount; ik++) {
final int position=ik;
final View item = telList.getView(ik, null, null);
telField.addView(item);
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
telField.removeView(item);
telNumList.remove(position);
telList.notifyDataSetChanged();
//refresh();
createAdapter();
}
});
}
Here position will help you to remove the particular view which you want to remove. I hope this is what you are asking for.
Inside your for loop, write these line of code:
for (ik = 0; ik < adapterCount; ik++) {
final View item = telList.getView(ik, null, null);
item.setTag(ik);
telField.addView(item);
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int index = Integer.parseInt(v.getTag().toString());
telNumList.remove(index);
refresh();
}
});
}
Hope it will help you out.
We make listener for Items(Rows) click listener in ListView. How to get know, in a particular row a specific View get clicked?
R1 --> TextView1 | TextView2
R2 --> TextView1 | TextView2
I can get which row is clicked. but i also want to know which View of the row get clicked.
You need to set position as tag to textView, and then onClick you can receive its tag.
So basicly you set textView's staff on adapter's getView method
textView.setTag(position);
textView.setOnClickListener(listener);
And then you'll have click listener like this
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
int position = Integer.parseInt(v.getTag().toString());
// Do whatever you like...
}
};
You can add specific click handlers to the textViews:
<TextView
android:onClick="clickHandler">
Then in your activity:
public void clickHandler(View v) {
// obtain parent row
LinearLayout parentView = (LinearLayout) v.getParent();
// with the parentView you can get all other views as well:
TextView textView1 = (TextView) parentRow.getChildAt(0);
}
I do it like this:
assign onClickListener to whatever can be clicked
I assign tag to it to identify v.setTag(R.id.itemId, item.id);
In onclick I do:
#Override
public void onClick(View v) {
final int id = (Integer) v.getTag(R.id.itemId);
}
I am populating a table dynamically from a string array.Each row of the table also has a plus and minus button to increment/decrement the value of one column. These buttons are also dynamically created like in the code below. Here how can I detect the exact button upon clicking. i.e; if I click on the '+' button of the 2nd row, how can I get the id of the button clicked for further processing.
plusButton= new Button(this);
minusButton= new Button(this);
createView(tr, tv1, names[i]);
createView(tr, tv2, (String)(names[i+1]));
minusButton.setId(i);
minusButton.setText("-");
minusButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
plusButton.setId(i);
plusButton.setText("+");
plusButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));`
You can set an onClickListener listener for each button. Use the id of the button from view.getId() method on your onClick() method to identify the button click.
You can add separate listeners for each button like here (assuming that the id you are setting for each button corresponds to a row)
minusButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
// Do some operation for minus after getting v.getId() to get the current row
}
}
);
Edit:
I am assuming your code is like this. Correct me if there is a deviation.
Button minusButton = null;
for(int i = 0; i < rowCount; i++)
{
minusButton = new Button(this);
minusButton.setId(i);
// set other stuff and add to layout
minusButton.setOnClickListener(this);
}
Let your class implement the interface View.OnClickListener and implement the onClick() method.
public void onClick(View v){
// the text could tell you if its a plus button or minus button
// Button btn = (Button) v;
// if(btn){ btn.getText();}
// getId() should tell you the row number
// v.getId()
}
You could do with tags: minusButton.setTag("-") and plusButton.setTag("+").
In your clickListener just get it from your button with view.getTag().
Then switch between your actions comparing the string tag.
Edit:
ID's "should" be unique. The setTag() method may help you if setId() doesn't work for you.
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 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();
}