【发布时间】:2018-07-06 21:52:38
【问题描述】:
我正在尝试更改要插入到 RecyclerView 中的 Textview 数组的属性。我希望负数为红色,正数为绿色。很简单。
声明:public TextView[] mDay= new TextView[100];
初始化:
for(int i=0;i<=99;i++){
mDay[i] = (TextView) v.findViewById(R.id.day_text);
}
在布局中显示:
Public void onBindViewHolder(MyViewHolder holder, final int position) {
//Hier werden die ganzen Daten eingebunden
holder.mCoin.setText(mDataset[position]);
holder.mPrice.setText(mDataset2[position]);
for(int i=0;i<=mDataset4.length-1;i++){
if(mDataset4[i].contains("-"))
{
Log.w("Negativ",mDataset4[i]);
holder.mDay[i].setTextColor(Color.RED);
holder.mDay[i].setText(mDataset4[i]);
}
else {
Log.w("Positiv",mDataset4[i]);
holder.mDay[i].setTextColor(Color.GREEN);
holder.mDay[i].setText(mDataset4[i]);
}
}
只有我的 Dataset 中的最后一条数据被插入到 RecyclerView as you can see here。
当我像其他人一样插入我的数据集时,一切都在正确的位置
holder.mDay[position].setText(mDataset2[position]);
谢谢!
编辑:
我的视图持有者:
public static class MyViewHolder extends RecyclerView.ViewHolder {
public CardView mCardView;
public TextView mCoin;
public TextView mPrice;
public TextView mDay;
public MyViewHolder(View v) {
super(v);
//Sachen aus dem Layout zuordnen
mCardView = (CardView) v.findViewById(R.id.card_view);
mCoin = (TextView) v.findViewById(R.id.coin_text);
mPrice = (TextView) v.findViewById(R.id.price_text);
mDay = (TextView) v.findViewById(R.id.day_text);
}
}
我的构造器:我有多个数据集,所以我可以将不同的数据加载到不同的 texviews 等等。
public MyAdapter(ArrayList <String> coin,ArrayList <String> cap,ArrayList <String> price,ArrayList <String> day) {
mDataset = coin.toArray(new String[coin.size()]);
mDataset2 = cap.toArray(new String[cap.size()]);
mDataset3 = price.toArray(new String[price.size()]);
mDataset4 = day.toArray(new String[day.size()]);
}
我的 onBindViewHolder 函数:我将数据加载到我想要的文本视图中,然后我尝试根据文本视图的颜色是负数还是正数来更改它
public void onBindViewHolder(MyViewHolder holder, final int position) {
//Hier werden die ganzen Daten eingebunden
holder.mCoin.setText(mDataset[position]);
holder.mPrice.setText(mDataset2[position]);
for(int i=0;i<=mDataset4.length-1;i++){
if(mDataset4[i].contains("-"))
{
Log.w("Negativ",mDataset4[i]);
holder.mDay.setTextColor(Color.RED);
holder.mDay.setText(mDataset4[i]);
}
else {
Log.w("Positiv",mDataset4[position]);
holder.mDay.setTextColor(Color.GREEN);
holder.mDay.setText(mDataset4[position]);
}
}
holder.mCardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String currentValue = mDataset[position];
Log.d("CardView", "CardView Clicked: " + currentValue);
}
});
}
【问题讨论】:
-
您需要发布更多代码,以便更轻松地为您提供准确的答案。
-
你的代码没有意义。你的
RecyclerView的一排有 100 个TextViews? -
不,我没有 100 个文本视图。我的 CardFragment 中只有 1 个。我尝试将其声明为数组,这样我就可以更改每个条目的颜色。检查图像,也许它们可以帮助您了解我想要实现的目标。
-
你为什么这样做:: "public MyAdapter(ArrayList
coin,ArrayList cap,ArrayList price,ArrayList day) {" ??所有这些 ArrayList对象的大小都相同吗?如果没有,那么这个 "mDataset[position]" ... "mDataset4[position]" 将不起作用!您应该使用公共 getter 和 setter(标准 Java 的东西!)填充RecyclerView所需的所有值创建一个类。 -
Yikes.So 我有 4 个不同的 ArrayList,我将它们交给构造函数,以便我可以使用这些数据。这似乎有效。一个 Arraylist 对应一个 mDataset。数据和所有内容都正确显示,但只是我无法更改每一行的颜色。
标签: android arrays android-recyclerview textview