簡體   English   中英

從android中的sqlite數據庫列表視圖中的列表頂部的新項目

[英]new item on top of list in list view from sqlite database in android

如何在android中的sqlite數據庫的列表視圖中顯示新項目

代碼如下:

我的displayData()方法:

private void displayData()
                {
        dataBase = mHelper.getWritableDatabase();
        Cursor mCursor = dataBase.rawQuery("SELECT * FROM "+ DbHelper.TABLE_NAME, null);
        userId.clear();
        Name.clear();
        Description.clear();
                Assignto.clear();
                Duration.clear();
                Priority.clear();
                Category.clear();
                Kudos.clear();
        if (mCursor.moveToFirst())
                  {
            do {
                userId.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ID)));
                Name.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.Name)));
                Description.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.Description)));
                                Assignto.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.Assignto)));
                                Duration.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.Duration)));
                                Priority.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.Priority)));
                                Category.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.Category)));
                                Kudos.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.Kudos)));                                       }
                     while (mCursor.moveToNext());
          }
CustomList adapter = new CustomList(tasklist.this,userId,Name,Description,Assignto,Duration,Priority,Category,Kudos,imageId);
        list.setAdapter(adapter);
        mCursor.close();
            }`

我的自定義列表適配器類:

 public View getView(int pos, View child, ViewGroup parent)
          {
           Holder mHolder;
            LayoutInflater layoutInflater;

        if (child == null)
            {
                    layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    child = layoutInflater.inflate(R.layout.list_single, null);
                    mHolder = new Holder();
                    mHolder.imageId=(ImageView) child.findViewById(R.id.img);
                    mHolder.txt_Name = (TextView) child.findViewById(R.id.txt_Name);
                    mHolder.txt_Description = (TextView) child.findViewById(R.id.txt_Description);
                    mHolder.txt_Assignto = (TextView) child.findViewById(R.id.txt_Assignto);
                    child.setTag(mHolder);
            }
           else {
                   mHolder = (Holder) child.getTag();
                }

            mHolder.imageId.setImageResource(imageId[pos]);
            mHolder.txt_Name.setText(Name.get(pos));
            mHolder.txt_Description.setText(Description.get(pos));
            mHolder.txt_Assignto.setText(Assignto.get(pos));
            return child;
      }
    public class Holder
     {

            ImageView imageId;
            TextView txt_Name;
            TextView txt_Description;
            TextView txt_Assignto;
    }

我在查詢中嘗試了“desc”但沒有工作並顯示錯誤。

您可以嘗試使用moveToLast()方法,該方法將從最后一個開始顯示您的數據

if(cursor.moveToLast()){
     do{
         //your operations here
     }while(cursor.moveToPrevious());
}

在數據庫中是否有row_ID等列是Integer主鍵(使用autoincrease或必須增加值)?
如果您有此列,請嘗試使用desc。

Cursor mCursor = dataBase.rawQuery("SELECT * FROM "+ DbHelper.TABLE_NAME + " order by userId DESC;", null);

columnName是主鍵列。
然后,結果是數據是排序的輸入數據。

如果您的數據庫沒有整數主鍵值,例如row_id,我認為您必須創建此字段以獲取選擇一行或更多行數據。

您應該通過數據庫的id列以相反的順序獲取數據。 這里是Cursor查詢的例子。你也可以通過rawquery獲取它。

 db = helper.getReadableDatabase();
Cursor cursor = db.query(helper.Table_name,null,null,null,null,null,helper._id + " DESC ");

             // THE DESIRED COLUMNS TO BE BOUND
String[] columns = new String[] {helper.KeyName, helper.KeyVlue };
            // THE XML DEFINED VIEWS WHICH THE DATA WILL BE BOUND TO
 int[] to = new int[] { R.id.name, R.id.score };

            // CREATE THE ADAPTER USING THE CURSOR POINTING TO THE DESIRED DATA AS WELL AS THE LAYOUT INFORMATION
 MyCursorAdapter dataAdapter = new MyCursorAdapter(MainActivity.this, R.layout.simpl_list_item,cursor,columns,to,0);

            // SET THIS ADAPTER AS YOUR LISTACTIVITY'S ADAPTER   
 list.setAdapter(dataAdapter);

暫無
暫無

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

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