mercredi 19 août 2015

how to apply multiple cursor results from sqlite database to an array to be displayed in a list view

i have created a local database where i have extracted the data into a simple cursor adapter where i display this adapter within a list view.

the problem is that i want to select a button from another activity where it will display a certain row of information to be inflated within a list view.

at the moment the passed data is layering on top of each other within the list view on a single position (position 1 within the array).

screen 1(onclick new activity) -> screen 2/3(onclick:Add ity button to add row to screen 1 then onclick: display playlist to bring up screen 1) -> screen 1 (onclick: add button to display rows added)-> screen 4 (overlays row 1 with row 2)-> screen 5(onclick row 2 disappears ) -> screen 6(row 1 shows)

http://ift.tt/1NuQU7Q

is there a way to allow the rows to abide by an array where there will layer one after another no matter when they are initiated i.e. listview (position 0) = data base (row 0) listview (position 1) = data base (row 2) listview (position 2) = data base (row 1)

here is my code so far...

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

            openDB();
            populateListViewFromDB1();
            populateListViewFromDB2();
            // setListview();
            registerListClickCallback();
        }



 public void populateListViewFromDB1() {

            Cursor cursor = myDb.getAddView1();


            setListview(cursor);
            // Set the adapter for the list view

    //Log.e("get getAddView1 ",""+  myDb.getAddView1());
            // Allow activity to manage lifetime of the cursor.
            // DEPRECATED! Runs on the UI thread, OK for small/short queries.

        }

        public void populateListViewFromDB2() {
            Cursor cursor = myDb.getAddView2();

            setListview(cursor);

            // Set the adapter for the list view
        }

        public void setListview(Cursor cursor) {
            startManagingCursor(cursor);

            // Setup mapping from cursor to view fields:
            String[] fromFieldNames = new String[]
                    {DBAdapter.KEY_NAME, DBAdapter.KEY_STUDENTNUM, DBAdapter.KEY_FAVCOLOUR, DBAdapter.KEY_STUDENTNUM};
            int[] toViewIDs = new int[]
                    {R.id.item_name, R.id.item_icon, R.id.item_favcolour, R.id.item_studentnum};

            // Create adapter to may columns of the DB onto elemesnt in the UI.
            SimpleCursorAdapter myCursorAdapter =
                    new SimpleCursorAdapter

                            (
                                    this,        // Context
                                    R.layout.item_layout,    // Row layout template
                                    cursor,                    // cursor (set of DB records to map)
                                    fromFieldNames,            // DB Column names
                                    toViewIDs                // View IDs to put information in
                            );

            ListView myList = (ListView) findViewById(R.id.listViewFromDB);
            //  ArrayList<Object> arr = new ArrayList<>();
            //   arr.add(myCursorAdapter);
            //   myList.setAdapter(arrData);

            myList.setAdapter(myCursorAdapter);
        }

        private void registerListClickCallback() {
            ListView myList = (ListView) findViewById(R.id.listViewFromDB);
            myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View viewClicked,
                                        int position, long idInDB) {

                    updateItemForId(idInDB);
                    displayToastForId(idInDB);
                    if (playlistITYstream2 != null && idInDB == 2)
                        playVideo(idInDB);
                }
            });
        }

        private void updateItemForId(long idInDB) {
            Cursor cursor = myDb.getRow(idInDB);
            if (cursor.moveToFirst()) {
                long idDB = cursor.getLong(DBAdapter.COL_ROWID);
                String name = cursor.getString(DBAdapter.COL_NAME);
                int studentNum = cursor.getInt(DBAdapter.COL_STUDENTNUM);
                String favColour = cursor.getString(DBAdapter.COL_FAVCOLOUR);

                favColour += "!";
                myDb.updateRow(idDB, name, studentNum, favColour);
            }
            cursor.close();
            populateListViewFromDB1();
        }

and this is where i get the information from the database...

public Cursor getAddView1(){

    long i=1;
            String where = KEY_ROWID + "=" + i;
            Log.e("get add view"," "+where);
            Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
                    where, null, null, null, null, null);

            if (c != null) {
                c.moveToFirst();
            }

            return c;
        }

        public Cursor getAddView2(){

            long i=2;
            String where = KEY_ROWID + "=" + i;
            Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
                    where, null, null, null, null, null);

            if (c != null) {
                c.moveToFirst();
            }

            return c;
        }

i have searched the internet and all i found was topics on matrix adapters, would this be useful to use a matrix adapter on a cursor adapter? if not what else can i do to solve this problem.

Aucun commentaire:

Enregistrer un commentaire