如何在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"在查询中但没有工作并显示错误。
答案 0 :(得分:2)
您可以尝试moveToLast()
方法,该方法将从最后一个开始显示您的数据
if(cursor.moveToLast()){
do{
//your operations here
}while(cursor.moveToPrevious());
}
答案 1 :(得分:0)
您是否在数据库中有row_ID等列是整数主键(使用自动增加或必须增加值)? 如果您有此列,请尝试使用desc。
Cursor mCursor = dataBase.rawQuery("SELECT * FROM "+ DbHelper.TABLE_NAME + " order by userId DESC;", null);
columnName是主键列 然后,结果是数据是输入数据的排序。
如果您的数据库没有整数主键值,例如row_id,我认为您必须创建此字段以获取选择一行或更多行数据。
答案 2 :(得分:0)
您应该通过数据库的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);