TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Updating ListView after deleting MySQL Data

Here is the delete call in my context menu:

    @Override
    public boolean onContextItemSelected(MenuItem item) {

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
                .getMenuInfo();
        int clickedPosition = info.position;
        tvInt = reviews.get(clickedPosition);

        switch (item.getItemId()) {
        case R.id.Delete:

            new DeleteCommentTask().execute();

            reviews.remove(clickedPosition);
            adapter.notifyDataSetChanged();

            Toast.makeText(getActivity(), "Review Deleted",
                    Toast.LENGTH_SHORT).show();

            return true;

        }
        return false;

    }

I do verify that in my DeleteCommentTask it does remove the comment from my database. But ListView not being updated?

UPDATE: Here is my adapter I am using:

            MyReviewObject co = new MyReviewObject[reviews.size()];
            int index = 0;

            for (@SuppressWarnings("unused")
            String i : reviews) {
                co[index] = new MyReviewObject(datelist.get(index),
                        reviews.get(index), items.get(index),
                        cats.get(index));
                index++;
            }

            adapter = new MyReviewAdapter(getActivity(), co);
            setListAdapter(adapter);

Upvotes: 0

Views: 309

Answers (2)

TheLettuceMaster
TheLettuceMaster

Reputation: 15734

The reason this is not working is because of this: "you need to remove/delete item in co array. That is not possible/recommended, use ArrayList of "MyReviewObject" in Adapter."

I need to use an ArrayList which can shrink dynamically, an Array can't. Also, I was just removing a single String ArrayList. Inside my Array object are multiple ArrayList's. So I should be removing the entire object instead.

Upvotes: 0

Rohit Sharma
Rohit Sharma

Reputation: 13815

See the updated answer

Don't do this

reviews.remove(clickedPosition);

Just remove the comment from the data source from where it was getting used in the getView of adapter. say from a Arraylist of Strings.

After this only notifying the adapter will going to do the trick

Try executing this code in the onPostExecute of DeleteCommentTask

adapter.notifyDataSetChanged();
Toast.makeText(getActivity(), "Review Deleted", Toast.LENGTH_SHORT).show();

Try Passing Argument to the Execute method of DeleteCommentTask which will notify which position to remove.

Upvotes: 1

Related Questions