How to insert a string into a ListView adapter?

 Cursor getPositions() { return mDB.rawQuery("SELECT * FROM "+ TABLE + " WHERE _id = 0", null); } 

At the output in the ListView is only the first value. The cycle works, as I understand the problem in the adapter, which apparently is not intended to write the row as a column in the ListView ?

  cursor = DB.getPositions(); String[] from = new String[cursor.getColumnCount()]; // создаём массив, размерность которого равна количеству столбцов. for (int i = 0; i < cursor.getColumnCount(); i++) { from [i] = "P" + i; // в массив заносим названия столбцов, получается P1, P2, P3 и т.д. } scAdapter = new SimpleCursorAdapter(getActivity(), R.layout.simple_item, cursor, from, new int[] {android.R.id.text1}, 0); //объявляем адаптер и вносим туда известные нам названия столбцов. listview.setAdapter(scAdapter); 
  • which apparently is not intended to write a row in the form of a column - is it possible in more detail what do you mean? - Barmaley
  • From a table in SQLite, I take a row with _id = 0, write to Cursor. The data in the Cursor (element-wise) I need to write to the ListView, when I read the data line by line within one column of the table I see the picture I need, but when I want to read the data on the columns within one row, I get the very first element of the array. The number of records in the Cursor corresponds to the number of elements in the from array and corresponds to the number of columns. The from array contains the column names of my table in SQLite. - Antonem
  • Do you need to display the value of each column of a single row from the database in the ListView as a separate item? display the "horizontal" record from the database as a "vertical" list, that is, the value from column P1 in the first position of the list, P2 in the second and so on? or solve your problem in more detail - pavlofff
  • Exactly as you wrote. - Antonem

1 answer 1

I tried to find a solution purely through Cursor, honestly I could not.

  cursor = DB.getPositions(); List<String>lt = new ArrayList<String>(); String ad = null; cursor.moveToFirst(); for (int i = 0; i < cursor.getColumnCount(); i++) { ad = cursor.getString(i); lt.add(ad); } arAdapter = new ArrayAdapter<String>(getActivity(),R.layout.simple_item, lt); listview.setAdapter(arAdapter);