我一直在试图找出这个问题而没有寻求帮助,但我已经达到了我真正无法弄清楚的程度。我的数据库查询显示按字母顺序对值进行排序
这里是我用来查询数据库的功能(我不得不保密的改变名称):
public Cursor getAllItems(){
Cursor cursor = db.query(true, DATABASE_TABLE, new String []{KEY_NAME, KEY_1,KEY_2, KEY_3,
KEY_4, KEY_5, KEY6,
KEY_7, KEY_8, KEY_9, KEY_10, KEY_11, KEY_12,
KEY_13}, null, null, null, null, null, null);
cursor.moveToFirst();
return cursor;
}
这是我填充已使用XML创建一个tablelayout。
public void populate(){
db = new DBAdapter(this);
TableLayout TL = (TableLayout)findViewById(R.id.table);
db.open();
c = db.getAllItems();
while(!c.isAfterLast()){
if(Integer.toString(c.getPosition())!=null){
TableRow TR = new TableRow(this);
TR.setId(c.getPosition());
TextView column1 = new TextView(this);
TextView column2 = new TextView(this);
TextView column3 = new TextView(this);
TR.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
column1.setGravity(0x01);
column1.setTextColor(0xff000000);
column1.setTextSize(2,20);
//column1.setText(c.getString(3));
column1.setText(Integer.toString(c.getPosition()));
TR.addView(column1);
column2.setGravity(0x01);
column2.setTextColor(0xff000000);
column2.setTextSize(2,20);
column2.setText(c.getString(0));
TR.addView(column2);
column3.setGravity(0x01);
column3.setTextColor(0xff000000);
column3.setTextSize(2,20);
//column3.setText(c.getString(1));
column3.setText(Integer.toString(TR.getId()));
TR.addView(column3);
TR.setOnLongClickListener(this);
TL.addView(TR);
1Total += c.getDouble(1);
2Total += c.getDouble(2);
}
c.moveToNext();
}
db.close();
如果我查看使用DDMS数据库,一切都被显示在正确的顺序(其中我插入到表中的顺序)。但是,当我尝试在tablelayout中显示数据时,出于某种原因,它会根据KEY_NAME值组织值。因此,如果我将数据插入表格中,如“B - C - D - A - E”,它将填充我的表格布局“A - B - C - D - E”
奇怪的是,以便在任何给定行上单击并生成一个显示其余信息的对话框片段,并且此信息是正确的。因此,如果使用上面的例子,我长按一下显示的“C”行,然后我会实际得到“D”的数据,这是正确的。
有什么想法?
我打算接受这个答案作为解决方案。我解决这个问题的方式基本上就是你在第二部分所说的。我已经有一个autoincrementing id,但我没有做的是在我的查询语句中返回id。我还编辑了查询函数调用,如下所示: Cursor cursor = db.query(DATABASE_TABLE,new String [] {KEY_ROWID,KEY_NAME,KEY_1,KEY_2,KEY_3,KEY_4,KEY_6,KEY_7,KEY_8,KEY_9,KEY_9,KEY_10 ,KEY_11,KEY_12,\t KEY_13},null,null,null,KEY_ROWID); – JaRay