我有一个可扩展列表视图,它有一个删除按钮,首先从DATABASE中删除适配器。代码在模拟器上运行正常问题是当我点击“删除”按钮时,它只会删除列表中的最后一项。无论我点击哪个按钮。我希望它删除我点击的按钮的相应组项目。有人可以告诉我哪个是正确的rowId,以便从数据库中删除特定的组项?这是我的Button click listener的代码
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
Button delButton = (Button) convertView.findViewById(R.id.delButton);
delButton.setFocusable(false); // remove focus from the button
delButton.setOnClickListener( // del onclick listener
new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] columns = new String[] { testshop.TABLE_ROW_ID,
testshop.TABLE_ROW_ONE, testshop.TABLE_ROW_TWO };
Cursor cursor = testshop.db.query(testshop.TABLE_NAME, columns, null,
null, null, null, null);
cursor.moveToFirst();// move the cursor to the first item
if (!cursor.isAfterLast()) {
do{
Long rowId = cursor.getLong(0);
String item_name = cursor.getString(1);
} while (cursor.moveToNext());}
}
//delete from group array elements
_listDataHeader.remove(item_name);
notifyDataSetChanged(); //notify of data change in array
testshop.deleteRow(testshop.TABLE_NAME,
testshop.TABLE_ROW_ID, Long.toString(rowId)); //delete from database
//I'm using this toast to debug
Toast.makeText(_context, "Sucessfully Deleted" +rowId + getdeletedString(rowId),
Toast.LENGTH_SHORT).show();
}
});