我的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))));
}
}
你能发表TABLE的声明吗 – bluewhile
如果你使用的是自定义适配器,你可以发布这个函数:'public long getItemId(int position)' – Merlevede
如果你使用的是'ListView'的游标适配器,该标签中的“view”标识符和删除标签是您实现目标的方式。 –