2016-03-22 8 views
0

実際にタイトルは私の問題を記述しています。 アダプターをrecyclerViewに設定していますが、まだ問題が表示されています。 私はアダプターをセットする方法から呼び出す場所は問題ではなく、事実...(あるいはそれは問題ではないかもしれない)と思います。アダプターが取り付けられていません。 RecyclerViewでレイアウトをスキップする

アダプタを設定する方法はshowData()です。

編集: は(THXのRohitアー​​ヤ)もう何のエラーはありませんが、私はまだ(空白のページがあります)写真を見ることができません。

コード:

MainFragmentクラス:

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

BoardAdapterクラス:

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

fragment_main:

<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> 

board_row:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <android.support.v7.widget.CardView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <ImageView 
       android:id="@+id/board_row_image" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:src="@drawable/photo" /> 

      <TextView 
       android:id="@+id/board_row_name" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Trap" /> 


     </LinearLayout> 


    </android.support.v7.widget.CardView> 


</LinearLayout> 
+0

fragment_main_layout.xmlを表示できますか? – z3n105

+0

OK、投稿が編集されました。 –

答えて

0

RecyclerViewにはアタッチされていないアダプタがあります。 RecyclerViewを空のリストで初期化し、データを受信した後にアダプタを設定し、リストを更新してアダプタのnotifyDataSetChangedに電話をかけることができます。 onCreateView

0

ここ
manager = new LinearLayoutManager(getContext()); 

recyclerView.setLayoutManager(manager); 

adapter = new BoardAdapter(APIConfig.names, APIConfig.bitmaps); 
recyclerView.setAdapter(adapter); 

APIConfig.namesAPIConfig.bitmapsが空になります(ないnull)。

これらの値をonPostExecuteAsyncTaskに更新し、アダプタでnotifyDataSetChangedを呼び出します。

+0

エラーが発生しました:java.lang.RuntimeException:アクティビティを開始できませんComponentInfo {com.sarusi.amit.testa/com.sarusi.amit.smarts.MainActivity}:java.lang.NullPointerException:長さを取得しようとしていますnull配列 –

+0

これらの2つの配列を最初に 'APIConfig.names、APIConfig.bitmaps'を初期化します。 –

+0

もうエラーはありませんが、画像が表示されません。これはアダプターのせいだと思いますか? –

関連する問題