-7

How can I get the next column value not the new row?

Im using listview to display information of the selected user from the spinner.

What shows up: Firstname, Firstname, Firstname

Should be instead: Firstname, Lastname, Email, etc.

Im thinking about adding i++ but seems not to be working

  public void myMethod(String user) {

    ArrayList<String> theList = new ArrayList<>();

    Cursor data = db.getListViewAccountrequestinfo();
    if (data.getCount() == 0) {

    } else {

        while (data.moveToNext()) {


            theList.add( data.getString(0) );
            ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList);
            lvinfo.setAdapter(listAdapter);
        }
    }
}
Aya Sato
  • 29
  • 1
  • 9
  • You are setting adapter inside loop . Thats a total disaster. Put that code outside of loop . And have a local variable increment it on each iteration and use it further for your problem . – ADM Nov 30 '17 at 13:15
  • Please specify what the problem is or add the errors(if any). – Abhishek S Nov 30 '17 at 13:26

2 Answers2

0

Listview should display the rows in database, not columns.

Because data.getString() takes parameter as int columnIndex

so when you increment its value(columnIndex), it will give Next Column value not the new row.

Refer Cursor.getString()

Community
  • 1
  • 1
Firoz Memon
  • 4,282
  • 3
  • 22
  • 32
0

Ive got it already. Here is my new working code after 4 hrs LOL

 public void myMethod() {

    ArrayList<String> theList = new ArrayList<>();


    Cursor data = db.getListViewAccountrequestinfo(spnpending.getSelectedItem().toString());
    if (data.getCount() == 0) {

    } else {


        if (data.moveToFirst()) {
            do {
                theList.add("Username: " + spnpending.getSelectedItem());
                theList.add("First name: " + data.getString(1));
                theList.add("Last name: " + data.getString(2));
                theList.add("Contact: " + data.getString(3));
                theList.add("Email: " + data.getString(4));

                ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList);
                lvinfo.setAdapter(listAdapter);

            } while (data.moveToNext());
        }
    }
Aya Sato
  • 29
  • 1
  • 9