lundi 2 mai 2016

Displaying SQLite row values if the row ID matches the listView item ID

here is my scenario:

lets say I have a SQLite table which displays some rows with and ID that is of course auto incremented.

public static final String DataTableName = "jobsTable";
    public static final String ColID = "ID";
    public static final String ColPlace = "placeName";
    public static final String ColPosition = "position";
    public static final String ColStartingTime = "startingTime";
    public static final String ColHours = "hours";
    public static final String ColAddress = "address";
    public static final String ColPhoneNumber = "phoneNumber"; 

the "placeName" and "position" and "startingTime" are used to populate listView cells like so: Click here to see screen capture

Now my question is, how can I display the values of a Table row, whose ID matches that of list item? so when the user presses on an item, the table row that share the same ID will be displayed. thanks

here is what I've tried so far (only the onItemClickedListner method) but I know I'm not retrieving the data right

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


                // get the IDs of each column in the database
                String [] IDs = activity.getAllColumnsIDs().toArray(
                        new String[activity.getAllColumnsIDs().size()]);

                // loop throught the IDs and see if they match the listView
                // item ids. if yes, display the detail
                DataBaseHelper summerJobDB;
                summerJobDB = new DataBaseHelper(activity);
                for (int i = 0; i >= IDs.length; i++) {
                    if (i == id) {


                        Cursor res = summerJobDB.getAllData();
                        if (res.getCount() ==0){
                            // show some message
                            showMessage("Error", "Nothing found");
                            return;
                        }
                        StringBuffer buffer = new StringBuffer();
                        // append the values from the database to the buffer.
                        // this is based on the index number of the columns
                        while (res.moveToNext()) {
                            buffer.append(res.getString(1)+ "/" + res.getString(2) +"\n");
                            buffer.append("Starts on " + res.getString(3)+"\n\n");
                            buffer.append("Address: \n"+ res.getString(5)+"\n\n");
                            buffer.append("Phone: \n"+ res.getString(6)+"\n\n");
                            buffer.append("Hours: \n"+ res.getString(4)+"\n\n");
                        }
                        // show all data here

                        showMessage("Data",buffer.toString());

                    }
                }

            }

Aucun commentaire:

Enregistrer un commentaire