英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论