Android | 2. Fragment中获取SQLite中数据并使用ListView显示

经过上一章,成功的向SQLite数据库中添加了数据,并且成功查询出其中数据,以比较简单的Log日志形式打印出来,以供我们查看。但大多数真实情况都是使用ListView来承载显示数据的,那么在Fragment之下我们又该如何操作呢。


1.首先在布局中添加一个listview控件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
     
    <ListView
        android:id="@+id/lv_diary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
     ></ListView>
 
</LinearLayout>

2.ListView Item的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
     
    <TextView android:id="@+id/tv_title" 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_weight="1"
        android:text="标题"
        android:textSize="20sp"/>
    
    <TextView android:id="@+id/tv_content"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_weight="1"
        android:text="内容"
        android:textSize="20sp"/>

</LinearLayout>

3.DisplayFragment

public class DisplayFragment extends Fragment { 
	
	TextView tv_id,tv_title,tv_content;
	ListView lv_diary;
	String TAG = "TAG";
	private SQLiteDatabase db;
	MySQLiteHelper helper;
	private ArrayList<DiaryInfo> listData;
	private DiaryAdapter adapter;
	
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.display_fragment, container, false); 
    	return view; 
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		tv_title = (TextView)getActivity().findViewById(R.id.tv_title);
		tv_content = (TextView)getActivity().findViewById(R.id.tv_content);
		
		lv_diary = (ListView)getActivity().findViewById(R.id.lv_diary);
		listData = new ArrayList<DiaryInfo>();
		//创建MySQLiteHelper实例
		helper = new MySQLiteHelper(getActivity(),"diary",null,1);
		//得到数据库
		db = helper.getWritableDatabase();
		//查询数据
		Cursor cursor= db.query("diary",null,null,null,null,null,null);
		while(cursor.moveToNext()){
			DiaryInfo diary = new DiaryInfo();
			diary.setTitle(cursor.getString(cursor.getColumnIndex("title")));
			diary.setContent(cursor.getString(cursor.getColumnIndex("content")));
			listData.add(diary);
		}		
		adapter = new DiaryAdapter(this.getActivity(),listData);
		lv_diary.setAdapter(adapter);	
		adapter.notifyDataSetChanged();						
	}
	
}

4.附上Info类,其中封装get set 是非常基础的啦:

package com.example.entity;

public class DiaryInfo{
	private int Id;
	private String Title;
	private String Content;
	
	public int getId(){
		return Id;
	}
	public void setId(int id){
		Id = id;
	}
	public String getTitle() {
		return Title;
	}
	public void setTitle(String title) {
		Title = title;
	}
	public String getContent() {
		return Content;
	}
	public void setContent(String content) {
		Content = content;
	}	
}

5.和使用SQLite密切相关的SQLiteOpenHelper:

其中就一个创建,一个升级,但俩方法非常基本和重要了~

public class MySQLiteHelper extends SQLiteOpenHelper{

	private Context context;
	
	public MySQLiteHelper(Context context, String name,
			CursorFactory factory, int version) {
		
		super(context, "Blossom.db", null, 1);
		this.context = context;
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("create table diary(id integer PRIMARY KEY AUTOINCREMENT," 
				+"title text,content text)");
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
		// TODO Auto-generated method stub
		db.execSQL("drop table if exists diary");
		onCreate(db);
	}
		
}

6.用ListView,肯定要用Adapter适配器啦~:

public class DiaryAdapter extends BaseAdapter{

	private Context context;
	private ArrayList<DiaryInfo> listData;
	
	
	public DiaryAdapter(Context context,ArrayList<DiaryInfo> listData) {  
    	this.context = context;
		this.listData = listData;
	
    }  

    @Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listData.size();
	}
	@Override
	public Object getItem(int pos) {
		// TODO Auto-generated method stub
		return listData.get(pos);
	}
	@Override
	public long getItemId(int pos) {
		// TODO Auto-generated method stub
		return pos;
	}

    @Override  
    public View getView(int pos, View view, ViewGroup arg2) {  
    	// TODO Auto-generated method stub
    	ViewHolder viewholder = null;
    	if(view == null){
    	LayoutInflater inflate=LayoutInflater.from(context);
    	view=inflate.inflate(R.layout.listview_display,null);//此处layout是listview item的布局,我的命名不规范!!!
    	viewholder = new ViewHolder();
    	viewholder.tv_title= (TextView)view.findViewById(R.id.tv_title);
    	viewholder.tv_content = (TextView)view.findViewById(R.id.tv_content);
    	view.setTag(viewholder);		
    }
    	viewholder = (ViewHolder)view.getTag();
    	DiaryInfo model = listData.get(pos);
    	viewholder.tv_title.setText(model.getTitle());
    	viewholder.tv_content.setText(model.getContent());
    	return view;
}  

	class ViewHolder {
	    private TextView tv_title;  
	    private TextView tv_content; 
	}  
}
以上,如果有任何问题,欢迎大家指导~~
阅读更多
个人分类: Android
上一篇Android | 1. Fragment中使用SQLite实现简单存储及查询
下一篇【转】Android | 学习Android的SQLite中增删改查功能!
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭