解决The content of the adapter has changed but ListView did not receive a notification的问题

</pre>遇到的问题主要是:ListView在动态更新的时候,有概率的出现这个问题。<p></p><p>原先的写法:</p><p><span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">listAdapter = new MerListAdapter(this,applist);</span></p><p><span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);"></span></p><pre name="code" class="java">for (int i = 0; i < mer_name.length; i++) {
						HashMap<String, Object> map = new HashMap<String, Object>();
						map.put("ItemText", mer_name[i]);// 按序号做ItemText
						applist.add(map);
					}
然后在Handler中

listAdapter.notifyDataSetChanged();

最后的解决办法:定义了 applist2,作为过渡的list

		applist2.clear();
					for (int i = 0; i < mer_name.length; i++) {
						HashMap<String, Object> map = new HashMap<String, Object>();
						map.put("ItemText", mer_name[i]);// 按序号做ItemText
						applist2.add(map);
					}
					
				for(int i = 0; i < applist2.size(); i++){
					applist.add(applist2.get(i));
				}
然后在 Handler 中

listAdapter.setDeviceList(applist);
listAdapter.notifyDataSetChanged();

下面是adapter

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.bolu.pos.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MerListAdapter extends BaseAdapter{


	private List<HashMap<String, Object>> mAppList;
	private LayoutInflater mlnflater;

	public MerListAdapter(Context context,
			List<HashMap<String, Object>> list) {
		this.mAppList = list;
		mlnflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return mAppList.size();
	}

	@Override
	public HashMap<String, Object> getItem(int position) {
		return mAppList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint({ "ViewHolder", "InflateParams" })
	@Override
	public View getView(int position, View contentView, ViewGroup view) {
			HashMap<String, Object> mVO = getItem(position);
			contentView = mlnflater.inflate(R.layout.listitem_mercharnt, null);
			TextView ItemText = (TextView) contentView
					.findViewById(R.id.mer_name);
			ItemText.setText(mVO.get("ItemText").toString());
			
		return contentView;
	}

	
	 @SuppressWarnings("unchecked")  
	    public void setDeviceList(ArrayList<HashMap<String, Object>> list) {  
	        if (list != null) {  
	        	mAppList = (ArrayList<HashMap<String, Object>>) list.clone();  
	            notifyDataSetChanged();  
	        }  
	    }  
	  
}
这样就解决了问题,主要的问题应该是,适配器加载的列表add多次,导致反应不过来,加入一个中间列表过渡,就可以正常了。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值