目前我正在制作一个新闻阅读器应用程序 1 - 最新消息 2 - 未读新闻
基本上我的Recyclerview有两个适配器,一个用于最新消息,另一个用于未读消息。现在,未读新闻适配器只是我的副本,看看它是如何表现的,一旦我确定适配器工作,我将对未读新闻进行排序。最近的适配器工作正常,但当我点击并运行未读适配器时,它给了我这个错误:
11-09 06:16:11.766 2534-2534/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.azrie.dummyvoice, PID: 2534
java.lang.ClassCastException: com.example.azrie.dummyvoice.RecyclerView_Adapter_RecentNews$ViewHolder cannot be cast to com.example.azrie.dummyvoice.RecyclerView_Adapter_UnreadNews$ViewHolder
at com.example.azrie.dummyvoice.RecyclerView_Adapter_UnreadNews.onBindViewHolder(RecyclerView_Adapter_UnreadNews.java:18)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5768)
这是我最近的新闻适配器类:
public class RecyclerView_Adapter_RecentNews extends RecyclerView.Adapter<RecyclerView_Adapter_RecentNews.ViewHolder>{
ArrayList<Store_News_XML> newsXML;
Context context;
public RecyclerView_Adapter_RecentNews(Context context, ArrayList<Store_News_XML> newsXML){
this.context = context;
this.newsXML = newsXML;
//this.recyclerView = recyclerView;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
TextView text_title;
TextView text_description;
TextView text_date;
View view;
String currentLink;
public ViewHolder(View itemView) {
super(itemView);
text_title = (TextView) itemView.findViewById(R.id.title_text_r);
text_description = (TextView) itemView.findViewById(R.id.title_description_r);
text_date = (TextView) itemView.findViewById(R.id.date_text_r);
view = itemView;
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recycler_view_card,parent,false);
final ViewHolder holder = new ViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
newsXML.get(holder.getLayoutPosition()).setRead(true);
/*for(int i = 0; i < newsXML.size(); i++){
Log.d("News " + i + " ", " "+ newsXML.get(i).getRead() );
}*/
Intent intent = new Intent(view.getContext(),Activity_WebView.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("link", newsXML);
bundle.putInt("position",holder.getLayoutPosition());
intent.putExtra("bundle",bundle);
view.getContext().startActivity(intent);
}
});
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Store_News_XML current = newsXML.get(position);
holder.text_title.setText(current.getTitle());
holder.text_description.setText(current.getDescription());
holder.text_date.setText(current.getPubDate());
holder.currentLink = current.getLink();
// Log .d("News Read Outside " + position ,"" + current.getRead() + " / News Title : " + current.getTitle());
if(!newsXML.get(position).getRead()){
//Log .d("News Not Read " + position ,"" + current.getRead() + " / News Title : " + current.getTitle());
holder.text_title.setTextColor(Color.parseColor("#000000"));
holder.text_description.setTextColor(Color.parseColor("#000000"));
holder.text_date.setTextColor(Color.parseColor("#000000"));
}
else
{
//Log .d("News Read " + position ,"" + current.getRead() + " / News Title : " + current.getTitle());
holder.text_title.setTextColor(Color.parseColor("#BAC1C1"));
holder.text_description.setTextColor(Color.parseColor("#BAC1C1"));
holder.text_date.setTextColor(Color.parseColor("#BAC1C1"));
}
}
@Override
public int getItemCount() {
return newsXML.size();
}
}
这是我的未读新闻适配器类:
public class RecyclerView_Adapter_UnreadNews extends RecyclerView.Adapter<RecyclerView_Adapter_UnreadNews.ViewHolder>{
ArrayList<Store_News_XML> newsXML;
Context context;
public RecyclerView_Adapter_UnreadNews(Context context, ArrayList<Store_News_XML> newsXML){
this.context = context;
this.newsXML = newsXML;
//this.recyclerView = recyclerView;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
TextView text_title;
TextView text_description;
TextView text_date;
View view;
String currentLink;
public ViewHolder(View itemView) {
super(itemView);
text_title = (TextView) itemView.findViewById(R.id.title_text_r);
text_description = (TextView) itemView.findViewById(R.id.title_description_r);
text_date = (TextView) itemView.findViewById(R.id.date_text_r);
view = itemView;
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recycler_view_card,parent,false);
final ViewHolder holder = new ViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
newsXML.get(holder.getLayoutPosition()).setRead(true);
/*for(int i = 0; i < newsXML.size(); i++){
Log.d("News " + i + " ", " "+ newsXML.get(i).getRead() );
}*/
Intent intent = new Intent(view.getContext(),Activity_WebView.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("link", newsXML);
bundle.putInt("position",holder.getLayoutPosition());
intent.putExtra("bundle",bundle);
view.getContext().startActivity(intent);
}
});
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Store_News_XML current = newsXML.get(position);
holder.text_title.setText(current.getTitle());
holder.text_description.setText(current.getDescription());
holder.text_date.setText(current.getPubDate());
holder.currentLink = current.getLink();
// Log .d("News Read Outside " + position ,"" + current.getRead() + " / News Title : " + current.getTitle());
if(!newsXML.get(position).getRead()){
//Log .d("News Not Read " + position ,"" + current.getRead() + " / News Title : " + current.getTitle());
holder.text_title.setTextColor(Color.parseColor("#000000"));
holder.text_description.setTextColor(Color.parseColor("#000000"));
holder.text_date.setTextColor(Color.parseColor("#000000"));
}
else
{
//Log .d("News Read " + position ,"" + current.getRead() + " / News Title : " + current.getTitle());
holder.text_title.setTextColor(Color.parseColor("#BAC1C1"));
holder.text_description.setTextColor(Color.parseColor("#BAC1C1"));
holder.text_date.setTextColor(Color.parseColor("#BAC1C1"));
}
}
@Override
public int getItemCount() {
return newsXML.size();
}
}
我很抱歉凌乱且非结构化的代码,我对编程很新。感谢您提前获得所有帮助。