来自sqlite的Android Listview

来自sqlite的Android Listview,android,sqlite,listview,Android,Sqlite,Listview,我已经挣扎了好几天了,我做错了什么,以至于我没有在我的listview上获取数据 // This is the function inside database class public ArrayList<HashMap<String, String>> getMessage(){ ArrayList<HashMap<String, String>> message = new ArrayList<HashMap<S

我已经挣扎了好几天了,我做错了什么,以至于我没有在我的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;
    }
};
-第三:->我声明了两个表,第一个表包含我要显示的列的名称,第二个表包含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);
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();
            }
        }

    });
-第五:->我创建了我的类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方法中:

lv = (ListView) findViewById(R.id.list);
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“message”,但从未添加到其中。因此,您总是返回一个空数据集供适配器使用

在光标行上的每次迭代中,将已创建的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());

@埃尔·乌菲尔·哈蒂姆:谢谢,你的做法非常令人信服。如果你能提供一个完整的实现,也许是一个完整的项目,如你所说的显示所有数据库操作,这不仅对我非常有帮助,而且对所有其他寻求更好数据库实现的人都非常有帮助。你是welcom,这是我制作的一个简单应用程序,这是一个猜数字的游戏,非常简单,你可以在这个应用程序中找到我之前给你的代码。这是链接:@EL-OUFIR-Hatim:非常感谢;一个愚蠢的错误毁了我的生活。谢谢你告诉我我的错误。