0

我正在尝试从列表视图和适配器以及数据库中删除一行。这是我的 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);
    }

}
4

3 回答 3

0

Dont add the model object of _WebMsgCore in the holder.imgDelete.setTag(_WebMsgCore); in getView() Method. In place of that add position in the holder.imgDelete.setTag(position) and in onClick() callback method get the position from the Tag and remove the item from the position which you received and do call notifyDataSetChanged() of an adapter.

Hope this will resolve your problem.

于 2012-05-24T07:08:47.083 回答
0

您应该为 convertView 实现 seton onclick 侦听器,

convertView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    _WebMsgList.remove(position);   
    WebMessageAdapter.this.notifyDataSetChanged();
}

});

于 2012-05-24T07:58:58.010 回答
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();
    }

});
于 2012-05-24T06:53:19.010 回答