簡體   English   中英

來自sqlite的Android Listview

[英]Android Listview from sqlite

我現在一直在苦苦掙扎我做錯了什么,所以我沒有在listview上獲取我的數據

// This is the function inside database class

public ArrayList<HashMap<String, String>> getMessage(){
        ArrayList<HashMap<String, String>> message = new ArrayList<HashMap<String, String>>();
        String selectQuery = "SELECT  * FROM " + TABLE_MESSAGE;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // Move to first row
        if (cursor.moveToFirst()) {
            do {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("message_sno", cursor.getString(0));
            map.put("message_sender", cursor.getString(1));
            map.put("message_subject", cursor.getString(2));
            map.put("message_date", cursor.getString(3));
            map.put("message_read_flag", cursor.getString(4));
            } while (cursor.moveToNext());
        }
        // return contact list
        return message;
    }

以下是我的活動中擴展ListActivity

ArrayList<HashMap<String, String>> emaillist = db.getMessage();
        if(emaillist.size()!=0) {
            Toast.makeText(getApplicationContext(), "this" + emaillist.get(1), Toast.LENGTH_SHORT).show();
            ListView lv = getListView();

            /*<!-- android:id="@+id/inboxlist" -->*/
            ListAdapter adapter = new SimpleAdapter( InboxActivity.this, emaillist,
                    R.layout.inbox_list_item,
                    new String[] {"message_sender","message_subject","message_date","message_read_flag"}, new int[] {
                     R.id.from,R.id.subject,R.id.date,R.id.unread});
            setListAdapter(adapter);
        }

對我來說,我總是使用這種方法,它工作得很好:)

- 首先: - >我宣布我的變量:

private ListView lv; //My listview that will contain the informations
DBHelper helper = new DBHelper(this); //DBHelper is the class that contain my database on SQLite
private SQLiteDatabase db; //An instance of SQLiteDatabase that will contain my Database 
private Cursor cursor; //A cursor for the query 
public SimpleCursorAdapter adapter ; //The adapter of my ListView   

- 第二: - >我創建了我的ViewBinder:

public static ViewBinder viewBinder = new ViewBinder() {

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        return false;
    }
};

- 第三: - >我聲明了兩個表(第一個表包含我要顯示的列的名稱,以及seconde TextView或ImageView ......它將包含該信息):

static final String[] FROM = { "name", "image" };
static final int[] TO = { R.id.txt_name, R.id.img };

- 第四: - >我在onCreate()方法上聲明我的listview:

lv = (ListView) findViewById(R.id.list);

- 第五: - >我創建了包含將包含信息的對象的ViewHolder類:

static class ViewHolder {

    private TextView txtName;
    private ImageView img;

    public ViewHolder(View vue) {

        txtName = (TextView) vue.findViewById(R.id.txt_name);
        img =(ImageView) vue.findViewById(R.id.img);
    }
}

- 第六: - >我創建了我的arrayAdapter類:

class CustomAdapter extends SimpleCursorAdapter {

    Activity context;
    Cursor c;

    @SuppressWarnings("deprecation")
    public CustomAdapter(Activity context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.context = context;
        this.c = c;
        Log.i("custom", "" + c.getCount());
    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.informations, null);
        //layout.informations is the layout that contain the textview and image view that will contain my informations
        ViewHolder wrapper = new ViewHolder(row);
        row.setTag(wrapper);

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

        int n_name = c.getColumnIndex("name");

        //Example how i fill my image from informations of SQLite
        if (c.getString(n_name).equals("Suite royale")) {
            wrapper.imge.setImageResource(R.drawable.suite_royale);
        }
        if (c.getString(n_name).equals("Suite familiale")) {
            wrapper.img.setImageResource(R.drawable.suite_familiale);
        }
        wrapper.txtName.setText(c.getString(n_name));
    }
}

- 最后: - >在我的onResume()方法中:

db = helper.getReadableDatabase();
cursor = db.query("Name_of_table", null, null, null, null, null,"_id DESC");
//it's like the SQL query : SELECT * FROM Name_of_table
startManagingCursor(cursor);
adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.informations, cursor, FROM, TO);
adapter.setViewBinder(viewBinder);
lv.setAdapter(adapter);
SimpleCursorAdapter chambres = new CustomAdapter(this, R.layout.informations,cursor, FROM, TO);
lv.setAdapter(chambres);

//Just doing this you can see your informations in your ListView and if you want a clickListener on your line of the listView just add the code below :

lv.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long id) {
            Cursor c = db.query("Name_of_table", null, "_id="+(lv.getCount()-position), null, null, null,null);
//== SELECT * FROM Name_of_table WHERE _id = Id_Selected, so you need an id called (_id) on your table his type is INT AUTO_INCREMANTE for example
            c.moveToFirst();
            Log.i("position", ""+position);
            Log.i("taille","Cursor :"+c.getCount());
            String name= c.getString(c.getColumnIndex("name"));
            Toast.makeText(getBaseContext(), "Name selected is : " + name, Toast.LENGTH_SHORT).show();
            }
        }

    });

我希望這會對你有所幫助,祝你好運:)

在您的getMessage()方法中,您實例化ArrayList'消息',但從不添加它。 因此,您始終返回要使用的適配器的空數據集。

在光標行的每次迭代中,將您創建的HashMap添加到ArrayList,如下所示:

do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("message_sno", cursor.getString(0));
map.put("message_sender", cursor.getString(1));
map.put("message_subject", cursor.getString(2));
map.put("message_date", cursor.getString(3));
map.put("message_read_flag", cursor.getString(4));
message.add(map);    //Add the map to the message ArrayList
} while (cursor.moveToNext());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM