0

Hi I have this code which retrieves data from the SQLite local database I have created.

the code below is a public cursor which retrieves all the data i require from the database

public Cursor RetriveAdvertData (DatabasOpperations DBOpp, String Username, String AdvertType){
    SQLiteDatabase SQDB = DBOpp.getReadableDatabase();
    String[] Coloumns = {TableData.TableInfo.USERNAME, TableData.TableInfo.ADVERT_NAME, TableData.TableInfo.ADVERT_EMAIL, TableData.TableInfo.ADVERT_ADDRESS, TableData.TableInfo.ADVERT_NUMBER, TableData.TableInfo.ADVERT_TYPE};
    String Where = TableData.TableInfo.USERNAME + " LIKE ? AND " + TableData.TableInfo.ADVERT_TYPE + " LIKE ?";
    String Argument[] = {Username, AdvertType};
    Cursor Cur = SQDB.query(TableData.TableInfo.TABLE_NAME2, Coloumns, Where, Argument, null, null, null);
    return Cur;
}

I then call that Cursor in another java page

Context Contx = this;
public DatabasOpperations DB = new DatabasOpperations(Contx);
public Cursor Cur;
Cur = DB.RetriveAdvertData(DB, DBUsername, "Restaurant");

String AdName = "";
String AdEmail = "";
String AdAddress = "";
String AdNumber = "";
String AdType = "";

if (Cur.moveToFirst()){
            do {
                AdName = Cur.getString(Cur.getColumnIndex("AdvertsName"));
                AdEmail = Cur.getString(Cur.getColumnIndex("AdvertEmail"));
                AdAddress = Cur.getString(Cur.getColumnIndex("AdvertAddress"));
                AdNumber = Cur.getString(Cur.getColumnIndex("AdvertNumber"));
                AdType = Cur.getString(Cur.getColumnIndex("AdvertType"));

                final String[] Adverts = new String[]{
                        AdName, AdEmail, AdAddress, AdNumber, AdType
                };

                ArrayAdapter setListAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Adverts);
                LV1.setAdapter(setListAdapter);
            }while (Cur.moveToNext());
        }
        Cur.close();

the code does display only one row from the database, how do i amend the code to display all the rows in the database?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
RexDough
  • 181
  • 1
  • 11
  • If only displays one row because you are resetting the adapter within each loop. – OneCricketeer Mar 28 '16 at 15:50
  • what do you suggest to fix this? – RexDough Mar 28 '16 at 15:59
  • get rid of the do? and put a while in – RexDough Mar 28 '16 at 15:59
  • i did add instead of the do a while(!Cur.AfterLast) but the app just displayed a black screen when i ran it, no error message or nothing – RexDough Mar 28 '16 at 16:00
  • Well, just look what you are doing. For each row, you make a brand new array containing only the data from that row, then you create a new adapter for that one row. You should make a object class for the data you want to display, then add to an ArrayList within the while. Outside of the while loop is where you set the adapter. – OneCricketeer Mar 28 '16 at 16:09
  • what the one answer below says, is what you are suggesting, i have commented what appears instead of the data – RexDough Mar 28 '16 at 16:13

1 Answers1

0

Try to put the setAdapter out of the while loop:

    List<String[]> arr = new ArrayList<>();

    if (Cur.moveToFirst()) {
        do {
            AdName = Cur.getString(Cur.getColumnIndex("AdvertsName"));
            AdEmail = Cur.getString(Cur.getColumnIndex("AdvertEmail"));
            AdAddress = Cur.getString(Cur.getColumnIndex("AdvertAddress"));
            AdNumber = Cur.getString(Cur.getColumnIndex("AdvertNumber"));
            AdType = Cur.getString(Cur.getColumnIndex("AdvertType"));

            final String[] Adverts = new String[]{
                    AdName, AdEmail, AdAddress, AdNumber, AdType
            };

            arr.add(Adverts)
        } while (Cur.moveToNext());
    }
    Cur.close();

    ArrayAdapter setListAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arr);
    LV1.setAdapter(setListAdapter);
motis10
  • 2,484
  • 1
  • 22
  • 46
  • when i view the list view it doesn't display anything other than [Ljava.lang.String'@32c6d061, any clue why? – RexDough Mar 28 '16 at 16:11
  • @RexDough Becuase `android.R.layout.simple_list_item_1` just calls `toString` on its elements. You can see [this post](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) for reference. – OneCricketeer Mar 28 '16 at 16:19
  • oh ok, so its printing the interger value of that string, how do i amend it to display the data rather than @ingerger value of the string? – RexDough Mar 28 '16 at 16:24
  • I didn't understand. Do you see the listview with more then one row? Now the problem is just the string in the listview? – motis10 Mar 29 '16 at 08:09