2012-03-09 205 views
0

我在表中獲取數據。但我需要刪除時記錄或行,但
用戶點擊,當我在2號位置記錄點擊刪除3再紀錄..但我需要刪除該用戶點擊
記錄。請提供一些提示或教程。 謝謝..
這裏是我的示例代碼。如何從tableview中刪除記錄?

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();*/ 

      } 
     }); 
+0

你如何創建行和增加'TableView',請顯示代碼。 – 2012-03-09 12:10:14

+0

@AdilSoomro:檢查此代碼! – 2012-03-09 12:12:45

回答

2

因爲要設置爲上一行標籤之前遞增counter變量。 所以你有第1行的位置1,可能這是刪除錯誤行的原因。

1

你可以把這條線放在最後。

table.removeView(v); 
1

而不是寫你的動態中的onCreate(創建TableLayout的代碼),做一個單獨的方法,並從的onCreate()調用它...

在該方法中,第一行是: TableLayout表=( TableLayout)findViewById(R.id.locprofile_table);

寫入上面的行的for循環之外。第二線將是:

table.removeAllViews();

所以,當你刪除的任何行,只需再次調用該方法,你被稱爲斷爲相同的onCreate().....