sqlite和listview联合使用


public class MainActivity extends Activity {
private View footer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

initView();
}

private void initView() {
ListView lv = (ListView) findViewById(R.id.lv);
OtherFishService ofs = new OtherFishService(this);

/* 
这里调用数据库,需要查询得到数据,如果数据量很大,就很不好,所以需要优化
所以用到simplecursoradapter 作用 :仅加载需要显示的数据,性能好,还可以自定义cursoradapter
List fishs = ofs.queryAll();
MyBaseAdapter adapter = new MyBaseAdapter(this, fishs);

lv.setAdapter(adapter);*/

/* Cursor c = ofs.getCursor();
//简单游标适配器
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
R.layout.item,
c,
new String[]{"_id","name","money"},
new int[]{R.id.tv_id,R.id.tv_name,R.id.tv_money});
lv.setAdapter(adapter);
*/

//给ListView添加Footer
footer = getLayoutInflater().inflate(R.layout.footer, null);
lv.addFooterView(footer);
//设置控件的显示和隐藏 GONE 隐藏并且不占据空间 INVISIBLE 隐藏占据空间 VISIBLE 显示
footer.setVisibility(View.GONE);

Cursor c = ofs.getCursor();
MyCursorAdapter adapter = new MyCursorAdapter(this, c);
lv.setAdapter(adapter);

//添加滑动监听事件
lv.setOnScrollListener(new MyOnScorllListener());
}

private class MyOnScorllListener implements OnScrollListener{

//ListView的状态改变 idel --> scroll--> fling(很快的滑动)
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub

}

/**
* AbsListView view, ListView 
* int firstVisibleItem, listView屏幕控件的第一个条目的id 
int visibleItemCount, 可见条目的总数
int totalItemCount 总共条目数
*/
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
Log.i("i", "firstVisibleItem:"+firstVisibleItem+",visibleItemCount:"+visibleItemCount+",totalItemCount:"+totalItemCount);
if(firstVisibleItem + visibleItemCount == totalItemCount){
//滑动到了最后 

//添加正在加载数据
footer.setVisibility(View.VISIBLE);
}else{
footer.setVisibility(View.GONE);
}

}

}

}


 

 

 

public class MyCursorAdapter extends CursorAdapter {
	
	private LayoutInflater mInflater;

	public MyCursorAdapter(Context context, Cursor c) {
		super(context, c);
		// TODO Auto-generated constructor stub
		mInflater = LayoutInflater.from(context);
	}

	//创建item
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = mInflater.inflate(R.layout.item, null);
		return view;
	}

	//把数据绑定给View
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		// TODO Auto-generated method stub
		
		//获取条目的位置
		int position = cursor.getPosition();
		if(position%2==0){
			view.setBackgroundColor(Color.RED);
		}else{
			view.setBackgroundColor(Color.YELLOW);
		}
		
        //获取控件
		TextView tv_id = (TextView) view.findViewById(R.id.tv_id);
		TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
		TextView tv_money = (TextView) view.findViewById(R.id.tv_money);
		
		//获取数据
		String _id = cursor.getString(0);
		String name = cursor.getString(1);
		String money = cursor.getString(2);
		
		//绑定数据
		tv_id.setText(_id);
		tv_name.setText(name);
		tv_money.setText(money);
	}

}


 

你可能感兴趣的:(Android入门)