2016-06-30 113 views
-3

嗨,我是新來的android項目,所以我想使用回收視圖。但它不起作用。我不斷收到錯誤@此行「RecyclerAdapter adapter = new RecyclerAdapter(this,getData());」它說RecyclerAdapter(android.content.context,列表)在RecyclerAdapter不能應用到我的包類,請幫助RecyclerView給出錯誤

statement.java 


import android.content.Context; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.support.v7.widget.DefaultItemAnimator; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.support.v7.widget.Toolbar; 

import java.util.ArrayList; 
import java.util.List; 

import nnnnn.R; 
import nnnnnn.RecyclerAdapter; 
import nnnnnnnnnnnnn.Landscape; 




public class Statements extends Fragment { 
    Toolbar toolbar; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.statements,container,false); 

     toolbar = (Toolbar) v.findViewById(R.id.toolbar); 
     toolbar.setTitle("Statements"); 
     toolbar.inflateMenu(R.menu.menu_main); 

     setUpRecyclerView(); 
     return v;} 

    private void setUpRecyclerView() { 


     RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); 
     RecyclerAdapter adapter = new RecyclerAdapter(this.getActivity(),Landscape.getData()); 
     recyclerView.setAdapter(adapter); 

     final LinearLayoutManager mLinearLayoutManagerVertical = new LinearLayoutManager(this.getActivity()); // (Context context, int spanCount) 
     mLinearLayoutManagerVertical.setOrientation(LinearLayoutManager.VERTICAL); 
     recyclerView.setLayoutManager(mLinearLayoutManagerVertical); 

     recyclerView.setItemAnimator(new DefaultItemAnimator()); // Even if we dont use it then also our items shows default animation. #Check Docs 
    } 




} 

RecyclerAdapter代碼如下

RecyclerAdapter 
import android.content.Context; 
import android.support.v7.widget.RecyclerView; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 

import java.util.List; 

import xx.xx.xx.xx.xx.R; 
import xx.xx.xx.xx.model.Landscape; 


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

    private static final String TAG = RecyclerAdapter.class.getSimpleName(); 

    private List<Landscape> mDataList; 
    private LayoutInflater inflater; 

    public RecyclerAdapter(Context context, List<Landscape> data) { 
     this.mDataList = data; 
     this.inflater = LayoutInflater.from(context); 
    } 



    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     Log.i(TAG, "onCreateViewHolder"); 

     View view = inflater.inflate(R.layout.statementlist, parent, false); 
     MyViewHolder holder = new MyViewHolder(view); 
     return holder; 
    } 

    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 
     Log.i(TAG, "onBindViewHolder" + position); 

     Landscape current = mDataList.get(position); 
     holder.setData(current, position); 
     holder.setListeners(); 
    } 

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

    public void removeItem(int position) { 
     mDataList.remove(position); 
     notifyItemRemoved(position); 
//  notifyItemRangeChanged(position, mDataList.size()); 
//  notifyDataSetChanged(); 
    } 

    public void addItem(int position, Landscape landscape) { 
     mDataList.add(position, landscape); 
     notifyItemInserted(position); 
//  notifyItemRangeChanged(position, mDataList.size()); 
//  notifyDataSetChanged(); 
    } 

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
     TextView title; 
     ImageView imgThumb, imgDelete, imgAdd; 
     int position; 
     Landscape current; 

     public MyViewHolder(View itemView) { 
      super(itemView); 
      title  = (TextView) itemView.findViewById(R.id.tvTitle); 
      imgThumb = (ImageView) itemView.findViewById(R.id.img_row); 
      imgDelete = (ImageView) itemView.findViewById(R.id.img_row_delete); 
      imgAdd  = (ImageView) itemView.findViewById(R.id.img_row_add); 
     } 

     public void setData(Landscape current, int position) { 
      this.title.setText(current.getTitle()); 
      imgThumb.setImageResource(current.getImageID()); 
      this.position = position; 
      this.current = current; 
     } 

     public void setListeners() { 
      imgDelete.setOnClickListener(MyViewHolder.this); 
      imgAdd.setOnClickListener(MyViewHolder.this); 
      imgThumb.setOnClickListener(MyViewHolder.this); 
     } 

     @Override 
     public void onClick(View v) { 
//   Log.i("onClick before operation", position + " " + mDataList.size()); 
      switch (v.getId()) { 
       case R.id.img_row_delete: 
        removeItem(position); 
        break; 

       case R.id.img_row_add: 
        addItem(position, current); 
        break; 
      } 
//   Log.i("onClick after operation", mDataList.size() + " \n\n" + mDataList.toString()); 
     } 
    } 
} 

回答

2

你的「本」爲合格RecyclerAdapter的第一個參數。 「這個」是片段類。片段類不從Context擴展,因此不能像這樣使用。

嘗試「this.getActivity()」。這可能會導致null,因此最好將其放在Fragment子類的「onActivityCreated」方法中(而不是「onCreateView」方法)

+0

感謝@fishstix現在運行應用程序後它崩潰了這是給出的錯誤「java.lang.NullPointerException:嘗試調用虛擬方法」android.view.View android .support.v7.widget.RecyclerView.findViewById(int)'在空對象引用「 –

+0

似乎你也有一個錯誤在這裏:recyclerView =(RecyclerView)recyclerView.findViewById(R.id.recyclerView); (你應該只是在Fragment類中調用findViewById()...不在當前爲null的recyclerview實例上) – FishStix

+0

是的,當我刪除recyclerView.findViewByid時出現錯誤,顯示無法解析方法 –

0

您需要重寫OnActivitycreated才能調用getActivity。在OnActivitycreated中調用setUpRecyclerView並在findViewById之前添加getActivity.並且它將工作

+0

嘗試自己學習一點,我給你所有的鑰匙 –