0

In my database code I am currently going through the ID column of my SQLite database. Now at the moment this is returning a string variable which is listing ALL entries when I put it is a textview for example. But I want to put that variable in some sort of listview (or similar) and for the user to be able to click that listview and bring up the rest of the information relating to that primary key. I have been doing research and have found tutorials to do this but they do not suite what I want to do and didn't exactly work for me. Here is the code below which is in the activity that I want to show the list view. Thanks.

public class View_Saved_Dates extends Activity {
ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_saved_dates);       
    list = (ListView)findViewById(R.id.listView1);

    Database info = new Database(this);
    info.open();
    //I want to put the variable below which lists all data within the id column of my database into a list view or some sort of list where I can 
    //click the id and bring up the rest of the information in the databse.
    String cowid = info.getCowid();

    info.close();
    }
}
3
  • I have tried this but have had no luck at all. My app just quits. I am pretty sure I followed the tutorial the right way. Commented Jun 30, 2013 at 11:19
  • If the app "just quits", that sounds like an uncaught exception to me. Check your error logs for stacktraces, or run your app in debug mode in an IDE such as Eclipse. Commented Jun 30, 2013 at 11:27
  • possible duplicate of Populating a list view from SQLite Database Commented Jun 30, 2013 at 11:28

2 Answers 2

0

Try the below links-

http://www.mysamplecode.com/2012/07/android-listview-cursoradapter-sqlite.html

http://androidtuts4u.blogspot.in/2012/11/android-sqlite-and-listview-example.html

How to Read Data from SQLite Database and show it on a list view

Display data from SQLite database into ListView in Android

How to get data form SQLite database and display it in ListView

how to fetch the data from sqlite database and show in listview in android

It may help you.

Sign up to request clarification or add additional context in comments.

Comments

0

I beleive that this should fix your problem. In your database class, instead of putting all your queried data into a string put it into a string array.

public String[] getid() {
    // TODO Auto-generated method stub
    ArrayList<String> strings = new ArrayList<String>();
    String[] id = new String[]{KEY_ID};
    Cursor c = ourDatabase.query(DATABASE_TABLE, id, null, null, null, null, null);
    int i = c.getColumnIndex(KEY_ID);
    while(c.moveToNext()){ strings.add(c.getString(i));
    }
    String[] mString = (String[]) strings.toArray(new String[strings.size()]);
    return mString;

}

Then in your main class where you want this list view to be displayed create a list view and use the following code and it should work

lv = (ListView)findViewById(R.id.list_view); 
    Database info = new Database(this);
    info.open();
    String [] id = info.getid();
    ArrayAdapter<String> arrayAdapter =  new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, id);
    lv.setAdapter(arrayAdapter);
    info.close();

I hope that works for you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.