jeudi 2 juillet 2015

Get ListItem Id

I have a custom ListView where I show elementss from a jsonArray that I download from server. Now I want the user be able to "like" entries. So when he clicks on an ListItem, the database of the server is updated so ne number of Likes increases. To avoid that users can like infinte times I store a row in the SQLite Database with the position of the ListItem. This works fine until there are new entries in the server database, and the positions change. How can I solve this problem? Is there a possibility to get an id from a ListItem instead of the positon or something? Here is my code:

getALlEntrysListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Cursor cursor = getLikes(dbh);
                cursor.moveToFirst();
                if (cursor.moveToFirst()) {
                    do {
                  /*check if Item is liked (=1) or not (=0)*/
                        if (Integer.parseInt(cursor.getString(2)) == 1) {
                            dbh.updateLike(dbh, position, Integer.parseInt(cursor.getString(2)), 0);
                            dislike(position);
                            cursor.close();
                        } else if (Integer.parseInt(cursor.getString(2)) == 0) {
                            dbh.updateLike(dbh, position, Integer.parseInt(cursor.getString(2)), 1);
                            likeUpload(position);
                            cursor.close();
                        } else {
                            dbh.addLike(dbh, position, 1);
                            likeUpload(position);
                            cursor.close();
                        }
                    } while (cursor.moveToNext());
                }

/*increase numer of likes on the Server Database (the dislike method works the same way):*/
public void likeUpload (int position){
    try {

        JSONObject entryClicked = jsonArray.getJSONObject(position);
        entryID = entryClicked.getString("id");

        final List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("id", entryID));

        new AsyncTask<ApiConnector, Long, Boolean>() {
            @Override
            protected Boolean doInBackground(ApiConnector... apiConnectors) {
                return apiConnectors[0].like(params);

            }
        }.execute(new ApiConnector());

        finish();
        startActivity(getIntent());

Aucun commentaire:

Enregistrer un commentaire