Search code examples
androidgetstring

c.getstring not found in ListActivity


public class SimpleCursorAdapterConLogoActivity extends ListActivity
{

    private DatabaseHelper databaseHelper;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        databaseHelper = new DatabaseHelper(this);
        Cursor c = databaseHelper.getAllSiti();
        startManagingCursor(c);
        System.out.println("SimpleProvinceSimpleCursorAdapter con logo");
        setListAdapter(new ProvinceSimpleCursorAdapter(this, c));

    }

Hi guys I wanted to ask why it does not work in the getstring onListItemClick the compiler does not give me error but it crashes when I press on the list position 0

 protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        final int index = position;
        databaseHelper = new DatabaseHelper(this);
        Cursor c = databaseHelper.getAllSiti();
        startManagingCursor(c);

        //You can add whatever you want to happen when you click here

        Log.i("testy", "I Clicked on Row " + index + " and it worked!");
        switch (index){
            case  0:
                {
                    Cursor c1 = databaseHelper.getAllSiti();
                    startManagingCursor(c1);
                    String nomeRegione = c.getString(c.getColumnIndex(SitiTable.NOME));
                    System.out.println(nomeRegione);

                        //System.out.println(nomeRegione);
                }
                break;
            case  1:
                System.out.println("hello2");
                break;
            case  2:
                System.out.println("hello£");   
                break;
        }           
  }
@Override
protected void onDestroy()
{
    super.onDestroy();
    databaseHelper.close();
}

}


Solution

  • You have to call cursor.moveToFirst() or cursor.moveToPosition(int) before you can obtain column data.