2016-04-03 73 views
0
public class AdapterQuestion extends RecyclerView.Adapter<AdapterQuestion.ViewQuestion>{ 
private LayoutInflater mLayoutInflater; 
//this is an arrayList of questionData objects 
private ArrayList<QuestionData> data =new ArrayList<>(); 
//Created the layoutInflator 
public AdapterQuestion(Context context){ 
    //get from context 
    mLayoutInflater=LayoutInflater.from(context); 

} 
public void setBloglist(ArrayList<QuestionData> data){ 
    this.data =data; 
    notifyItemRangeChanged(0, data.size()); 
    System.out.print("Size of the array "+data.size()); 
} 
@Override 
public ViewQuestion onCreateViewHolder(ViewGroup parent, int viewType) { 
    //inflates the customQuestion view or converts it to java code 
    View view= mLayoutInflater.inflate(R.layout.customquestion, null); 
    //We now want to convert the View into a ViewQuestion, view Question takes 
    //a view so we pass the view into view question and then return it. 

    ViewQuestion holder=new ViewQuestion(view); 
    return holder; 
} 
//ViewGroup parent and ViewType are not being assigned. 
@Override 
public void onBindViewHolder(ViewQuestion holder, int position) { 
    //here we need to bind the data to our view, there is currently no Data! 
    //We need to get the data from our JSON 
    //Parameters is a ViewHolder and a Position 
    QuestionData currentBlog= data.get(position); 
    holder.answerText.setText(currentBlog.getMtext()); 
    holder.answerId.setText(currentBlog.getId()); 
    holder.mVotes.setText(currentBlog.getVotes()); 
    holder.mLikeButton.setTag(currentBlog); 
} 

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

public class ViewQuestion extends RecyclerView.ViewHolder{ 
    //once we create it once the reclycer view will automatically recycle it 
    private TextView answerText; 
    private TextView answerId; 
    private TextView mVotes; 
    private LikeButton mLikeButton; 

    public ViewQuestion (View itemView){ 
     super(itemView); 
     //here we are finding the views by their ID 
     answerText=(TextView)itemView.findViewById(R.id.answerText); 
     answerId=(TextView)itemView.findViewById(R.id.answerId); 
     mVotes=(TextView)itemView.findViewById(R.id.VoteTextView); 
     mLikeButton=(LikeButton)itemView.findViewById(R.id.heart_buttons); 

     mLikeButton.setOnLikeListener(new OnLikeListener() { 
      @Override 
      public void liked(LikeButton likeButton) { 
       Voting vote = new Voting(); 
       vote.onUpVote(convertToString(),getAdapterPosition(),ViewQuestion.this); 
        System.out.print("Adapter  Position"+getAdapterPosition()); 
      } 
      @Override 
      public void unLiked(LikeButton likeButton) { 
       Voting onDown=new Voting(); 
       onDown.onDownVote(convertToString(), getAdapterPosition(), ViewQuestion.this); 

      } 
     }); 

    } 
    public String getVoteView(){ 
     String voteView=mVotes.getText().toString(); 
     return voteView; 
    } 
    public String convertToString(){ 
     String converted=answerId.getText().toString(); 
     return converted; 
    } 
    public int convertToInt(){ 
     String converted=answerId.getText().toString(); 
     int ConvertedInt=Integer.parseInt(converted); 
     return ConvertedInt; 
    } 
} 
} 

1.首先,爲什麼我們將RecyclerView.Adapter作爲arrayList擴展? 2. public void setBloglist(ArrayList<QuestionData> data){ this.data =data; notifyItemRangeChanged(0, data.size()); System.out.print("Size of the array "+data.size()); } 在上面的方法中,我們傳入一個ArrayList,並且將Arraylist的數據設置爲傳入的數據,但是我們不必在ViewHolder中設置數據? 3. super(itemView); Why does the ViewHolder extend super? isnt that itself? 有關適配器的幾個問題

回答

0
  1. 它ArrayList不是它是泛型

  2. 該關鍵字是指當前對象,所以我們正在設置數據的值在AdapterQuestion的對象到 實例變量作爲方法爭論通過的值

  3. ViewHolder並不擴展名爲super的類,但您的ViewQuestion類擴展了RecyclerView.ViewHolder類,super(itemView);用於ca RecyclerView.ViewHolder構造函數

+0

什麼是泛型? – eli

+0

哦,它使用Recycler.viewholders構造函數嗎? – eli

+0

是的,它使用的是構造函數.. [在java中的泛型](https://docs.oracle.com/javase/tutorial/java/generics/types.html) – Sam