简体   繁体   English

从android中的sqlite数据库列表视图中的列表顶部的新项目

[英]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 如何在android中的sqlite数据库的列表视图中显示新项目

the code is as follows: 代码如下:

my displayData() method : 我的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();
            }`

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. 我在查询中尝试了“desc”但没有工作并显示错误。

You may try moveToLast() method , which will start displaying your data from last to first 您可以尝试使用moveToLast()方法,该方法将从最后一个开始显示您的数据

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? 在数据库中是否有row_ID等列是Integer主键(使用autoincrease或必须增加值)?
If you have this column, try desc. 如果您有此列,请尝试使用desc。

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

columnName is primary key column. columnName是主键列。
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. 如果您的数据库没有整数主键值,例如row_id,我认为您必须创建此字段以获取选择一行或更多行数据。

You Should get the data in reverse order through id column of the database . 您应该通过数据库的id列以相反的顺序获取数据。 here example of Cursor query .you can get it through rawquery also. 这里是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