简体   繁体   中英

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

How to display new item on top in list view from sqlite database in android

the code is as follows:

my displayData() method :

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();
            }`

My Custom list adapter class:

 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;
    }

i tried "desc" in query but not working and showing error.

You may try moveToLast() method , which will start displaying your data from last to first

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

Do you have column such as row_ID is Integer primary key(using autoincrease or you must increase value) in database?
If you have this column, try desc.

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

columnName is primary key column.
Then, result is data witch is input data in sorted.

If your database doesn't have integer primary key value such as row_id, I think you must create this field for get select one row or more row data.

You Should get the data in reverse order through id column of the database . here example of Cursor query .you can get it through rawquery also.

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM