Search code examples
androiddatabasesqlitecursor

cursor is not displaying data in second activity


I have a list of items, the data are stored in the SQLite. The problem occurred when I wanted to get detailed view of the item, it doesn't display any data. This is activity that displays all rows from the database in a listview, works great.

public class List extends Activity {

    public final static String ID_EXTRA="com.example.bazadanych._ID";

    DBAdapter myDB;

    String passedV=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        passedV=getIntent().getStringExtra(MainActivity.ID_EXTRA);

        openDB();

        populateListViewDB ();

        onclickcallback();

    }

@Override
    protected void onDestroy() {
        super.onDestroy();
        closeDB();
    }

private void openDB() {
        myDB = new DBAdapter(this);
        myDB.open();

    }

private void closeDB() {
        myDB.close();

}

     private void populateListViewDB() {
    Cursor cursor = myDB.getAllRows();

    startManagingCursor(cursor);

    String[] fromFieldNames = new String[]
                {DBAdapter.KEY_NAME, DBAdapter.KEY_COUNTRY, DBAdapter.KEY_REGION, DBAdapter.KEY_PHONE};
    int [] toViewIDs = new int []
            {R.id.list_item_name, R.id.list_item_country, R.id.list_item_region, R.id.list_icon_item};

    SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(
            this, 
            R.layout.list_item, 
            cursor,                 
            fromFieldNames,         
            toViewIDs               
            );  


    ListView myList = (ListView) findViewById(R.id.listViewDB);
    myList.setAdapter(myCursorAdapter);

    }
              private void onclickcallback() {
         ListView myList = (ListView) findViewById(R.id.listViewDB);
            myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked,
                    int position, long id) {

                Intent intent = new Intent(List.this, Details.class);
                intent.putExtra(ID_EXTRA, String.valueOf(id));
                startActivity(intent);       
                                }
                        });
     }
}

From the List.class after clicking on item from the list the user can move to details.class that is suppose to display details of the item. Unfortunately it doesn't display anything and doesn't give any error either.

I'm passing an Id from one activity to another, it moves from one to the other but later on doesn't display any data,

public class Details extends Activity {

    DBAdapter myDB;

    String passedV=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        passedV=getIntent().getStringExtra(List.ID_EXTRA);      
        myDB = new DBAdapter(this);
    }

         private void populatedetailsViewDB() {
        Cursor cursor = myDB.getRow(0);

        startManagingCursor(cursor);


        String[] fromFieldNames = new String[]
                    {DBAdapter.KEY_NAME, DBAdapter.KEY_COUNTRY, DBAdapter.KEY_REGION, DBAdapter.KEY_ADRESS, DBAdapter.KEY_PHONENUM, DBAdapter.KEY_PHONE};
        int [] toViewIDs = new int []
                {R.id.item_name, R.id.item_country, R.id.item_region, R.id.item_adress, R.id.item_phonenum, R.id.list_icon_item};

        SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(
                this, 
                R.layout.item_layout, 
                cursor,                 
                fromFieldNames,         
                );  

        ListView myList = (ListView) findViewById(R.id.detailsViewDB);
        myList.setAdapter(myCursorAdapter);


    }
}

That's getting a single row from the DBAdapter.

public class DBAdapter {

...

public DBAdapter(Context ctx) {
        this.context = ctx;
        myDBHelper = new DatabaseHelper(context);
    }

    // Open the database connection.
    public DBAdapter open() {
        db = myDBHelper.getWritableDatabase();
        return this;
    }

    // Close the database connection.
    public void close() {
        myDBHelper.close();
    }

...

    public Cursor getRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                        where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

Can anyone see what might be the problem?


Solution

  • So you're querying your database on the getRow method of the DBAdapter class, which returns a cursor pointing at a single row. However in your Details activity you always call getRow(0), instead of the actual row of the item.

    You might want to change this method:

    private void populatedetailsViewDB() {
            myDB.open();
            Cursor cursor = myDB.getRow(Long.parseLong (passedV));
    
            startManagingCursor(cursor);
    
    
            String[] fromFieldNames = new String[]
                        {DBAdapter.KEY_NAME, DBAdapter.KEY_COUNTRY, DBAdapter.KEY_REGION, DBAdapter.KEY_ADRESS, DBAdapter.KEY_PHONENUM, DBAdapter.KEY_PHONE};
            int [] toViewIDs = new int []
                    {R.id.item_name, R.id.item_country, R.id.item_region, R.id.item_adress, R.id.item_phonenum, R.id.list_icon_item};
    
            SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(
                    this, 
                    R.layout.item_layout, 
                    cursor,                 
                    fromFieldNames,         
                    );  
    
            ListView myList = (ListView) findViewById(R.id.detailsViewDB);
            myList.setAdapter(myCursorAdapter);
            myDB.close();
    
        }