android 从 SQLite数据库取数据并展示到 listVIew中最简单的例子

文章标签: android,android-listview
2014-12-20 20:46:27     人阅读    

android 开发从sqlite数据库查询数据并展示到listview的例子
.

例子的具体代码如下:
自定义的 CursorAdapter 代码

public class ItemAdapter extends CursorAdapter {
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    public ItemAdapter(Context context, Cursor c) {
        super(context, c);
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
 
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = mLayoutInflater.inflate(R.layout.items_row, parent, false);
        return v;
    }



    @Override
 
    public void bindView(View v, Context context, Cursor c) {
        String title = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_TITLE));
        String date = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_DATE));
        String imagePath = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_IMG));
        String reminder = c.getString(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_REMINDER));
        int deletion = c.getInt(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_DELETION));
        int priority = c.getInt(c.getColumnIndexOrThrow(ItemDbAdapter.KEY_PRIORITY));

        /**
 


设置enery的标题
       

  */

        TextView title_text = (TextView) v.findViewById(R.id.item_text);
        if (title_text != null) {
            title_text.setText(title);
        }

        setPriorityColor(title_text, priority);

        /**
 
         * 设置日期
         */

        TextView date_text = (TextView) v.findViewById(R.id.item_date);
        if (date_text != null) {
            date_text.setText(date);
        }

        /**
 
       决定是否要显示回形针图标表示图像附件
         */

        ImageView item_image = (ImageView) v.findViewById(R.id.item_attachment);
        item_image.setVisibility(ImageView.INVISIBLE);
        if (imagePath != null && imagePath.length() != 0 && item_image != null) {
            item_image.setVisibility(ImageView.VISIBLE);
        }

        /**
 
         * 决定是否要显示删除指示
         */
        ImageView del_image = (ImageView) v.findViewById(R.id.item_deletion);
        del_image.setVisibility(ImageView.INVISIBLE);
        if (deletion == 1) {
            del_image.setVisibility(ImageView.VISIBLE);
        }

        /**
 
         * 决定是否要显示提醒指示灯
         */
        ImageView rem_image = (ImageView) v.findViewById(R.id.item_reminder);
        rem_image.setVisibility(ImageView.INVISIBLE);
        if(reminder != null && reminder.length() != 0 && rem_image != null) {
            rem_image.setVisibility(ImageView.VISIBLE);
        }
    }


    private void setPriorityColor(TextView title, int priority) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        Resources res = mContext.getResources();

        switch(priority) {
        case ItemDbAdapter.PRIORITY_HIGH:
            title.setTextColor(prefs.getInt("highColor", res.getColor(R.color.high_priority)));
            break;
        case ItemDbAdapter.PRIORITY_NORMAL:
            title.setTextColor(prefs.getInt("normColor", res.getColor(R.color.norm_priority)));
            break;
        case ItemDbAdapter.PRIORITY_LOW:
            title.setTextColor(prefs.getInt("lowColor", res.getColor(R.color.low_priority)));
            break;
        }
    }
}


XML配置文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/list_bg">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView android:id="@+id/item_text"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:lines="1"
            android:scrollHorizontally="true"
            android:ellipsize="end"
            android:paddingLeft="2sp"
            android:paddingTop="2sp"
            android:textSize="18sp"
            android:textStyle="bold"
            android:shadowColor="#90909090"
            android:shadowDx="1.0"
            android:shadowDy="1.0"
            android:shadowRadius="1.0"/>

        <TextView android:id="@+id/item_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:textColor="#FF808080"
            android:paddingLeft="2sp"
            android:paddingTop="2sp"/>
    </LinearLayout>

    <ImageView android:id="@+id/item_deletion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/deletion"
        android:visibility="invisible"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:paddingRight="5sp"/>

    <ImageView android:id="@+id/item_attachment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/attachment"
        android:visibility="invisible"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/item_deletion"/>

    <ImageView android:id="@+id/item_reminder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/alarm"
        android:visibility="invisible"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/item_attachment"/>

</RelativeLayout>


下面是Main Activity代码,我调用这个函数使用我的自定义适配器显示光标:

private void fillData(String sortOrder) {
    Cursor itemsCursor = mDbHelper.fetchAllItems(sortOrder);
    startManagingCursor(itemsCursor);

    ItemAdapter itemAdapter = new ItemAdapter(this, itemsCursor);
    setListAdapter(itemAdapter);
    itemAdapter = null;
}

main Activity 的xml配置文件  顶部相对布局的 action bar
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:background="@drawable/action_bar">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            android:textStyle="bold"
            android:shadowColor="#B0B0B0B0"
            android:shadowDx="2.0"
            android:shadowDy="2.0"
            android:shadowRadius="2.0"
            android:layout_centerVertical="true"
            android:paddingLeft="10dip"/>

        <ImageView
            android:drawable="@drawable/action_bar_add"
            android:id="@+id/action_bar_add"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:paddingRight="5dip"
            android:paddingLeft="5dip"
            android:src="@drawable/action_bar_add_anim"/>

        <View
            android:layout_height="fill_parent"
            android:layout_width="2px"
            android:layout_toLeftOf="@id/action_bar_add"
            android:background="#90909090"/>

</RelativeLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <TextView android:id="@+id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/no_items"
        android:textSize="20sp"
        android:textStyle="bold"
        android:shadowColor="#90909090"
        android:shadowDx="1.0"
        android:shadowDy="1.0"
        android:shadowRadius="1.0"/>

</LinearLayout>
</LinearLayout>


原文地址:http://www.itmmd.com/201412/350.html
该文章由 萌萌的IT人 整理发布,转载须标明出处。

CallLog.Calls按日期查询   上一篇
下一篇  android 开发,在按钮触发后想布局管理器中添加一个新的视图(TextView)
精彩回复
#1楼    ; [评论人]:哈哈   [发表时间]:2014-12-23 9:19:41

经典专业,快速解决了我的android开发问题

我要追加问题,请求站长解决!
姓名:       

《程序员app》专门为程序员量身定做!