addy123
addy123

Reputation: 524

Increment counter on listview

What I want is that I'm trying to increment a counter everytime a user clicks on a item in listview. However each time the counter gets incremented only for the user which created that particular item in the listview. Please help, I want to increment the counter on each of the listItem where user clicks. Thanks.

             listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(final AdapterView<?> parent,
                final View view, final int position, final long id) {

            try {
                caseID = ((CaseModel) parent.getAdapter().getItem(position))
                        .getCaseId();
                caseTittle = ((CaseModel) parent.getAdapter().getItem(
                        position)).getTitle();
                caseDescription = ((CaseModel) parent.getAdapter().getItem(
                        position)).getDescription();
                caseCreator = ((CaseModel) parent.getAdapter().getItem(
                        position)).getNameofcreator();
                userId = ((CaseModel) parent.getAdapter().getItem(position))
                        .getUserId();
            } catch (Exception e) {
                // TODO: handle exception
            }

            if (caseID != null) {
                Intent gotoDetail = new Intent(
                        DoxinetCaseListActivity.this,
                        DoxinetCaseDiscussionForum.class);
                gotoDetail.putExtra("caseId", caseID);
                gotoDetail.putExtra("caseTitle", caseTittle);
                gotoDetail.putExtra("caseDescription", caseDescription);
                gotoDetail.putExtra("nameCreator", caseCreator);
                gotoDetail.putExtra("userId", userId);
                gotoDetail.putExtra("readStatus", true);
                callToCaseStats();
                callToIncrementCounter();
                startActivity(gotoDetail);
            } else {
                return;
            }

        }
    });

Here is the method callToIncrementCounter();

    protected void callToIncrementCounter() {
    // TODO Auto-generated method stub

    // TODO Auto-generated method stub
    ParseQuery<ParseObject> query = ParseQuery.getQuery("case");
    //query.whereEqualTo("objectId", caseID);
    try {
        ob = query.find();

        System.out.println("In counter userob" + ob);

        for (ParseObject ob1 : ob) {

            counter = Integer.parseInt(ob1.get("counter").toString());
            System.out.println("In counter " + counter);

            counter = counter + 1;
            ob1.put("counter", counter );
            ob1.saveInBackground();
            System.out.println("In counter " + counter);
        }
    }

    catch (Exception d) {
        System.out.println("In exception");
        d.getMessage();
    }

}

Upvotes: 0

Views: 1169

Answers (2)

mhemdan
mhemdan

Reputation: 111

You are trying to make a counter for each item in the listView, if I got it right, and that is what you meant, please find a solution for this situation.

You are using the default adapter for the listView while you should use a custom adapter.

Then you should fill the adapter with list<Objects>, this Object should contain counter and item.

And then on onItemClick method you should update the counter for clicked item and then call adapter.notifyDataSetChanged(); this method will update the listview to show the updated counter for each item.

Upvotes: 1

Pedro Vaz
Pedro Vaz

Reputation: 820

You can try to create a Map and use the position in the ListView as a key. The value of the map will be the count for each row.

Upvotes: 1

Related Questions