在RecyclerView适配器内部Split函数无正常工作。

huangapple go评论136阅读模式
英文:

Split function not working properly inside Recyclerview Adapter

问题

@Override
public void onBindViewHolder(@NonNull Adapter.ViewHolder holder, int position) {

    Colours = products.getColour();
    String[] values = Colours.split(",");

    switch (values.length) {

        case 1 : {
            Colour1 = values[0];
            break;
        }

        case 2 : {
            Colour1 = values[0];
            Colour2 = values[1];
            break;

        }

        case 3 : {
            Colour1 = values[0];
            Colour2 = values[1];
            Colour3 = values[2];
            break;
        }

        case 4 : {
            Colour1 = values[0];
            Colour2 = values[1];
            Colour3 = values[2];
            Colour4 = values[3];
            break;

        }

        case 5 :{
            Colour1 = values[0];
            Colour2 = values[1];
            Colour3 = values[2];
            Colour4 = values[3];
            Colour5 = values[4];
            break;

        }

        case 6:{
            Colour1 = values[0];
            Colour2 = values[1];
            Colour3 = values[2];
            Colour4 = values[3];
            Colour5 = values[4];
            Colour6 = values[5];
            break;

        }

    }
    Log.d(TAG, "Colours are: " + Colour1 + Colour2 + Colour3 + Colour4 + Colour5 + Colour6);

}

Actual values inside the "Colour" String are:

"-7941431,-9522231";
"-16183539";
"-13680981,-1813697,-5554626,-5144272,-47523";
"-11582057";
"-2975686";

Values obtained after splitting:

D/MyTag: Colours are: -7941431-9522231
D/MyTag: Colours are: -16183539-9522231
D/MyTag: Colours are: -13680981-1813697-5554626-5144272-47523
D/MyTag: Colours are: -11582057-1813697-5554626-5144272-47523
D/MyTag: Colours are: -2975686-1813697-5554626-5144272-47523
英文:

I am using a split function inside a RecyclerView Adapter to split a String separated by comma although the code works fine inside the activity. But inside the recycler view Adapter, it is giving different answers I am getting data from firebase Firestore.

My Code

 @Override
public void onBindViewHolder(@NonNull Adapter.ViewHolder holder, int position) {
Colours =products.getColour();
String[] values = Colours.split(",");
switch (values.length) {
case 1 : {
Colour1 = values[0];
break;
}
case 2 : {
Colour1=values[0];
Colour2=values[1];
break;
}
case 3 : {
Colour1=values[0];
Colour2=values[1];
Colour3=values[2];
break;
}
case 4 : {
Colour1=values[0];
Colour2=values[1];
Colour3=values[2];
Colour4=values[3];
break;
}
case 5 :{
Colour1=values[0];
Colour2=values[1];
Colour3=values[2];
Colour4=values[3];
Colour5=values[4];
break;
}
case 6:{
Colour1=values[0];
Colour2=values[1];
Colour3=values[2];
Colour4=values[3];
Colour5=values[4];
Colour6=values[5];
break;
}
}
Log.d(TAG,"Colours are : "+Colour1+Colour2+Colour3+Colour4+Colour5+Colour6);
}

Actual values inside Colour String are

"-7941431,-9522231"

"-16183539"

"-13680981,-1813697,-5554626,-5144272,-47523"

"-11582057"

"-2975686"

Values I get after splitting

D/MyTag: Colours are : -7941431-9522231
D/MyTag: Colours are : -16183539-9522231
D/MyTag: Colours are : -13680981-1813697-5554626-5144272-47523
D/MyTag: Colours are : -11582057-1813697-5554626-5144272-47523
D/MyTag: Colours are : -2975686-1813697-5554626-5144272-47523

答案1

得分: 2

看起来你没有重置其他变量,所以它显示的是旧值,所以试试这样做:

@Override
public void onBindViewHolder(@NonNull Adapter.ViewHolder holder, int position) {

    Colours = products.getColour();
    Colour1 = "";
    Colour2 = "";
    Colour3 = "";
    Colour4 = "";
    Colour5 = "";
    Colour6 = "";
    String[] values = Colours.split(",");

    switch (values.length) {

        case 1 : {
            Colour1 = values[0];
            break;
        }

        case 2 : {
            Colour1 = values[0];
            Colour2 = values[1];
            break;

        }

        case 3 : {
            Colour1 = values[0];
            Colour2 = values[1];
            Colour3 = values[2];
            break;
        }

        case 4 : {
            Colour1 = values[0];
            Colour2 = values[1];
            Colour3 = values[2];
            Colour4 = values[3];
            break;

        }

        case 5 :{
            Colour1 = values[0];
            Colour2 = values[1];
            Colour3 = values[2];
            Colour4 = values[3];
            Colour5 = values[4];
            break;

        }

        case 6:{
            Colour1 = values[0];
            Colour2 = values[1];
            Colour3 = values[2];
            Colour4 = values[3];
            Colour5 = values[4];
            Colour6 = values[5];
            break;

        }

    }
    Log.d(TAG, "Colours are : " + Colour1 + Colour2 + Colour3 + Colour4 + Colour5 + Colour6);

}

如果你有任何问题,请随时问。

英文:

Looks like you are not resetting other variables so it is showing old values, so try this:

    @Override
public void onBindViewHolder(@NonNull Adapter.ViewHolder holder, int position) {
Colours =products.getColour();
Colour1 = "";
Colour2 = "";
Colour3 = "";
Colour4 = "";
Colour5 = "";
Colour6 = "";
String[] values = Colours.split(",");
switch (values.length) {
case 1 : {
Colour1 = values[0];
break;
}
case 2 : {
Colour1=values[0];
Colour2=values[1];
break;
}
case 3 : {
Colour1=values[0];
Colour2=values[1];
Colour3=values[2];
break;
}
case 4 : {
Colour1=values[0];
Colour2=values[1];
Colour3=values[2];
Colour4=values[3];
break;
}
case 5 :{
Colour1=values[0];
Colour2=values[1];
Colour3=values[2];
Colour4=values[3];
Colour5=values[4];
break;
}
case 6:{
Colour1=values[0];
Colour2=values[1];
Colour3=values[2];
Colour4=values[3];
Colour5=values[4];
Colour6=values[5];
break;
}
}
Log.d(TAG,"Colours are : "+Colour1+Colour2+Colour3+Colour4+Colour5+Colour6);
}

huangapple
  • 本文由 发表于 2020年8月18日 01:00:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63455392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定