android的notifyDataSetChanged注释掉照样可以更新数据集

plugin 发布于 2015/01/12 18:01
阅读 747
收藏 0
package com.dong.adapter;

import java.util.List;

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

import com.dong.domain.UserInfo;
import com.example.loadlistview.R;

public class MyAdapter extends BaseAdapter {
	private List<UserInfo> list;
	private Context context;
	public MyAdapter(Context context, List<UserInfo> list) {
		this.list = list;
		this.context = context;
	}
	
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		UserInfo info = list.get(position);
		ViewHolder holder;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = View.inflate(context, R.layout.view,	null);
			holder.tv_name = (TextView) convertView.findViewById(R.id.name);
			holder.tv_password = (TextView) convertView.findViewById(R.id.password);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.tv_name.setText(info.getName());
		holder.tv_password.setText(info.getPassword());
		return convertView;
	}

	class ViewHolder{
		TextView tv_name;
		TextView tv_password;
	}

	public void onDataChange(List<UserInfo> userinfos) {
		list = userinfos;
		//notifyDataSetChanged();
	}
}


package com.example.loadlistview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

public class MyListView extends ListView implements OnScrollListener{
	private View footer_layout;
	private int totalItemCount;
	private int lastItemCount;
	private boolean isLoading;
	
	public MyListView(Context context) {
		super(context);
		initView(context);
	}

	public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initView(context);
	}

	public MyListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}
	
	private void initView(Context context){
		LayoutInflater inflater = LayoutInflater.from(context);
		footer_layout = inflater.inflate(R.layout.my_list_view, null);
		footer_layout.findViewById(R.id.footer_load).setVisibility(View.GONE);
		addFooterView(footer_layout);
		setOnScrollListener(this);
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if(lastItemCount == totalItemCount && scrollState == SCROLL_STATE_IDLE){
			if(!isLoading){
				isLoading = true;
				footer_layout.findViewById(R.id.footer_load).setVisibility(View.VISIBLE);
				//请求数据
				//isLoading=false
				iOnLoadDataListener.onLoad();
			}
		}
	}
	public void loadComplete(){
		isLoading = false;
		footer_layout.findViewById(R.id.footer_load).setVisibility(View.GONE);
	}
	
	
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		this.lastItemCount = firstVisibleItem + visibleItemCount;
		this.totalItemCount = totalItemCount;
	}

	public void setOnLoadDataListener(IOnLoadDataListener iOnLoadDataListener){
		this.iOnLoadDataListener = iOnLoadDataListener;
	}
	
	public interface IOnLoadDataListener{
		public void onLoad(); 
	}
	
	IOnLoadDataListener iOnLoadDataListener;
	
}



package com.example.loadlistview;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.dong.adapter.MyAdapter;
import com.dong.domain.UserInfo;
import com.example.loadlistview.MyListView.IOnLoadDataListener;

public class MainActivity extends Activity implements IOnLoadDataListener{
	private MyListView listView;
	private List<UserInfo> userinfos;
	MyAdapter myAdapter;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		listView = (MyListView) findViewById(R.id.list);
		userinfos = new ArrayList<UserInfo>();
		getData();
		
		myAdapter = new MyAdapter(this, userinfos);
		listView.setOnLoadDataListener(this);
		listView.setAdapter(myAdapter);
	}
	
	private void show_listView(List<UserInfo> userinfos){
		if(myAdapter == null){
			listView = (MyListView) findViewById(R.layout.my_list_view);
			listView.setOnLoadDataListener(this);
			myAdapter = new MyAdapter(this, userinfos);
			listView.setAdapter(myAdapter);
			
		}else{
			myAdapter.onDataChange(userinfos);
		}
	}

	@Override
	public void onLoad() {
		loadData();
		show_listView(userinfos);
		listView.loadComplete();
	}
	
	public void loadData(){
		for(int i = 0; i < 10; i++){
			userinfos.add(new UserInfo("newName" + i, "newPassword" + i));
		}
	}

	
	public void getData(){
		for(int i = 0; i < 10; i++){
			userinfos.add(new UserInfo("name" + i, "password" + i));
		}
	}
}



如题。实在不解。


加载中
返回顶部
顶部