0

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();

}
KP_
  • 1,854
  • 25
  • 43
  • possible duplicate of [Android List view refresh](http://stackoverflow.com/questions/4088862/android-list-view-refresh) – Lalit Poptani Jan 16 '12 at 09:29

2 Answers2

4

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.

deepak Sharma
  • 1,641
  • 10
  • 23
3

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

adapter.notifyDataSetChanged();
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • Should i check any condition for this? – KP_ Jan 16 '12 at 09:26
  • once the voting operation is completed, simply update the data used in adapter class and then call adapter's notifyDataSetChanged to update the views in your list automatically – waqaslam Jan 16 '12 at 09:30
  • Sorry,im asking is where should i place this code? The listview is loaded at the start of the activity.So i dont know where to place this notify. – KP_ Jan 16 '12 at 09:33
  • when you press the button and the block where its calling operation is completed, include it there at the bottom – waqaslam Jan 16 '12 at 09:35
  • actually my voting button is in custom adapter class. – KP_ Jan 16 '12 at 09:38
  • good, then call it as this.notifyDataSetChanged() or your_adapter_class.this.notifyDataSetChanged() – waqaslam Jan 16 '12 at 09:40