adapter.notifyDataSetChanged()
;更新但不起作用。使用adapter.clear()
时,它可以正常工作并清除列表视图。
我将根据listview
。adapter.notifyDataSetChanged();
进行更新但不起作用。使用adapter.clear()
时,它可以正常工作并清除列表视图。listview
删除。我使用的是adapter.notifyDataSetChanged();更新但不起作用。使用adapter.clear()
时,它可以正常工作并清除list view
。< - spacer将以下代码保留在上面的项目符号列表中。 - >
holder.dlttxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Contact lContact = (Contact) list
.getItemAtPosition(position);
long positionid = list.getItemIdAtPosition(position);
Log.e("", "--position id---" + positionid);
Log.e("", "----deleted id--" + lContact.getID()
+ "--deleted name---" + lContact.getName());
db.deleteContact(lContact.getID());
quan = lContact.getName();
Toast.makeText(getContext(),
lContact.getName() + ":" + "Deleted Sucessfully",
Toast.LENGTH_SHORT).show();
Log.e("", "---array size--" + imageArry.size());
imageArry.remove(positionid);
/*adapter.clear();
adapter.addAll(db.getAllContacts());
*/
adapter.notifyDataSetChanged();
count = db.getContactsCount();
HelperActivity.num.setText(String.valueOf(count));
}
});
答案 0 :(得分:0)
我认为您可以删除适配器getview方法中的项目
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
-------------------------- inflating stuff etc.
holder.dlttxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Contact lContact = (Contact) list
.getItemAtPosition(position);
long positionid = list.getItemIdAtPosition(position);
Log.e("", "--position id---" + positionid);
Log.e("", "----deleted id--" + lContact.getID()
+ "--deleted name---" + lContact.getName());
db.deleteContact(lContact.getID());
quan = lContact.getName();
Toast.makeText(getContext(),
lContact.getName() + ":" + "Deleted Sucessfully",
Toast.LENGTH_SHORT).show();
Log.e("", "---array size--" + imageArry.size());
imageArry.remove(positionid);
/*adapter.clear();
adapter.addAll(db.getAllContacts());
*/
adapter.notifyDataSetChanged();
count = db.getContactsCount();
HelperActivity.num.setText(String.valueOf(count));
}
});
}
答案 1 :(得分:0)
** notifyDataSetChanged()只是调用此方法删除适配器的语句bc notifyDataSetChanged()是适配器的方法,你在侧面适配器的getView()方法**
**并且还有一件事情无法正常运行你必须使用标签来获取要删除的精确位置**
long positionid = list.getItemIdAtPosition(position);
答案 2 :(得分:0)
您可以使用interface
来处理delete
按钮的点击事件
代码段
interface
用于处理点击事件
public interface ListItemBtnClickInterface {
public void listItemBtnClickListener(Object obj, int viewId);
}
Adapter
public AddExpTypeAdapter(Context context, int resource,
List<ExpenseTypeInfo> objects, ListItemBtnClickInterface _interface) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
_context = context;
layoutId = resource;
dataList = objects;
this._interface = _interface;
dbHelper = new PortfolioDbHelper(context);
notifyDataSetChanged();
}
初始化后适配器中的Delete Button click
的代码
vieweHolder.deleteImgView.setTag(position);
vieweHolder.deleteImgView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Object data = v.getTag();
if(data != null) {
_interface.listItemBtnClickListener(data, v.getId());
}
}
});
在ListView
Activity
设置适配器的代码
addExpTypeAdapter = new AddExpTypeAdapter(AddExpTypeActivity.this,
R.layout.row_add_type, expTypeList, new ListItemBtnClickInterface() {
private int clickedPosition;
@Override
public void listItemBtnClickListener(Object obj, int viewId) {
// TODO Auto-generated method stub
clickedPosition = Integer.parseInt(obj.toString());
ExpenseTypeInfo expenseTypeInfo = expTypeList.get(clickedPosition);
final long expTypeId = expenseTypeInfo.getId();
switch (viewId) {
case R.id.deleteImgView:
new AlertDialog.Builder(AddExpTypeActivity.this)
.setTitle(getResources().getString(R.string.delete_exp_type_title))
.setIcon(R.drawable.delete_icon)
.setMessage(R.string.delete_exp_type_msg).setCancelable(true)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dbHelper.deleteExpenseType(expTypeId);
dbHelper.closeDB();
expTypeList.remove(clickedPosition);
addExpTypeAdapter.notifyDataSetChanged();
}
}).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
break;
default:
break;
}
}
});
typeListView.setAdapter(addExpTypeAdapter);
N.B: - 我正在使用ArrayAdapter
,请更改这些变量名称