2017-01-12 95 views
0

所以我有这个应用程序在其中必须作出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); 
    } 

编辑:我想我发现了一种方法,但我不知道这是不是最好的

+1

显示您的代码和logcat。 –

+0

添加一些代码。你应该在'holder.itemView'中找到id的视图,而不是'RecyclerView'本身。 –

+0

我不认为我可以检查持有者的文本更改,我可以在那里声明他们并将其绑定。 –

回答

1

该解决方案将涉及改变你的Item类有点。

你的问题是,你没有正确地将boolean触发器从Activity正确传递到你的RecyclerView.Adapter。例如。 isEndedBoolean的价值,以了解该物品的状态。你有正确的想法在使用所有三个类。

我会建议做的是在你的Item类中创建一个构造函数,将你的Activity中的值传递给你的适配器。我觉得使用getterssetters更容易,而不是直接从代码中分配变量。

因此,让我们开始,

boolean isEnded; 
    if(!endedAt.equals("null")) { 
    endedAt = endedAt.substring(endedAt.indexOf("T") + 1, endedAt.lastIndexOf(":")); 
    isEnded=true; 
    } else { 
    endedAt="ongoing"; 
    isEnded=false; 
    } 
    String timeDifference = createdAt+" - "+endedAt; 
    if(externalSystem.equals("null")) { 
    externalSystem=""; 
    } else { 
    externalSystem = externalSystem; 
    } 
    Log.i("attr",externalSystem); 
    items.add(new ItemModel(isEnded, timeDifference, externalSystem); 

    itemAdapter=new ItemAdapter(this, items); 
    recyclerView.setAdapter(itemAdapter); 
    itemAdapter.notifyDataSetChanged(); 

你会发现我是如何增加一个新的ItemModel为的RecyclerView内部变量数组的每一行,然后传递数组到Adapter。这样可以更容易地知道将什么变量传递给模型,并因此知道Adapter内部位置的相应行。

ItemModel类的例子看起来是这样的:

public class ItemModel { 
    // Getter and Setter model for recycler view items 
    private boolean isEnded; 
    private String timeDifference; 
    private String externalSystem; 
    //other variables, title etc etc 

    public ItemModel(boolean isEnded, String timeDifference, String externalSystem) { 

     this.isEnded = isEnded; 
     this.timeDifference = timeDifference; 
     this.externalSystem = externalSystem; 
     //only pass to the model if you can access it from code above otherwise to assign the variables statically like you have. 
    } 

    public boolean getIsEnded() {return isEnded;} 

    public String getTimeDifference() {return timeDifference;} 

    public String getExternalSystem() { return externalSystem; } 

} 

以上信息只是作为参考,为您打造更高效的模型框架来传递数据,而不是使用静态变量。

现在要解决您的问题,您需要检查if (item.getIsEnded()),然后更改与该if条件对应的文本颜色。

RecyclerView.AdapteronBindViewHolder将如下所示:

@Override 
public void onBindViewHolder(ItemViewHolder holder, int position) { 
    ItemModel item =itemList.get(position); 
    holder.timeDifference.setText(item.getTimeDifference()); 
    holder.title.setText(item.title); 
    holder.timeCounter.setText(item.timeCounter); 
    holder.externalSystem.setText(item.getExternalSystem()); 
    holder.type.setText(item.type); 
    holder.project.setText(item.project); 
    if (item.getIsEnded() { 
    holder.timeDifference.setTextColor(item.color); 
    } else { 
    holder.timeDifference.setTextColor(item.color); 
    } 
} 

Adapter的目的是充气的布局,组件绑定到该布局,并在专用位置对应于所述布局中的项执行的功能。您需要知道列表中的哪个项目处于哪个状态,您将无法单独通过您的Activity来完成。请注意,Adapter将您的Activity的代码与RecyclerView的实际活动区分开来有多么有用。

+1

对不起,我周末出去了,但处理我的问题的好方法,非常感谢! –

+0

乐意帮忙。祝你好运。 –

相关问题