There is a database from which output is performed in the List View. Strings of incomprehensible characters are displayed instead of data. What exactly is wrong here? Thanks in advance for the answers.

// Helper

public class Helper extends SQLiteAssetHelper { private static final String DBName ="BookD.sqlite"; private static final int DBV=1; private SQLiteDatabase db; public Helper(Context context) { super(context,DBName,null,DBV); } public Helper open() throws SQLiteException { db =getWritableDatabase(); return this; } public ArrayList<Object[]> geData() { // TODO Auto-generated method stub String[]columns=new String[]{"id","title","author"}; Cursor c =db.query("books", columns, null, null, null, null, null); ArrayList<Object[]> array = new ArrayList<>(); if (c.moveToFirst()) { do { Object[] obj =new Object[3]; obj[0]=c.getInt(0); obj[1]=c.getString(1); obj[2]=c.getString(2); array.add(obj); } while (c.moveToNext()); }return array; }} 

// MainActivity

 public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Helper helper= new Helper(getApplicationContext()); ListView lv = (ListView) findViewById(R.id.listMe); Helper info = new Helper(this); info.open(); ArrayList<Object[]> array = info.geData(); info.close(); lv.setAdapter(new ArrayAdapter<Object[]>(this, android.R.layout.simple_list_item_1,array)); 

    1 answer 1

    An ArrayAdapter writes to the list item what the toString() method returns, a class enclosed in angle brackets, in your case Object[] . I would write a class, redefine the toString() method depending on what I need to see in the list, and would stick the adapter.

     public class SomeClass { private int id; private String title; private String author; @Override public String toString() { return id + " " + title + " " + author; } public void setId(int id) { this.id = id; } public void setTitle(String title) { this.title = title; } public void setAuthor(String autor) { this.author = autor; } } 

    Then use this:

     public ArrayList<SomeClass> geData() { String[]columns=new String[]{"id","title","author"}; Cursor c =db.query("books", columns, null, null, null, null, null); ArrayList<SomeClass> array = new ArrayList<>(); while (c.moveToNext()); SomeClass someclass =new SomeClass(); someclass.setId(c.getInt(0); someclass.setTitle(c.getString(1)); someclass.setAuthor(c.getString(2)); array.add(someclass); } }return array; }} 

      ArrayList<SomeClass> array = info.geData(); info.close(); lv.setAdapter(new ArrayAdapter<SomeClass>(this, android.R.layout.simple_list_item_1, array));