Android 如何从tableview中删除记录?

Android 如何从tableview中删除记录?,android,sqlite,tableview,Android,Sqlite,Tableview,我从表中提取数据。但当用户单击记录或行时,我需要将其删除,但 当我点击2号位置记录时,它会删除3号位置记录。。但是我需要删除 用户单击的记录。请给我一些提示或指导。 谢谢.. 这是我的示例代码 public class MyTable extends Activity { int counter=0; MySQLiteHelper m=new MySQLiteHelper(this); @Override protected void onCreate(Bundle savedInstan

我从表中提取数据。但当用户单击记录或行时,我需要将其删除,但
当我点击2号位置记录时,它会删除3号位置记录。。但是我需要删除
用户单击的记录。请给我一些提示或指导。 谢谢..
这是我的示例代码

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

            }
        });
公共类MyTable扩展活动{
int计数器=0;
MySQLiteHelper m=新的MySQLiteHelper(此);
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.locprofile);
//读取所有联系人
final MySQLiteHelper m=新的MySQLiteHelper(getBaseContext());
最终列表LocWiseProfile=m.getAllLocWiseProfile();
对于(最终LocWiseProfileBeans cn:LocWiseProfile){
//获取TableLayout的引用
TableLayout table=(TableLayout)findviewbyd(R.id.locprofile_table);
//创建一个新的TableRow
TableRow row=新的TableRow(本);
//把计数器向上数一
计数器++;
字符串log=“Loc Name:”+cn.getLocname()+”,latitude:“+cn.getlatitude()+”,经度:“+cn.getLongitude()+”,所选配置文件:“+cn.getSelectedprofile()+”id:“+cn.getId();
TextView t=新的TextView(此);
//最终int Id=cn.Id;
//将文本设置为“文本xx”
t、 setText(cn.getLocname());
TextView t2=新的TextView(此);
t2.setText(cn.getSelectedprofile());
row.setTag(counter);//为标记使用计数器或索引,以便以后可以从LocWiseProfile获取数据
row.setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
试一试{
int标记=(整数)v.getTag();
LocWiseProfileBeans cn=LocWiseProfile.get(标记);
int value=cn.getId();
m、 删除(值);
deleteMessage();
}捕获(例外e){}
/*LocWiseProfileBeans lc=新的LocWiseProfileBeans();
int标记=(整数)lc.getId();
LocWiseProfileBeans值=LocWiseProfile.get(标记);
//if(value.Id);
m、 删除(value.Id);
deleteMessage()*/
}
});

你可以把这一行放在末尾

table.removeView(v);

你可以把这条线放在最后

table.removeView(v);

因为在设置为行上的标记之前,要递增
计数器
变量。
因此,第1行有一个位置1,这可能是删除错误行的原因。

因为在设置为行上的标记之前,您正在递增
计数器
变量。
因此,第1行有一个位置1,这可能是删除错误行的原因。

与其在onCreate()中编写动态创建TableLayout的代码,不如创建一个单独的方法并从onCreate()调用它

在该方法中,第一行是: TableLayout table=(TableLayout)findviewbyd(R.id.locprofile_table)

在for循环外的上面一行写入。第二行是:

table.removeAllViews()


因此,当您删除任何行时,只需再次调用被取消的方法(与onCreate()中的方法相同)…

而不是在onCreate()中编写动态创建TableLayout的代码,创建一个单独的方法并从onCreate()中调用它

在该方法中,第一行是: TableLayout table=(TableLayout)findviewbyd(R.id.locprofile_table)

在for循环外的上面一行写入。第二行是:

table.removeAllViews()


因此,当您删除任何行时,只需再次调用被取消的方法,就像在onCreate()中一样…

您如何创建行并将其添加到
TableView
,请显示代码。@AdilSoomro:检查此代码!您如何创建行并将其添加到
TableView
,请显示代码。@AdilSoomro:检查此代码!