0

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); 
    } 

}
Shaista Naaz
  • 8,281
  • 9
  • 37
  • 50
  • Once you get this working, you should probably consider using a List View which can automate how you deal with your cursor. – Matthew Mar 14 '11 at 07:21

1 Answers1

2

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.

Community
  • 1
  • 1
Matthew
  • 44,826
  • 10
  • 98
  • 87
  • oh really so nice of you. let me just try it and get back to you. Thanks – Shaista Naaz Mar 13 '11 at 21:04
  • @Mathew, what I see here is that I can use deleteButton.setTag(rowId) but the getTag returns Object, but I will need an integer, which is nothing but my rowId. Please excuse me if it is very basic stuff. – Shaista Naaz Mar 13 '11 at 21:20
  • just use a cast: int rowid = (Integer) button.getTag(); – Matthew Mar 13 '11 at 21:24
  • Actually I am sorry I had to use long and not integer but I am not able to typecast Object to long. when i use integer in place of long i get nulpointerException public void onClick(View v) { db.deleteTitle((Integer)delButton.getTag()); } //...deletes the particular user data... public boolean deleteTitle(long rowId) { return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; } – Shaista Naaz Mar 13 '11 at 21:49
  • are you sure you've done `setTag(rowId)` on all of your delete buttons? – Matthew Mar 13 '11 at 21:53
  • also if you need a `long` you should probably use (Long) as your cast. – Matthew Mar 13 '11 at 21:54
  • long rowId=0; {//some condition delButton.setTag(rowId++);} //on click....-> db.deleteTitle((Long)delButton.getTag()); //declaration....-> public boolean deleteTitle(long rowId) { return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; } – Shaista Naaz Mar 13 '11 at 22:07
  • Try: Long rowId = (Long) delButton.getTag(); if (rowId != null) { deteleTitle(rowId); } – Matthew Mar 13 '11 at 22:19
  • Hi Mathew, I don't really understand what is the problem it crashes Long rowId = (Long) delButton.getTag()itself. I dont know why I am inflatting this button and then attaching a clicklistener – Shaista Naaz Mar 13 '11 at 22:43
  • 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));Button delButton = (Button)findViewById(R.id.Delete_Name); delButton.setTag(rowId++);delButton.setOnClickListener(this);//then here -> public void onClick(View v) {Long rowId = (Long)delButton.getTag(); if (rowId != null) {db.deleteTitle(rowId); }} – Shaista Naaz Mar 13 '11 at 22:44
  • Is it giving a class cast exception? You have to get the casting right... For example, it may have to be Integer rowId = (Integer) ... – Matthew Mar 13 '11 at 22:58
  • My casting is perfect I believe as you can see the code I have provided above. I am afraid if I am using setTag()/getTag() in proper way as I found this link but still not sure about it. http://stackoverflow.com/questions/2434988/android-view-gettag-settag-and-illegalargumentexception – Shaista Naaz Mar 14 '11 at 04:38
  • Could you update the question with your code, so that it is formatted and readable? – Matthew Mar 14 '11 at 04:48
  • Hi Mathew, I am now able to get the tag without crash but still this does not solve the purpose. because while I am setting the tag I am doing it on the same button Button delButton = (Button)findViewById(R.id.Delete_Name); delButton.setTag(RowId++); inside a loop and when it comes out it. on doing getTag it returns me the last id. I think i need to think about the logic. – Shaista Naaz Mar 14 '11 at 05:02
  • Are you sure you aren't fetching the same button each time? If you use the same id then you will get the same button (not each button, as you're intending) In other words, your cannot use R.id.Delete_Name in your loop. – Matthew Mar 14 '11 at 05:14
  • ya i got it now. I am able to get the tag but eventually that tag is only one item after the complete loop iterates not 30 items as my database has 30 entries. I have to take some other alternative, but yes Mathew thanks for guiding me with set Tag and get Tag. I did not know about it now. Now may be I will have to get the row id on table row by clicking donno is it is possible. – Shaista Naaz Mar 14 '11 at 06:10
  • That's what I'm saying: you are overwriting the tag over and over again on the same button (with R.id.Delete_Name) – Matthew Mar 14 '11 at 06:19
  • Yes you are right. I will have to capture click event on Table row now. If this happens then my problem will be solved. – Shaista Naaz Mar 14 '11 at 06:19
  • You could also grab the delete button by doing rowView.findViewById(R.Delete_Name). That way you are limiting your search to just hat row. – Matthew Mar 14 '11 at 06:28
  • but my rows are getting added dynamically. here rowView you mean table rows right where I am adding these buttons ? – Shaista Naaz Mar 14 '11 at 06:34
  • Sure, wherever you are adding the rows you can do row.findViewById(...) – Matthew Mar 14 '11 at 06:37
  • The moment I am doing Button delButton = (Button)rowView.findViewById(R.id.Delete_Name); it crashes I think it is because I am inflating button from different xml and table row from different. what do you think? – Shaista Naaz Mar 14 '11 at 07:10
  • Is the button attached to the view hierarchy at that moment? – Matthew Mar 14 '11 at 07:14
  • yes the moment i inflate it gets attached as you can see here LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View inflated = inflater.inflate(R.layout.name, myTableLayout); Button delButton = (Button)rowView.findViewById(R.id.Delete_Name); – Shaista Naaz Mar 14 '11 at 07:17
  • Thank you so so much. It works and am sorry for troubling you. – Shaista Naaz Mar 14 '11 at 07:29