2016-10-12 12 views
0

私はネットワークからvolleyを使って解析したJsonobjectsを追加しようとしていますが、そのカードビューをrecyclerViewに追加しようとしています。 私search.java:CardViewにvolson Jsonobjectsを追加し、recyclerViewにcardViewを追加します。

public class search extends AppCompatActivity { 
    public RecyclerView recyclerView; 
    RequestQueue requestQueue; 
    itemsAdapter itemsAdapter; 
    @Override 
    protected void attachBaseContext(Context newBase) { 
     super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_search); 
     String url = "http://dl.naserpour.ir/WEBSERVICE/KETABSARA/all.php"; 
     recyclerView = (RecyclerView) findViewById(R.id.recyclerview); 
     recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
     requestQueue = Volley.newRequestQueue(this); 
     JsonArrayRequest req = new JsonArrayRequest(url, new Response.Listener<JSONArray>() { 
      @Override 
      public void onResponse(JSONArray response) { 
       final List<items> items = new ArrayList<>(); 
       for (int i = 0; i <= response.length(); i++) { 
        try { 
         JSONObject obj = response.getJSONObject(i); 
         String NAME = obj.getString("name"); 
         String WRITER = obj.getString("writer"); 
         String IMAGE = "http://dl.naserpour.ir/WEBSERVICE/KETABSARA/upload/images/images" + obj.getString("img"); 
         items.add(new items(NAME, WRITER, IMAGE)); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
        itemsAdapter = new itemsAdapter(items); 
       } 
       recyclerView.setAdapter(itemsAdapter); 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 

      } 
     }); 
     requestQueue.add(req); 

    } 

    public class items { 
     private String name, writer, image; 

     public items(String name, String writer, String image) { 
      this.name = name; 
      this.writer = writer; 
      this.image = image; 
     } 
    } 

    private class itemHolder extends RecyclerView.ViewHolder { 
     private TextView searchname, searchwriter; 
     private ImageView searchimage; 
     public itemHolder(View itemView) { 
      super(itemView); 
      searchimage = (ImageView)findViewById(R.id.searchimage); 
      searchname = (TextView)findViewById(R.id.searchname); 
      searchwriter = (TextView)findViewById(R.id.searchwriter); 

     } 

    } 

    private class itemsAdapter extends RecyclerView.Adapter<itemHolder> { 
     private List<items> items; 

     public itemsAdapter(List<items> items) { 
      this.items = items; 
     } 

     @Override 
     public itemHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      LayoutInflater inflater = LayoutInflater.from(search.this); 
      View v = inflater.inflate(R.layout.searchlist, parent, false); 
      return new itemHolder(v); 
     } 

     @Override 
     public void onBindViewHolder(final itemHolder holder, int position) { 
      items item = items.get(position); 
      holder.searchname.setText(item.name); 
      holder.searchwriter.setText(item.writer); 
      ImageRequest req = new ImageRequest(item.image, new Response.Listener<Bitmap>() { 
       @Override 
       public void onResponse(Bitmap response) { 
        holder.searchimage.setImageBitmap(response); 
       } 
      }, 0, 0, ImageView.ScaleType.CENTER_CROP, Bitmap.Config.ARGB_8888, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        error.printStackTrace(); 
       } 
      }); 
      requestQueue.add(req); 
     } 

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

私のレイアウト:

<?xml version="1.0" encoding="utf-8"?> 
<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:paddingBottom="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    android:layoutDirection="rtl" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="ir.naserpour.ketabsara.search"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/search" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:background="@drawable/shape" 
     android:hint="جستجو" 
     android:drawableLeft="@android:drawable/ic_menu_search" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" /> 
    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

私SEARCHLISTレイアウト:

<?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="match_parent" 
    android:orientation="vertical" 
    android:layoutDirection="rtl"> 

    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="180dp"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal"> 
      <ImageView 
       android:layout_width="120dp" 
       android:layout_height="match_parent" 
       android:id="@+id/searchimage" 
       android:scaleType="centerCrop" /> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:gravity="center_vertical" 
       android:paddingRight="10dp"> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:text="" 
        android:id="@+id/searchname" 
        android:layout_weight="1" 
        android:gravity="center_vertical|right" /> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:text="" 
        android:id="@+id/searchwriter" 
        android:layout_weight="1" 
        android:gravity="center_vertical|right" /> 
      </LinearLayout> 
     </LinearLayout> 
    </android.support.v7.widget.CardView> 

</LinearLayout> 

私logcat(エラーは、ライン112上の129である):

Process: ir.naserpour.ketabsara, PID: 15311 
java.lang.NullPointerException 
    at ir.naserpour.ketabsara.search$itemsAdapter.onBindViewHolder(search.java:129) 
    at ir.naserpour.ketabsara.search$itemsAdapter.onBindViewHolder(search.java:112) 
    at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471) 
    at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504) 
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741) 
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617) 

あなたは私を助けてくれますか? これとともに?

+0

私はコードを調べた後、あなたはBindViewHolderメソッドの 'ImageRequest'行にエラーがあるのは正しいのですか? –

答えて

1

エラーがitemHolderクラスにあった場合、itemHolderのビューオブジェクトを渡すことによってViewのIDを見つける必要があります。

 private class itemHolder extends RecyclerView.ViewHolder { 
     private TextView searchname, searchwriter; 
     private ImageView searchimage; 
     public itemHolder(View itemView) { 
     super(itemView); 
     searchimage = (ImageView)itemView.findViewById(R.id.searchimage); 
     searchname = (TextView)itemView.findViewById(R.id.searchname); 
     searchwriter = (TextView)itemView.findViewById(R.id.searchwriter); 
     } 
     } 
関連する問題