1

i have two mehods, one to collect SQL data and one to put it into a listview.. it seems i can't get the array to form correctly in the SQL part so that it can be passed tot the listview.

SQL act:

public String[] getDataInArray() { // get data for list and return in array form
    // TODO Auto-generated method stub

    String[] columms = new String[]{ KEY_ROWID, KEY_NAME};
    String[] return_colums = null;

    Cursor c = currentdatabase.query(DATABASE_TABLE, columms, null, null, null, null, null);

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iName = c.getColumnIndex(KEY_NAME);
    int rowcount = 0;

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
    {
        return_colums[rowcount] = c.getString(iName) + "," + c.getString(iRow);
        rowcount = rowcount + 1;
    }

    return return_colums;
}

And the listview act:

public ListView whiskeylist;
public String[] DataArryWhiskey;  

      @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Start db view of whiskey

    DBConfig whiskeyrows = new DBConfig(this);


    whiskeyrows.open();
    DataArryWhiskey = whiskeyrows.getDataInArray();
    whiskeyrows.close();

    Toast.makeText(MainScreen.this, result, Toast.LENGTH_LONG).show();

    whiskeylist = (ListView)findViewById(R.id.listofWhiskey);
    whiskeylist.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , DataArryWhiskey));


    // End db view of whiskey

}// end onCreate

I keeps on crashing, can anybody help met a bit? Thx in advance

2
  • If you post your logcat log it might help Commented Dec 24, 2011 at 20:22
  • If you need to pass the data to the listView, why didn't you use the SimpleCursorAdapter instead? Commented Dec 24, 2011 at 20:48

2 Answers 2

1

Moved return_colums defination and declaration.

public String[] getDataInArray() { // get data for list and return in array form
// TODO Auto-generated method stub

String[] columms = new String[]{ KEY_ROWID, KEY_NAME};


Cursor c = currentdatabase.query(DATABASE_TABLE, columms, null, null, null, null, null);

int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int rowcount = 0;
  String[] return_colums = new String[c.getCount()];
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
    return_colums[rowcount] = c.getString(iName) + "," + c.getString(iRow);
    rowcount = rowcount + 1;
}

return return_colums;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Move whiskeyrows.close(); from onCreate() to onDestroy(). More details in this link

5 Comments

You need to put the whiskeyrows as global variale: <br/> public String[] DataArryWhiskey; <br/> DBConfig whiskeyrows; <br/> @Override <br/> protected void onCreate(Bundle savedInstanceState) { <br/> // TODO Auto-generated method stub <br/> super.onCreate(savedInstanceState); <br/> setContentView(R.layout.main); <br/> <br/> // Start db view of whiskey <br/> whiskeyrows = new DBConfig(this); <br/> If you need to pass the data to the listView, why didn't you use the SimpleCursorAdapter instead?
i am realy a noob on adroid programming, i would like to use a simple cursor but i don't know how to transfrom this into my situation. i alsow cant get the code to work with your extra comment.
Strange thing is that it dit work and i still get a returnmessage if i use a string instead of a array.
It is simple to use SimpleCursorAdapter. Just load your cursor, rows to display and the layout representing each row in the listView in the adapter (SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)) and set the adapter of your listView to this SimpleCursorAdapter. This is much simpler than your implementation.
Thx for the help i am going to read the tutorial vogella.de/articles/AndroidSQLite/article.html

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.