Zychen

导航

Android:SQLite数据绑定ListView

public void showDbUpdata(){
        mContext = MainActivity.this;
        myDBHelper = new DbOpenHelper(mContext, "note.db", null, 1);

        TextView note_main_content = (TextView) findViewById(R.id.content_main_textView_1);

        mydb = myDBHelper.getWritableDatabase();
        mydbcontent = new StringBuilder();

        Cursor cursor = mydb.query("note", null, null, null, null, null, null);
        //int columnsSize = cursor.getColumnCount();获取长度
        List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
        if (cursor.moveToFirst()) {
            do {

                int noteid = cursor.getInt(cursor.getColumnIndex("noteid"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String fenlei = cursor.getString(cursor.getColumnIndex("fenlei"));
                String date = cursor.getString(cursor.getColumnIndex("date"));
                //mydbcontent.append( noteid + ":" + title+":" + fenlei+":" + date + "\n");

                Map<String, Object> showitem = new HashMap<String, Object>();
                showitem.put("icon", fenbie_icon_list[1]);
                showitem.put("title", title);
                showitem.put("fenlei", fenlei);
                showitem.put("date", date);
                showitem.put("number", 1);
                listitem.add(showitem);
            } while (cursor.moveToNext());
        }
        cursor.close();
        //创建一个simpleAdapter
        SimpleAdapter myAdapter = new SimpleAdapter(getApplicationContext(), listitem, R.layout.main_list_item, new String[]{"icon","title", "date","number"}, new int[]{R.id.imageView_note_icon,R.id.textView_title, R.id.textView_date,R.id.textView_number});
        ListView listView = (ListView) findViewById(R.id.main_note_list);
        listView.setAdapter(myAdapter);

       // note_main_content.setText(mydbcontent);
    }

posted on 2016-02-21 13:44  Zychen  阅读(113)  评论(0编辑  收藏  举报