Reputation: 8321
I have a list, this list is basically TableRows in scrollview which I have inflated with layout inflater, i.e. it depends at runtime, how many rows are being added. Now I have a button also attached in each row. This button is either a delete button or an edit button. Before programming I was thinking that I will just get the row id and call my delete function for that row id. But now when I program I see that I do not find a way to get the row id, because the button is just the same button always and how will button come to know with what row exactly it is linked to. I am basically doing database programming where I have taken lots of user input and displayed it in a table row inside a scroll view. But I don't know how do I get those rows. As of now I am not using any array list or array adapter. Do I need to use them either to solve my this problem ?
Please help.
I am entering the my code here
if(dbExists)
{
myName = (TextView)findViewById(R.id.myName);
db.open();
Cursor c = db.getAllTitles();
long NoOfRows = c.getCount(); //here I am gettin 30 as entered in database
while(NoOfRows >= 1)
{
c.moveToFirst();
//.........Inflate here name and number........
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflated = inflater.inflate(R.layout.name, myTableLayout);
TextView userName = (TextView)findViewById(R.id.myName);
userName.setText(c.getString(1));//here I am settng the user entered name
Button delButton = (Button)rowView.findViewById(R.id.Delete_Name);
delButton.setTag(RowId--);
delButton.setOnClickListener(this);
c.moveToNext();
NoOfRows--;
}
db.close();
}
@Override
public void onClick(View v) {
Long rowId = (Long)v.getTag();
if (rowId != null)
{
Toast.makeText(this, "rows get Tag() " + rowId, Toast.LENGTH_LONG).show();
//db.deleteTitle(rowId);
}
}
Upvotes: 0
Views: 783
Reputation: 44919
You can use setTag
/getTag
on a View so set and get special information for that view.
Using this you would call button.setTag(rowId)
and then retrieve it later using getTag
in your onClick
method.
Here is a detailed answer with a code example.
Upvotes: 2