KP_
KP_

Reputation: 1866

Refreshing a listview in android

I have a listview that contains values from webservice.Each page contains only 10 listitems and the next 10 in page 2 etc.Each listitem is clickable and it contains a button which is mainly for voting.So when i click the button in list item 1 ,a value is added to webservice.

The button click codes are placed in a custom base adapter class.So that i can easily add the vote.But the problem is,When i submit the vote,i want to refresh my listview also.Suppose if iam in page no 5,refresh that listview page.

How can i refresh this listview instantly after submitting the value to webservice?

sample code for main.java

private class ProgressThreadNextPageLoading extends
AsyncTask<String, Void, String> {

    // private String Content;




    @Override
    protected void onPreExecute() {

        progressDialog = new ProgressDialog(KidsCrackMeUp.this);

        progressDialog.setMessage("Loading..Please Wait..");
        progressDialog.setIcon(R.drawable.icon);


        progressDialog.show();



    }

    @Override
    protected String doInBackground(String... urls) {

        String response = "";

        // call ur webservice here

        try {

            //  pagenum = 1;
            posts= web
            .getAllposts(pagenum);
            response = "Yes";

        } catch (Exception e) {

            e.printStackTrace();

            response = "Failure";

        }

        return response;

    }

    @Override
    protected void onPostExecute(String result) {

        // below line code is to dismiss the progress bar


        progressDialog.dismiss();
        if (posts != null) {
                adapter = new DynamicListAdapter(
                    main.this, posts
            lstPosts.setAdapter(adapter);
            adapter.notifyDataSetChanged();

            //btnState.setPressed(true);
        }

----------------------------------custom adapter class

    viewHolder.btnVoting.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
 final Dialog d = new Dialog(activity);
                d.requestWindowFeature(Window.FEATURE_NO_TITLE);
                d.setContentView(R.layout.voteselectornew);
                Button btnCategoryCancel = (Button) d
                .findViewById(R.id.btnCategoryCancel);

        Button twcVote = (Button) d
                .findViewById(R.id.btnTwcVote);

        twcVote.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String confirm = web
                .addTwcVote(UserSessionKey, Userlist.get(position).contentid);


                if (confirm.contains("Successfully")) {

d.dismiss();

}

Upvotes: 0

Views: 877

Answers (2)

deepak Sharma
deepak Sharma

Reputation: 1641

You have to notify your ListView adapter that the data has changed.

listViewAdapater.notifyDataSetChanged();

you can just reasing your adapter via the constructor with the updated array.

Upvotes: 4

waqaslam
waqaslam

Reputation: 68187

call your listview adapter's method to update the change as:

adapter.notifyDataSetChanged();

Upvotes: 3

Related Questions