English 中文(简体)
为何调适器. 通知DataSetSet Changed () (); 无效?
原标题:why adapter.notifyDataSetChanged(); is not working?

我试图从列表视图和适配器中删除一行, 并且从数据库中删除。 这里是我的 WebMessageAdapter.java 类, 我正在给该类的适配者打电话。 通知DataSetChanged () ; 但它不会刷新列表 。 当我从其它活动回来时, 列表中再次包含刚刚删除的数据 。 我不明白我错过了什么或者在哪里??

谢谢

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);
    }

}
问题回答

切勿在 deserences.imgDelete.setTag (_WebMsgCore)(_WebMsgCore); in getView () 方法中添加 position 的示范对象 (position) (position) 和 (code> onClick () 调回调方法中添加 position 的模型位置,并从您收到的 position 中删除 项目 (dcode > (node> -- -- -- dize DataSetSetChanged () a reconferver.

希望这能解决你的问题

更改为 :

WebMessageAdapter.this.notifyDataSetChanged();

<强度 > EDIT:

remove your public void onClick(View v) {}, and 更改为 :

holder.imgDelete.setOnClickListener(new OnClickListener() {

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

});

您应该执行 seton 点击收听器来转换 View,

convertView.setOnClickListener(new OnClickListener() {

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

});





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...