我正在尝试从listview和适配器以及数据库中删除一行。这是我的WebMessageAdapter.java类,我在其中调用adapter.notifyDataSetChanged();但它没有刷新列表。当我从其他活动回来时,列表再次填充刚刚删除的数据。我不明白我错过了什么或在哪里..?
感谢。
package com.utility;
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.activity.ContactInfoActivity;
import com.activity.MYApplication;
import com..activity.R;
import com.database.WebMessageCore;
public class WebMessageAdapter extends ArrayAdapter<WebMessageCore> implements
OnClickListener {
Context _Context;
ArrayList<WebMessageCore> _WebMsgList;
WebMessageCore _WebMsgCore;
TextView _MsgContent;
private LayoutInflater mInflater;
ImageView Arrow;
ImageView imgDel;
MYApplication application;
WebMessageCore selectedWebMsgCore;
public WebMessageAdapter(Context context, int resource,
ArrayList<WebMessageCore> contactList) {
super(context, resource, contactList);
_Context = context;
_WebMsgList = contactList;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return _WebMsgList.size();
}
public WebMessageCore getItem(int position) {
return _WebMsgList.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.name);
holder.txtmessage = (TextView) convertView
.findViewById(R.id.meassage);
holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);
holder.txtdate = (TextView) convertView.findViewById(R.id.datetime);
holder.Arrow = (ImageView) convertView
.findViewById(R.id.settingArrow);
holder.imgDelete = (ImageView) convertView.findViewById(R.id.dlet);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
_WebMsgCore = _WebMsgList.get(position);
holder.txtName.setText(_WebMsgCore.getName());
holder.txtmessage.setText(_WebMsgCore.getMessage());
holder.txtPhone.setText(_WebMsgCore.getMobileNo());
holder.txtdate.setText(_WebMsgCore.getRecDate());
holder.imgDelete.setTag(_WebMsgCore);
holder.imgDelete.setOnClickListener(this);
holder.Arrow.setTag(_WebMsgCore);
holder.Arrow.setOnClickListener(this);
return convertView;
}
static class ViewHolder {
ImageView Arrow;
TextView txtName;
TextView txtmessage;
TextView txtPhone;
TextView txtdate;
ImageView imgDelete;
}
public WebMessageCore getSelectedWebMsgCore() {
return selectedWebMsgCore;
}
public void setSelectedWebMsgCore(WebMessageCore selectedWebMsgCore) {
this.selectedWebMsgCore = selectedWebMsgCore;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dlet:
WebMessageCore tempWebMsgCore = (WebMessageCore) v.getTag();
_WebMsgList.remove(tempWebMsgCore);
adapter.notifyDataSetChanged();
break;
case R.id.settingArrow:
callContactInfoActivity(v);
break;
default:
break;
}
}
private void callContactInfoActivity(View v) {
WebMessageCore tempWebMsgCore = (WebMessageCore) v.getTag();
WebMessageAdapter.this.setSelectedWebMsgCore(tempWebMsgCore);
Intent newActivity = new Intent(_Context, ContactInfoActivity.class);
newActivity.putExtra("id", tempWebMsgCore.getId());
newActivity.putExtra("name", tempWebMsgCore.getName());
newActivity.putExtra("message", tempWebMsgCore.getMessage());
newActivity.putExtra("mobilenumber", tempWebMsgCore.getMobileNo());
_Context.startActivity(newActivity);
}
}
答案 0 :(得分:0)
更改为:
WebMessageAdapter.this.notifyDataSetChanged();
修改强>
删除public void onClick(View v) {}
,然后更改为:
holder.imgDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
_WebMsgList.remove(position);
WebMessageAdapter.this.notifyDataSetChanged();
}
});
答案 1 :(得分:0)
不要在_WebMsgCore
方法的holder.imgDelete.setTag(_WebMsgCore);
中添加getView()
的模型对象。取而代之的是在position
和holder.imgDelete.setTag(position)
回调方法中添加onClick()
,从标记中获取位置,并从您收到的item
中移除position
并调用适配器的notifyDataSetChanged()
。
希望这能解决您的问题。
答案 2 :(得分:0)
你应该为convertView实现seton onclick listener,
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
_WebMsgList.remove(position);
WebMessageAdapter.this.notifyDataSetChanged();
}
});