问题描述
我有一个从数据库检索数据并将其显示在列表视图中的应用程序.我有一个相同的自定义适配器.因此,当我按下删除"按钮时,将为列表中的每一行显示一个删除按钮.如果按此键,则该特定行将在数据库中删除,并且该行也应反映在列表视图中.问题是,它没有反映出这种变化,我应该关闭该应用程序,然后重新打开或进行其他活动,然后返回以查看更新的结果.
I have an application which retrieves data from DB and displays it in a list view. I have a custom adapter for the same. So when I press the "delete" button, a delete button for each row in the list is displayed. If I press that, the particular row gets deleted in the DB and the same should be reflected in the listview also. The problem is, its not reflecting this change, I should close the app and reopen or move into some other activity and get back to see the updated results.
所以我的问题是:在哪里调用notifyDataSetChanged()
方法以使其立即更新?
So my question is: where do I call the notifyDataSetChanged()
method to get it updated instantly?
这是我的自定义适配器视图:
Here is my customadapter view:
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
MenuListItems menuListItems = menuList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.customlist, parent, false);
}
Button ck = (Button) convertView.findViewById(R.id.delbox);
if(i){
ck.setVisibility(View.VISIBLE);}
else{
ck.setVisibility(View.GONE);
}
ck.setTag(menuListItems.getSlno());
ck.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Integer Index = Integer.parseInt((String) v.getTag());
final DataHandler enter = new DataHandler(c);
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
enter.open();
enter.delet(Index);
enter.close();
notifyDataSetChanged();
dialog.dismiss();
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
dialog.dismiss();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(c);
builder.setMessage("Are you sure you want to Delete?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
});
TextView id = (TextView) convertView.findViewById(R.id.tvhide);
id.setText(menuListItems.getSlno());
TextView title = (TextView) convertView.findViewById(R.id.tvtitle);
title.setText(menuListItems.getTitle());
TextView phone = (TextView) convertView.findViewById(R.id.tvpnumber);
phone.setText(menuListItems.getPhone());
// ck.setChecked(menuList.)
notifyDataSetChanged();
return convertView;
}
推荐答案
您需要通过adapter.remove(adapter.getItem(position));
从列表视图的适配器中删除数据,然后在适配器上调用notifyDataSetChanged()
.
You need to delete the data from the adapter of the listview via adapter.remove(adapter.getItem(position));
and then call notifyDataSetChanged()
on adapter.
这篇关于android:从数据库中删除后,列表视图中的数据刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!