2014-02-22 86 views
0

我的Android应用程序遇到问题。问题是我无法从ListView或数据库中删除项目。我搜遍了Google,S0和YouTube,但找不到解决方案。这是我的ListView适配器:如何从ListView和数据库中删除项目

listContent.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(final AdapterView<?> parent, View view, int position, long id) { 
      int j = 0; 
      for (j = 0; j < parent.getChildCount(); j++) 
       parent.getChildAt(j).setBackgroundColor(Color.TRANSPARENT); 

      // change the background color of the selected element 
      view.setBackgroundColor(Color.LTGRAY); 

      final long hello = id; 
      Button button = (Button) getView().findViewById(R.id.clrselected); 

      button.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        dbAdapter.delete123456(hello); 
        adapter.notifyDataSetChanged(); 
        listContent.refreshDrawableState(); 
        listContent.invalidate(); 
       } 
      }); 

     } 
    }); 

在我的按钮OnClickListener,我试图从数据库中删除和ListView控件的值。这里是delete123456代码:

public boolean delete123456(long rowId) { 
    return mDb.delete(FTS_VIRTUAL_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 
} 

当我点击按钮,什么也没有发生。我不知道我做错了什么。任何有关这个问题的帮助将不胜感激。


编辑

这里是我的适配器类代码:

package com.example.foodsaver2; 

import android.content.Context; 
import android.database.Cursor; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.CursorAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 

public class CustomCursorAdapter extends CursorAdapter { 

public CustomCursorAdapter(Context context, Cursor c) { 
    super(context, c); 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    // when the view will be created for first time, 
    // we need to tell the adapters, how each item will look 
    LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
    View retView = inflater.inflate(R.layout.show_row_item, parent, false); 

    return retView; 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    // here we are setting our data 
    // that means, take the data from the cursor and put it in views 

    TextView textViewPersonName = (TextView) view.findViewById(R.id.tv_person_name); 
    textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1)))); 

    TextView textViewPersonPIN = (TextView) view.findViewById(R.id.tv_person_pin); 
    textViewPersonPIN.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2)))); 
} 

} 
+0

你能发表TABLE的声明吗 – bluewhile

+0

如果你使用的是自定义适配器,你可以发布这个函数:'public long getItemId(int position)' – Merlevede

+0

如果你使用的是'ListView'的游标适配器,该标签中的“view”标识符和删除标签是您实现目标的方式。 –

回答

0

我会张贴此作为一个答案,因为有限的评论空间。

适配器对列表视图中的每一行都有Idposition。在您使用的代码中:

final long hello = id; 
dbAdapter.delete123456(hello); 

通常情况下,位置就是表格中的位置。 Id由适配器中的用户设置。在使用数组时,通常位置和Id是相同的,但是在使用数据库时,Id是表中的一些内部引用。

所以我的猜测是,你使用的ID是错误的或没有设置。但是我无法从你发布的代码中看出来。也许你应该发布你的适配器的代码来进一步帮助。

+0

Merlevede,我发布了我的CursorAdapter代码。 – Rohodude

+0

当我将'hello'显示为吐司时,当我点击ListView中的第一个项目时,我得到0,当我点击ListView的第二个项目时,我得到0,等等。 – Rohodude

相关问题