I have been trying to figure this problem out without asking for help, but I have reached the point where I honestly can not figure it out.
Here is the function I use to query the database (I have had to change name for confidentiality):
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;
}
This is where I populate a tablelayout that has been created using xml.
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();
If I view the database using DDMS, everything is displayed in the correct order (the order in which I insert into the table). However, when I try to display the data in a tablelayout, for some reason it organizes the values based on the KEY_NAME value. So if I insert data into the table like "B - C - D - A - E" it will populate my table layout with "A - B - C - D - E"
Something strange is that I have it setup so that a longclick on any given row with generate a dialogfragment displaying the rest of the information, and this information is correct. So if using the example above I long click on the displayed "C" row then I will actually get the data for "D" which is correct.
Any idea?
In the javadocs for the SQLiteDatabase class, for the query method, it says that "Passing null [for sort order] will use the default sort order, which may be unordered." That null is one of the many nulls you are passing, and so the order may be unordered, but apparently it may be ordered instead.
Perhaps try using public Cursor rawQuery (String sql, String[] selectionArgs)
instead of query?
So I think for your case that would be something like
public Cursor rawQuery (String "SELECT DISTINCT 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 FROM DATABASE TABLE", null);
It's been my experience that Android SQLite returns rawQuery results in the insert order, although I'm not sure that it's required to. A more solid way to do it would be to add autoincrement key column -- giving each row a serial number, essentially -- and then ordering the results of queries on that.
精彩评论