实际上标题描述了我的问题。 我正在为recyclerView设置适配器,但问题仍然存在。 我认为我调用方法来设置适配器的地方不是问题而是事实......(或者可能不是问题)。
设置适配器的方法是showData();
修改 没有错误(THX Rohit Arya),但我仍然看不到照片(有空白页)。
代码:
MainFragment类:
node_modules
BoardAdapter类:
package com.sarusi.amit.smarts;
import android.support.v4.app.Fragment;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* A simple {@link Fragment} subclass.
*/
public class MainFragment extends Fragment {
private RecyclerView recyclerView;
private RecyclerView.LayoutManager manager;
private RecyclerView.Adapter adapter;
private APIConfig apiConfig;
public MainFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
APIConfig.names = new String[1];
APIConfig.bitmaps = new Bitmap[1];
recyclerView = (RecyclerView) view.findViewById(R.id.fragment_main_recyclerView);
recyclerView.setHasFixedSize(true);
manager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(manager);
adapter = new BoardAdapter(APIConfig.names, APIConfig.bitmaps);
recyclerView.setAdapter(adapter);
getBoardsData();
return view;
}
private void getBoardsData() {
class ConnectionTask extends AsyncTask<String, Void, String> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(getContext(), "Fetching Data...", "A Trap", false, false);
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressDialog.dismiss();
parseJSON(s);
showData();
adapter.notifyDataSetChanged();
}
@Override
protected String doInBackground(String... params) {
BufferedReader bufferedReader;
try {
URL url = new URL(apiConfig.IMAGES_URL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
}
ConnectionTask connectionTask = new ConnectionTask();
connectionTask.execute();
}
public void showData () {
adapter = new BoardAdapter(APIConfig.names, APIConfig.bitmaps);
recyclerView.setAdapter(adapter);
}
private void parseJSON(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray(apiConfig.TAG_JSON_ARRAY);
apiConfig = new APIConfig(jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject JSO = jsonArray.getJSONObject(i);
APIConfig.names[i] = JSO.getString(apiConfig.TAG_NAME);
APIConfig.bitmaps[i] = base64ToBitmap(JSO.getString(apiConfig.TAG_IMAGE));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private Bitmap base64ToBitmap(String b64) {
byte[] imageAsBytes = Base64.decode(b64.getBytes(), Base64.DEFAULT);
return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
}
}
fragment_main:
package com.sarusi.amit.smarts;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by amit on 3/21/16.
*/
public class BoardAdapter extends RecyclerView.Adapter<BoardAdapter.ViewHolder> {
List<Board> boards;
public BoardAdapter(String[] names, Bitmap[] images) {
super();
boards = new ArrayList<>();
for (int i = 0; i < names.length; i++) {
Board board = new Board();
board.setImage(images[i]);
board.setName(names[i]);
boards.add(board);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.board_row, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Board board = boards.get(position);
holder.imageView.setImageBitmap(board.getImage());
holder.name.setText(board.getName());
}
@Override
public int getItemCount() {
return boards.size();
}
//The class for the viewHolder
class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView name;
public ViewHolder(View itemView) {
super(itemView);
this.imageView = (ImageView) itemView.findViewById(R.id.board_row_image);
this.name = (TextView) itemView.findViewById(R.id.board_row_name);
}
}
}
board_row:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.sarusi.amit.smarts.MainFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/fragment_main_recyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
答案 0 :(得分:0)
有一段时间RecyclerView
没有适配器到达。
您可以在使用空列表初始化RecyclerView
时设置适配器,然后在收到数据后,更新列表并调用适配器的notifyDataSetChanged
。
答案 1 :(得分:0)
在onCreateView
:
manager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(manager);
adapter = new BoardAdapter(APIConfig.names, APIConfig.bitmaps);
recyclerView.setAdapter(adapter);
此处,APIConfig.names
和APIConfig.bitmaps
将为空(不 null
)。
更新onPostExecute
的这些值AsyncTask
并在适配器上调用notifyDataSetChanged
。