Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6640507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:40:50+00:00 2026-05-25T23:40:50+00:00

I have been trying to figure this problem out without asking for help, but

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:40:50+00:00Added an answer on May 25, 2026 at 11:40 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to get this problem solved, but I can't seem to figure
I've been trying to figure out this problem. I have an assignment to make
I have been trying to figure this out for way to long tonight. I
I have been trying to figure out a solution but nothing has really presented
I have been pulling my hair out trying to figure out what I can't
i've been trying to figure this out and i have absolutely no clue why
I've been trying to figure out how to detect this for months without any
Hey all i have been trying to figure out why i am getting this
I've been trying to figure this out for a while, but what is the
Ok I have been trying every which way to figure this out. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.