所以我有这个应用程序在其中必须作出RecyclerView
其中包含可以删除/编辑e.t.c的项目列表。在Android中以编程方式编辑RecyclerView项目
我在YouTube上跟随了一个教程,并在另一个布局和该项目的自定义适配器上制作了自定义CardView
项目。
事情是,根据项目的状态,我必须改变一些东西(例如背景颜色或文字颜色)。
当我在RecyclerView
活动中这样做时,即使我拥有该ID,也会获得NullPointerException
。
如何在我进行Retrofit调用的时候编辑这些项目中的TextViews
?
boolean isEnded;
if(!endedAt.equals("null"))
{
endedAt = endedAt.substring(endedAt.indexOf("T") + 1, endedAt.lastIndexOf(":"));
isEnded=true;
}
else
{
endedAt="ongoing";
isEnded=false;
}
item.timeDifference=createdAt+" - "+endedAt;
if(externalSystem.equals("null"))
{
item.externalSystem="";
}
else
{
item.externalSystem = externalSystem;
}
Log.i("attr",externalSystem);
items.add(item);
itemAdapter=new ItemAdapter(getApplicationContext(), items);
recyclerView.setAdapter(itemAdapter);
if(isEnded) {
error-> externalSystemView.setTextColor(Color.BLACK);
}
该应用程序相当大,但我认为你可以从这段代码中获得创意。
以下是错误:尝试上的空对象引用调用虚拟方法“无效android.widget.TextView.setTextColor(int)的”
public class ItemAdapter extends
RecyclerView.Adapter<ItemAdapter.ItemViewHolder>{
private Context context;
private ArrayList<Item> itemList;
public ItemAdapter(Context context, ArrayList<Item> itemList)
{
this.context=context;
this.itemList=itemList;
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
View view=layoutInflater.inflate(R.layout.item_layout,parent,false);
ItemViewHolder itemViewHolder=new ItemViewHolder(view);
return itemViewHolder;
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
Item item=itemList.get(position);
holder.timeDifference.setText(item.timeDifference);
holder.title.setText(item.title);
holder.timeCounter.setText(item.timeCounter);
holder.externalSystem.setText(item.externalSystem);
holder.type.setText(item.type);
holder.project.setText(item.project);
MY IDEA
"holder.timeDifference.setTextColor(Color.parseColor(item.color));"
}
@Override
public int getItemCount() {
if(itemList!=null)
return itemList.size();
else
return 0;
}
public static class ItemViewHolder extends RecyclerView.ViewHolder
{
public CardView cardViewItem;
public TextView title;
public TextView project;
public TextView externalSystem;
public TextView timeDifference;
public TextView timeCounter;
public TextView type;
public ItemViewHolder(View itemView)
{
super(itemView);
cardViewItem=(CardView)itemView.findViewById(R.id.card_view_item);
title=(TextView)itemView.findViewById(R.id.title);
project=(TextView)itemView.findViewById(R.id.project);
externalSystem=
(TextView)itemView.findViewById(R.id.external_system);
timeDifference=
(TextView)itemView.findViewById(R.id.time_difference);
timeCounter=(TextView)itemView.findViewById(R.id.time_counter);
type=(TextView)itemView.findViewById(R.id.type);
}
编辑:我想我发现了一种方法,但我不知道这是不是最好的
显示您的代码和logcat。 –
添加一些代码。你应该在'holder.itemView'中找到id的视图,而不是'RecyclerView'本身。 –
我不认为我可以检查持有者的文本更改,我可以在那里声明他们并将其绑定。 –