Android使用RecyclerView分段进行远程数据提取

时间:2017-09-01 13:22:03

标签: java android android-layout listview android-fragments

我正在构建我的第一个Android应用程序,我将使用三个按钮附加到片段的底部导航。第一个片段( MessageFragment )负责使用Volley和Gson库列出来自服务器的所有消息。

我正在尝试从服务器加载在表单中返回 JSON 数据的邮件。

[
 {
  "id": "1",
  "message": "Some message",
  "created_at": "2017-08-23 12:38:31"
 },
 {
 "id": "2",
 "message": "Another message",
 "created_at": "2017-08-23 12:39:28"
 },
 ....
]  

我在片段中找到了一个很好的RecyclerView示例,但遗憾的是,在此示例中,数据并非来自远程。

https://developer.android.com/samples/RecyclerView/src/com.example.android.recyclerview/RecyclerViewFragment.html

当我使用下面的代码时,有人可以帮助我解释为什么我没有在适配器中获取数据吗?

我该怎么办才能修复它?

这是我的代码

MessageFragment.java

public class MessageFragment extends Fragment {

 private Context mContext;

 private static final String ENDPOINT = "http://10.0.2.2/message/list?userid=1";
 List<Message> msgs = new ArrayList<Message>();
 private RequestQueue requestQueue;
 private Gson gson;


@Override
public void onAttach(Context context) {
    super.onAttach(context);
    this.mContext=context;
}

public static MessageFragment newInstance() {
    MessageFragment fragment = new MessageFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestQueue = Volley.newRequestQueue(this.mContext);
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setDateFormat("M/d/yy hh:mm a");
    gson = gsonBuilder.create();
    fetchMessages();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_message, container, false);
    RecyclerView recyclerView = view.findViewById(R.id.rvMessages);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); //set layoutManger
    MessageRVAdapter mAdapter = new MessageRVAdapter(msgs);//create an adapter
    recyclerView.setAdapter(mAdapter);  // set adapter
    return view;
}

private void fetchMessages() {
    System.out.println("Now in fetchMessages");
    StringRequest request = new StringRequest(Request.Method.GET, ENDPOINT, onPostsLoaded, onPostsError);
    requestQueue.add(request);
}

private final Response.Listener<String> onPostsLoaded = new Response.Listener<String>() {

    @Override
    public void onResponse(String response) {
        msgs = Arrays.asList(gson.fromJson(response, Message[].class));
        System.out.println("inside " + msgs);
    }
};

private final Response.ErrorListener onPostsError = new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        System.out.println("Messages load fail");
        Log.e("PostActivity", error.toString());
    }
};

}

MessageRVAdapter.php

public class MessageRVAdapter extends RecyclerView.Adapter<MessageRVAdapter.MyViewHolder> {

private List<Message> messages;

public MessageRVAdapter(List<Message> messages) {
    this.messages = messages;
}


public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView message;
    public MyViewHolder(View view) {
        super(view);
        message = (TextView) view.findViewById(R.id.rvMessage);
    }
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Message message = messages.get(position);
    holder.message.setText(message.getMessages());
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.rvmessages_row, parent, false);
    return new MyViewHolder(itemView);
}



@Override
public int getItemCount() {
    return messages.size();
}
}

1 个答案:

答案 0 :(得分:0)

在装载的帖子上设置 Recycler Adapter

private final Response.Listener<String> onPostsLoaded = new Response.Listener<String>() {

    @Override
    public void onResponse(String response) {
        msgs = Arrays.asList(gson.fromJson(response, Message[].class));
        System.out.println("inside " + msgs);
        MessageRVAdapter mAdapter = new MessageRVAdapter(msgs);//create an adapter
        recyclerView.setAdapter(mAdapter);  // set adapter
    }
};

也可能需要解析来自 onResponse JSON数据,无论你需要从响应字符串显示解析并显示它。