Sandip Armal Patil
Sandip Armal Patil

Reputation: 5905

how to delete record from tableview?

I fetch data in table. but i need to delete it when user click on record or row but
when i click on number 2 position record it delete 3 re record.. but i need to delete
record in which user click. pls provide me some hint or tutorial. thank you..
here is my sample code.

 public class MyTable extends Activity {

int counter=0;
 MySQLiteHelper m=new MySQLiteHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.locprofile);



      // Reading all contacts
    final MySQLiteHelper m=new MySQLiteHelper(getBaseContext());
    final List<LocWiseProfileBeans> LocWiseProfile= m.getAllLocWiseProfile();       

    for (final LocWiseProfileBeans cn : LocWiseProfile) {
        // get a reference for the TableLayout
        TableLayout table = (TableLayout) findViewById(R.id.locprofile_table);

        // create a new TableRow
        TableRow row = new TableRow(this);

        // count the counter up by one
        counter++;


        String log = "Loc Name: "+cn.getLocname()+" ,Lattitude: " + cn.getLattitude()+ " ,Longitude: " + cn.getLongitude()+ " , Selected Profile :"+cn.getSelectedprofile()+"id:"+cn.getId();
        TextView t = new TextView(this);

       //final int Id=cn.Id;
        // set the text to "text xx"
        t.setText(cn.getLocname());

        TextView t2 = new TextView(this);
        t2.setText(cn.getSelectedprofile());



        row.setTag(counter);  //  use counter or index for tag, so you can get the data from LocWiseProfile later
        row.setOnClickListener(new View.OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {
                try{
                int tag = (Integer)v.getTag();
                LocWiseProfileBeans cn = LocWiseProfile.get(tag);
                    int value=cn.getId();
                    m.delete(value);
                    deleteMessage();

                }catch(Exception e){}

                /*LocWiseProfileBeans lc=new LocWiseProfileBeans();
                int tag=(Integer)lc.getId();
                LocWiseProfileBeans value=LocWiseProfile.get(tag);
                //if(value.Id);
                m.delete(value.Id);
                deleteMessage();*/

            }
        });

Upvotes: 0

Views: 219

Answers (3)

Mohanish
Mohanish

Reputation: 469

Instead of writing your code of creating TableLayout dynamically in onCreate(),make a separate method and call it from onCreate()...

In that method, first line will be : TableLayout table = (TableLayout) findViewById(R.id.locprofile_table);

write above line outside of for loop. Second line will be :

table.removeAllViews();

So When you delete any row, just call the method again which you were called off as same as in onCreate().....

Upvotes: 1

Dharmendra
Dharmendra

Reputation: 33996

Because you are incrementing the counter variable before setting as a tag on a row. So you there is a position 1 for row 1 and probably this is the reason for deleting the wrong row.

Upvotes: 2

Adil Soomro
Adil Soomro

Reputation: 37729

you can put this line at the end.

table.removeView(v);

Upvotes: 1

Related Questions