[英]Couple Questions Regarding the Adapter
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()); }
public void setBloglist(ArrayList<QuestionData> data){ this.data =data; notifyItemRangeChanged(0, data.size()); System.out.print("Size of the array "+data.size()); }
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?
super(itemView); Why does the ViewHolder extend super? isnt that itself?
它不是ArrayList,而是泛型
此關鍵字指向當前對象,因此我們將AdapterQuestion
對象中的數據實例變量的值設置為作為方法爭論傳遞的值
ViewHolder不會擴展名為super的類,但是您的ViewQuestion
類將擴展RecyclerView.ViewHolder
類, super(itemView);
用於調用RecyclerView.ViewHolder
構造函數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.