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

  • Home
  • SEARCH
  • 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 6883015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:19:43+00:00 2026-05-27T05:19:43+00:00

This is my first try to work with databases in android.i want my app

  • 0

This is my first try to work with databases in android.i want my app to get all the database data and present them in a list.My problem is that i m just presenting the last item of the database in my list.This is the code i m getting database data:

public String[] getData()
{

    String[] columns =new String[]{DBHelper.ROWID, DBHelper.TITLE ,  DBHelper.AUTHOR, DBHelper.ISBN };
    Cursor c=ourDatabase.query(DBHelper.DATABASE_TABLE, columns, null, null, null, null, null);
    String sa = null;
    String sb = null;
    String sc = null;


    int iRow=c.getColumnIndex(DBHelper.ROWID);
    int is1=c.getColumnIndex(DBHelper.TITLE);
        int is2=c.getColumnIndex(DBHelper.AUTHOR);
            int is3=c.getColumnIndex(DBHelper.ISBN);

            for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
                 sa=c.getString(is1);
                 sb=c.getString(is2);
                 sc=c.getString(is3);
            }
return new String[] {sa,sb,sc};
}

What i have to return in order to see the whole database?Thanks

This is my onCreate:

  HotOrNot entry2=new HotOrNot(this);
       entry2.open();  
          String[] data2=entry2.getData();
          entry2.close();

          Toast.makeText(SQLView.this,data2[0].toString()+"  "+data2[1].toString()+"  "+data2[2].toString(), Toast.LENGTH_SHORT).show();




       ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();


       HashMap<String, String> map = new HashMap<String, String>();

       for(int i=0;i<data2.length;i+=3){
       map = new HashMap<String, String>();
       map.put("name",data2[i].toString());
       map.put("address", data2[i+1].toString());
       map.put("address2", data2[i+2].toString());

       mylist.add(map);
       }


       // ...
       ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
               new String[] { "name", "address", "address2"},
               new int[] {R.id.rtextView1,R.id.rtextView2,R.id.rtextView3});
       lv.setAdapter(mSchedule);

@Aki

public String[] getData()
{

    String[] columns =new String[]{DBHelper.ROWID, DBHelper.TITLE ,  DBHelper.AUTHOR, DBHelper.ISBN };
    Cursor c=ourDatabase.query(DBHelper.DATABASE_TABLE, columns, null, null, null, null, null);
    String result="";
    String sa = null;
    String sb = null;
    String sc = null;


    int iRow=c.getColumnIndex(DBHelper.ROWID);
    int is1=c.getColumnIndex(DBHelper.TITLE);
        int is2=c.getColumnIndex(DBHelper.AUTHOR);
            int is3=c.getColumnIndex(DBHelper.ISBN);

            for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
                //result=result+c.getString(is1)+" "+c.getString(is2)+" "+c.getString(is3)+"\n";
                 sa=c.getString(is1);
                 sb=c.getString(is2);
                 sc=c.getString(is3);
            }
return new String[] {sa,sb,sc};
}

and

public class SQLView extends ListActivity {
    /** Called when the activity is first created. */    

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.list_layout); 
       HotOrNot entry2=new HotOrNot(this);
       entry2.open();  
       Cursor cursor = getContentResolver().query(DBHelper.DATABASE_TABLE, new String[] {DBHelper.TITLE, DBHelper.AUTHOR, DBHelper.ISBN}, null, null, null);

                   startManagingCursor(cursor);



                   // THE DESIRED COLUMNS TO BE BOUND

                   String[] columns = new String[] { DBHelper.TITLE, DBHelper.AUTHOR, DBHelper.ISBN };

                   // THE XML DEFINED VIEWS WHICH THE DATA WILL BE BOUND TO
       int[] to = new int[] { R.id.rtextView1,R.id.rtextView2,R.id.rtextView3 };


                   // CREATE THE ADAPTER USING THE CURSOR POINTING TO THE DESIRED DATA AS WELL AS THE LAYOUT INFORMATION

                   SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, columns, to);


                   // SET THIS ADAPTER AS YOUR LISTACTIVITY'S ADAPTER

                   this.setListAdapter(mAdapter);

             }

       }
  • 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-27T05:19:44+00:00Added an answer on May 27, 2026 at 5:19 am

    Inside your getData method, you’re looping and on each iteration of the loop, you set sa, sb and sc. You don’t, however, add those to any collection until the loop exits so the array only holds the last set of values. If you still want to return a string array, you can do this:

    List<Map<String,String>> data = new ArrayList<Map<String,String>>();
     int iRow=c.getColumnIndex(DBHelper.ROWID);
        int is1=c.getColumnIndex(DBHelper.TITLE);
            int is2=c.getColumnIndex(DBHelper.AUTHOR);
                int is3=c.getColumnIndex(DBHelper.ISBN);
    
                for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
                     sa=c.getString(is1);
                     sb=c.getString(is2);
                     sc=c.getString(is3);
                     Map<String,String> map = new HashMap<String,String>();
                     map.put("name",sa);
                     map.put("address",sb);
                     map.put("address2",sc);
                     data.add(map);
                }
    
    return data;
    

    Then in your onCreate method you can simply do this:

      HotOrNot entry2=new HotOrNot(this);
      entry2.open();  
      List<Map<String,String>> data2=entry2.getData();
      entry2.close();
      ListAdapter mSchedule = new SimpleAdapter(this, data2, R.layout.row,
                   new String[] { "name", "address", "address2"},
                   new int[] {R.id.rtextView1,R.id.rtextView2,R.id.rtextView3});
      lv.setAdapter(mSchedule);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write an application to work with adress books. My first try
First off - hello, this is my first Stack Overflow question so I'll try
I have a JSON data like this: { hello: { first:firstvalue, second:secondvalue }, hello2:
I am trying to make my first Android app. I noticed that the SQLiteOpenHelper.onCreate()
I'm working on creating a checkbook app for android using a database to record
Consider this code (Yes its ugly but it should also work): try { //
This has been asked once before but that didn't work for me at all.
This first bit works: $my_id = 617; $post_id_7 = get_post($my_id); $title = $post_id_7->post_excerpt; echo
Saw this piece of code in a Ruby on Rails book. This first one
problem euler #5 i found the solution but i don't know why this first

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.