SimpleAdapter,CursorAdapter,SimpleCursorAdapter

SimpleAdapter,CursorAdapter,SimpleCursorAdapter

SimpleAdapter,CursorAdapter,SimpleCursorAdapter三者的使用區(qū)別

布局文件

  • 布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.vv.zvv.SimpleAdapterActivity">

    <!--數(shù)據(jù)庫(kù)字段名-->
    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="id"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="name"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="age"/>
    </LinearLayout>

    <!--數(shù)據(jù)庫(kù)內(nèi)容-->
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/linearLayout">

    </ListView>
</RelativeLayout>
  • 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">

    <!--id-->
    <TextView
        android:id="@+id/tv_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="id"/>

    <!--姓名-->
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="name"/>

    <!--年齡-->
    <TextView
        android:id="@+id/tv_age"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="age"/>
</LinearLayout>

SimpleAdapter

  • 摘要
 SimpleAdapter simpleAdapter = new SimpleAdapter(
                                                   this,
                                                   getData(), 
                                                   R.layout.item_database,
                                                   new String[]{"_id", "name", "age"}, 
                                                   new int[]{R.id.tv_id, R.id.tv_name, R.id.tv_age});
  • 詳細(xì)使用
private void init() {
        //打開(kāi)數(shù)據(jù)庫(kù)
        File file = new File(Environment.getExternalStorageDirectory(), "my_database.db");
        SQLiteDatabase database = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);
        mCursor = database.query("table_person", new String[]{"_id", "name", "age"}, "age >= ?", new String[]{"0"}, null, null, null);
        //實(shí)例適配器
        SimpleAdapter simpleAdapter = new SimpleAdapter(this,getData(), R.layout.item_database,
                new String[]{"_id", "name", "age"}, new int[]{R.id.tv_id, R.id.tv_name, R.id.tv_age});
        //綁定適配器
        mListView.setAdapter(simpleAdapter);
    }

    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<>();

        int idIndex = mCursor.getColumnIndex("_id");
        int nameIndex = mCursor.getColumnIndex("name");
        int ageIndex = mCursor.getColumnIndex("age");

        while (mCursor.moveToNext()) {
            int id = mCursor.getInt(idIndex);
            String name = mCursor.getString(nameIndex);
            int age = mCursor.getInt(ageIndex);

            Map<String ,Object> map = new HashMap<>();
            map.put("_id",id);
            map.put("name",name);
            map.put("age",age);

            list.add(map);
        }
        mCursor.close();
        return list;
    }

SimpleCursorAdapter

  • 摘要
 SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(
                this, 
                R.layout.item_database, 
                cursor, 
                new String[]{"_id","name", "age"}, 
                new int[]{R.id.tv_id,R.id.tv_name, R.id.tv_age},
               CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
  • 詳細(xì)使用
private void init() {
        File file = new File(Environment.getExternalStorageDirectory(), "my_database.db");
        SQLiteDatabase database = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);
        Cursor cursor = database.query("table_person", new String[]{"_id", "name", "age"}, "age >= ?", new String[]{"0"}, null, null, null);
        // flags:FLAG_REGISTER_CONTENT_OBSERVER,官方推薦使用的標(biāo)簽实苞,當(dāng)數(shù)據(jù)發(fā)生變化時(shí),通過(guò)觀察者標(biāo)簽烈疚,實(shí)時(shí)觀察數(shù)據(jù)的變化黔牵,并刷新UI界面
        SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(
                this, R.layout.item_database, cursor, new String[]{"_id","name", "age"}, new int[]{R.id.tv_id,R.id.tv_name, R.id.tv_age}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        mListView.setAdapter(simpleCursorAdapter);
    }

CursorAdapter

  • 摘要
 MyCursorAdapter adapter = new MyCursorAdapter(this, cursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
  • 自定義MyCursorAdapter
public class MyCursorAdapter extends CursorAdapter {

    public MyCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_database, null);
        ViewHolder viewHolder = new ViewHolder();

        viewHolder.mIdTextView = (TextView) view.findViewById(R.id.tv_id);
        viewHolder.mAgeTextView = (TextView) view.findViewById(R.id.tv_age);
        viewHolder.mNameTextView = (TextView) view.findViewById(R.id.tv_name);

        view.setTag(viewHolder);

        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder viewHolder = (ViewHolder) view.getTag();

        int id = cursor.getInt(cursor.getColumnIndex("_id"));
        String name = cursor.getString(cursor.getColumnIndex("name"));
        int age = cursor.getInt(cursor.getColumnIndex("age"));


        viewHolder.mIdTextView.setText(id + "");
        viewHolder.mNameTextView.setText(name);
        viewHolder.mAgeTextView.setText(age + "");

    }

    class ViewHolder {
        TextView mIdTextView;
        TextView mAgeTextView;
        TextView mNameTextView;
    }
}
  • 使用
private void init() {
        //拿到數(shù)據(jù)
        File file = new File(Environment.getExternalStorageDirectory(), "my_database.db"); // 數(shù)據(jù)庫(kù)文件 /storage/emulated/0/my_database.db
        SQLiteDatabase sqLiteDatabase = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);
    //讀取數(shù)據(jù)
        Cursor cursor = sqLiteDatabase.query("table_person", new String[]{"_id", "name", "age"}, "age >= ?", new String[]{"0"}, null, null, null);

        //適配
        MyCursorAdapter adapter = new MyCursorAdapter(this, cursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        mListView.setAdapter(adapter);
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市爷肝,隨后出現(xiàn)的幾起案子猾浦,更是在濱河造成了極大的恐慌,老刑警劉巖灯抛,帶你破解...
    沈念sama閱讀 219,366評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件金赦,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡对嚼,警方通過(guò)查閱死者的電腦和手機(jī)夹抗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)纵竖,“玉大人漠烧,你說(shuō)我怎么就攤上這事∶移觯” “怎么了已脓?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,689評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)通殃。 經(jīng)常有香客問(wèn)我度液,道長(zhǎng),這世上最難降的妖魔是什么画舌? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,925評(píng)論 1 295
  • 正文 為了忘掉前任堕担,我火速辦了婚禮,結(jié)果婚禮上骗炉,老公的妹妹穿的比我還像新娘照宝。我一直安慰自己,他們只是感情好句葵,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,942評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布厕鹃。 她就那樣靜靜地躺著兢仰,像睡著了一般。 火紅的嫁衣襯著肌膚如雪剂碴。 梳的紋絲不亂的頭發(fā)上把将,一...
    開(kāi)封第一講書(shū)人閱讀 51,727評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音忆矛,去河邊找鬼察蹲。 笑死,一個(gè)胖子當(dāng)著我的面吹牛催训,可吹牛的內(nèi)容都是我干的洽议。 我是一名探鬼主播,決...
    沈念sama閱讀 40,447評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼漫拭,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼亚兄!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起采驻,我...
    開(kāi)封第一講書(shū)人閱讀 39,349評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤审胚,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后礼旅,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體膳叨,經(jīng)...
    沈念sama閱讀 45,820評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,990評(píng)論 3 337
  • 正文 我和宋清朗相戀三年痘系,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了菲嘴。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,127評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡碎浇,死狀恐怖临谱,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情奴璃,我是刑警寧澤悉默,帶...
    沈念sama閱讀 35,812評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站苟穆,受9級(jí)特大地震影響抄课,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜雳旅,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,471評(píng)論 3 331
  • 文/蒙蒙 一跟磨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧攒盈,春花似錦抵拘、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,017評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)尚蝌。三九已至,卻和暖如春充尉,著一層夾襖步出監(jiān)牢的瞬間飘言,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,142評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工驼侠, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留姿鸿,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,388評(píng)論 3 373
  • 正文 我出身青樓倒源,卻偏偏與公主長(zhǎng)得像苛预,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子笋熬,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,066評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容