I try to make it so that when I click on the listview element a window with the column name pops up, but it turns out to display only id , or, if I use .getItemAtPosition(position) , I get a string like android.database.sqlite.SQliteCursor@.... Please help in the decision.

Class code

 public class StopActivity extends FragmentActivity implements LoaderCallbacks<Cursor> { final String LOG_TAG = "myLogs"; private static final int CM_DELETE_ID = 1; ListView lvData; bd db; SimpleCursorAdapter scAdapter; TextView tvText; // CursorAdapter scAdapter; Cursor cursor; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_stop ); // открываем подключение к БД db = new bd( this ); db.open(); // формируем столбцы сопоставления String[] from = new String[]{bd.COLUMN_NAME}; int[] to = new int[]{R.id.tvText}; // создааем адаптер и настраиваем список scAdapter = new SimpleCursorAdapter( this, R.layout.item, null, from, to, 0 ); //scAdapter = new MyAdapter( this, R.layout.item, null, from, to, 0 ); lvData = (ListView) findViewById( R.id.lvData ); lvData.setAdapter( scAdapter ); // добавляем контекстное меню к списку registerForContextMenu( lvData ); // создаем лоадер для чтения данных getSupportLoaderManager().initLoader( 0, null, this ); lvData.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText( getApplicationContext(), "Вы выбрали " + lvData.getItemAtPosition(position), Toast.LENGTH_SHORT ).show(); } } ); } protected void onDestroy() { super.onDestroy(); // закрываем подключение при выходе db.close(); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle bndl) { return new MyCursorLoader( this, db ); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { scAdapter.swapCursor( cursor ); } @Override public void onLoaderReset(Loader<Cursor> loader) { } static class MyCursorLoader extends CursorLoader { bd db; public MyCursorLoader(Context context, bd db) { super( context ); this.db = db; } @Override public Cursor loadInBackground() { Cursor cursor = db.getAllDataStop(); try { TimeUnit.SECONDS.sleep( 3 ); } catch (InterruptedException e) { e.printStackTrace(); } return cursor; } } } 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

The fact is that the method returns a corny Cursor. All you need is to get the column you need:

 Cursor cursor = (Cursor) lvData.getAdapter().getItem(position); String text = cursor.getString(0); Toast.makeText( getApplicationContext(), "Вы выбрали " + text, Toast.LENGTH_SHORT ).show(); 

You can also get the cursor from the adapter and walk on it:

 Cursor c = ((SimpleCursorAdapter) lvData.getAdapter()).getCursor(); c.moveToPosition(position); String text = c.getString(0); Toast.makeText( getApplicationContext(), "Вы выбрали " + text, Toast.LENGTH_SHORT ).show(); 
  • Thank! I corrected your decision a little, and then 1 method displayed the position, and in the second the text was displayed only for the last element. Changing the line as written below, everything began to work correctly. String text = cursor.getString (cursor.getColumnIndexOrThrow (bd.COLUMN_NUMBER)); - lossiel
  • Probably he did not display the position, but the ID? You need to know the number, and getColumnIndexOrThrow returns just the column number by its name. - Alexey Malchenko