无法在android中刷新listview - notifyDatSetChanged()不做任何事情

时间:2016-03-18 10:33:23

标签: java android listview refresh notifydatasetchanged

我正在构建一个简单的android客户端来使用我在服务器上制作并安装的REST API。我使用GET请求成功命中了相关端点并检索了我想要的数据。如果按下发送帖子按钮,我可以在列表中添加新帖子。我似乎无法刷新此列表,因此我必须注销并再次查看反映的更改。

以下是相关课程的代码:

PostsActivity(UI):

package com.bitcrunchy.gez.project2;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created by gerron on 05/03/16.
 */
public class PostsActivity extends AppCompatActivity {

    ListView lvPosts;
    public Button postButton;

    private PostAdapter adapter;
    private ArrayList<Post> postList;

    public Button getSubmitPostButton() {
        return postButton;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_posts);

        Intent intent = getIntent();

        final String jwt = intent.getStringExtra("jwt");

        lvPosts = (ListView) findViewById(R.id.posts_list);

        postButton = (Button) findViewById(R.id.button);

        postButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendPost("test post!", "test", null, null, null,
                        getApplicationContext(), jwt);
                adapter.notifyDataSetChanged();
            }
        });

        lvPosts.setItemsCanFocus(false);

        postList = new ArrayList<>();

        //get posts from hitting the api end point and parsing the json HAL response
        this.getPosts(this.getApplicationContext(), jwt);

    }

    public void getPosts(final Context context, final String jwt) {
        RequestQueue queue = Volley.newRequestQueue(this.getApplicationContext());

        final int loggedInUser = 25;
        StringRequest sr = new StringRequest(Request.Method.GET, "http://www.project.com/api/users/" + loggedInUser + "/posts", new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                try {
                    JSONObject hal = new JSONObject(response);

                    if (hal.has("_embedded")) {
                        JSONObject embedded = new JSONObject(hal.getString("_embedded"));

                        if (embedded.has("posts")) {
                            JSONArray posts = new JSONArray(embedded.getString("posts"));

                            for (int i = 0; i < posts.length(); i++) {
                                JSONObject post = new JSONObject(posts.getString(i));
                                PostFactory postFactory = new PostFactory();
                                postList.add(postFactory.getPost(post));
                            }

                            adapter = new PostAdapter(context.getApplicationContext(), postList, postButton);
                            lvPosts.setAdapter(adapter);


                        }
                        if (hal.has("err-msg")) {
                            System.out.println("ERROR : ".concat(hal.getString("err-msg")));
                        }
                    }
                } catch (JSONException e) {
                    System.out.println("JSON EXCEPTION : " + e.getStackTrace().toString());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println("VOLLEY ERROR : " + error);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/x-www-form-urlencoded");
                params.put("Authorization", "Bearer " + jwt);
                return params;
            }
        };
        queue.add(sr);
    }

    public void sendPost(final String text, final String url, String eventId, String groupId,
                         String attachmentId, final Context context, final String jwt) {
        final String event = eventId;
        final String attachment = attachmentId;
        RequestQueue queue = Volley.newRequestQueue(this.getApplicationContext());

        final int loggedInUser = 25;
        StringRequest sr = new StringRequest(Request.Method.POST, "http://www.project.com/api/users/" + loggedInUser + "/posts", new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                getPosts(context.getApplicationContext(), jwt);
                adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println("VOLLEY ERROR : " + error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("text", text);
                if (url != null) {
                    params.put("url", url);
                }
                if (event != null) {
                    params.put("event-id", event);
                }
                if (attachment != null) {
                    params.put("attachment-id", attachment);
                }
                return params;
            }


            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/x-www-form-urlencoded");
                params.put("Authorization", "Bearer " + jwt);
                return params;
            }
        };
        queue.add(sr);
    }


}

PostAdapter:

package com.bitcrunchy.gez.project;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
 * Created by gerron on 12/03/16.
 */
public class PostAdapter extends BaseAdapter {

    private Context mContext;
    private List<Post> posts ;
    private Button btn;


    public PostAdapter(Context mContext, List<Post> posts, Button btn) {
        this.mContext = mContext;
        this.posts = posts;
        BinaryImageResolver resolver = new BinaryImageResolver();

    }


    @Override
    public int getCount() {
        return posts.size();
    }

    @Override
    public Object getItem(int position) {
        return posts.get(position);
    }


    public void updatePostsList() {
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        return posts.get(position).render(mContext);
    }

}

SimplePost:

package com.bitcrunchy.gez.project;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by gerron on 04/03/16.
 */
public class SimplePost extends AbstractPost {

    public SimplePost(JSONObject postJSON) {
        super(postJSON);
    }

    public View render(Context mContext) {
        View v = View.inflate(mContext, R.layout.simple_post_row_layout, null);

        //get views
        ImageView profileImage = (ImageView) v.findViewById(R.id.profile_pic);

        TextView username = (TextView) v.findViewById(R.id.username);
        TextView timeOfPost = (TextView) v.findViewById(R.id.time_of_post);
        TextView dateOfPost = (TextView) v.findViewById(R.id.date_of_post);
        TextView postText = (TextView) v.findViewById(R.id.post_text);
        TextView urlView = (TextView) v.findViewById(R.id.url);

        //set view values
        Resources res = mContext.getResources();
        Drawable drawable = res.getDrawable(R.drawable.profile_image_dummy);

        profileImage.setImageBitmap(bitmap);
        username.setText("user 0");

        timeOfPost.setText(date);
        dateOfPost.setText(time);
        postText.setText(text);
        urlView.setText(url);
        v.setTag(id);

        return v;

    }



    public void generateFromJSONPost() {
        try {
            JSONObject links = new JSONObject(post.getString("_links"));
            JSONObject sender = new JSONObject(links.getString("sender"));
            getLastLeafFromUrl(sender.getString("href"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void populateValuesFromLinks() {
        generateIdFromJSONPost();
    }

}

AbstractPost:

package com.bitcrunchy.gez.project;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.AdapterView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by gerron on 16/03/16.
 */
abstract class AbstractPost implements Post {

    JSONObject post;
    int id;
    String text;
    String url;
    String time;
    String date;
    int senderId;
    Bitmap bitmap;
    Map<String,String> dateTime;

    public AbstractPost(JSONObject postJSON) {
        try {
            post = postJSON;
            id = Integer.parseInt(post.getString("id"));
            text = post.getString("text");
            if (post.getString("url") != "null") {
                url = post.getString("url");
            } else {
                url = " ";
            }
            dateTime = MySqlTimeConverter.splitIntoDateAndTime(post.getString("time_of_post"));
            date = dateTime.keySet().toArray()[0].toString();
            time = dateTime.get(date);
            populateValuesFromLinks();
            generateIdFromJSONPost();
            BinaryImageResolver resolver = new BinaryImageResolver();
            //load from api instead

            byte[] decoded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\/qhsz9zZUM3fOTzCQy7qdzdb5oqnwDyYrxvfPRxIP4\\/VMhv9skXv+\\/0Y4C8gud5k9r++CnAYALSrby2L80+NhX+P\\/Uhyrne7BAnbXWcsE\\/ig6ns\\/ntf1LxilGQoEIwS\\/dPt2bBhw4YNGzZs2LBhw4YNGzZs2LBhw4YNG+7jf70oLnsiKBNTAAAAAElFTkSuQmCC");
            bitmap = resolver.getBitmapFromBytes(decoded);
            bitmap = Bitmap.createScaledBitmap(bitmap, 42, 42, false);

        } catch (JSONException e) {
            System.out.println("JSON Exception : " + e.getStackTrace().toString());
        }
    }

    public void generateIdFromJSONPost() {
        try {
            JSONObject links = new JSONObject(post.getString("_links"));
            JSONObject sender = new JSONObject(links.getString("sender"));
            senderId = getLastLeafFromUrl(sender.getString("href"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void populateValuesFromLinks() {
        generateIdFromJSONPost();
    }

    public int getLastLeafFromUrl (String senderUrl) {
        String[] urlParts = senderUrl.split("/");
        return Integer.parseInt(urlParts[urlParts.length-1]);
    }

    @Override
    public String toString() {
        return "{id: " + id + ", text: " + text + ", url: " + url + ", time-of-post: " + time.toString() + "}";
    }

    public View render(Context mContext) {
        //do nothing, implement in subclasses.
        return null;
    };

}

我看到有人建议我需要在帖子适配器中的WITHIN onClickListener()方法的按钮上调用getView()。显然postAdapter没有一个属于UI的按钮,所以我尝试将按钮的引用传递给PostAdapter,以便onClickListener为按钮可以设置为getView()。这也没什么作用。我的列表肯定会更新,但我无法让它刷新更改。

请提供任何建议:)

1 个答案:

答案 0 :(得分:0)

尝试在onResponce

中使用adapter.notifyDataSetChanged()
 public void getPosts(final Context context, final String jwt) {
    RequestQueue queue =        Volley.newRequestQueue(this.getApplicationContext());

    final int loggedInUser = 25;
    StringRequest sr = new StringRequest(Request.Method.GET, "http://www.project.com/api/users/" + loggedInUser + "/posts", new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            try {
                JSONObject hal = new JSONObject(response);

                if (hal.has("_embedded")) {
                    JSONObject embedded = new JSONObject(hal.getString("_embedded"));

                    if (embedded.has("posts")) {
                        JSONArray posts = new JSONArray(embedded.getString("posts"));

                        for (int i = 0; i < posts.length(); i++) {
                            JSONObject post = new JSONObject(posts.getString(i));
                            PostFactory postFactory = new PostFactory();
                            postList.add(postFactory.getPost(post));
                        }

                        adapter = new PostAdapter(context.getApplicationContext(), postList, postButton);
                        lvPosts.setAdapter(adapter);


                    }
                    if (hal.has("err-msg")) {
                        System.out.println("ERROR : ".concat(hal.getString("err-msg")));
                    }
                }
            } catch (JSONException e) {
                System.out.println("JSON EXCEPTION : " + e.getStackTrace().toString());
            }

     adapter.notifyDataSetChanged() //add here
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.println("VOLLEY ERROR : " + error);
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/x-www-form-urlencoded");
            params.put("Authorization", "Bearer " + jwt);
            return params;
        }
    };
    queue.add(sr);
}