Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Alwin Lazar V
Alwin Lazar V
5,267 Points

How to give data to recyclerView using getters and setters in fragment. The data stored in a class as String Array

My aim is to build a recyclerView as GridLayoutManager with 5 columns in a fragment..

I call my BoardFragment in MainActivity's onCreateView

public class MainActivity extends AppCompatActivity {

public static final String BOARD_FRAGMENT = "board_fragment";
public static final String BOARD_NUMBER_INDEX = "board_number_index";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    NumberModel number = new NumberModel();
    number.setNumbers(Arrays.toString(BoardNumbers.numbers));

    BoardFragment boardFragment = new BoardFragment();

    Bundle bundle = new Bundle();
    bundle.putParcelable(BOARD_NUMBER_INDEX, savedInstanceState);
    boardFragment.setArguments(bundle);

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.mainContainer, boardFragment, BOARD_FRAGMENT);
    fragmentTransaction.commit();

}
}

and the BoardFragment is

public class BoardFragment extends Fragment { private NumberModel[] mNumberModels;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_board, container, false);


    Bundle bundle = this.getArguments();
    if (bundle != null) {
        mNumberModels = bundle.getParcelable(MainActivity.BOARD_FRAGMENT);
        Parcelable[] parcelables = bundle.getParcelableArray(MainActivity.BOARD_NUMBER_INDEX);
        mNumberModels = Arrays.copyOf(parcelables, parcelables.length, NumberModel[].class);
    }

    RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.boardRecyclerView);
    BoardGridAdapter adapter = new BoardGridAdapter(getActivity(), mNumberModels);
    recyclerView.setAdapter(adapter);

    RecyclerView.LayoutManager manager = new GridLayoutManager(getActivity(), 5);
    recyclerView.setLayoutManager(manager);

    recyclerView.setHasFixedSize(true);

    return view;
}
}

the BoardGridAdapter is

public class BoardGridAdapter extends RecyclerView.Adapter { private Context mContext; private NumberModel[] mNumberModels;

public BoardGridAdapter(Context context, NumberModel[] numberModels) {
    mContext = context;
    mNumberModels = numberModels;
}

@Override
public BoardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_board, parent, false);
    BoardViewHolder viewHolder = new BoardViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(BoardViewHolder holder, int position) {
    holder.bindView(mNumberModels[position]);

}

@Override
public int getItemCount() {
    return mNumberModels.length;
}

public class BoardViewHolder extends RecyclerView.ViewHolder {
    public TextView mTextView;

    public BoardViewHolder(View itemView) {
        super(itemView);
        mTextView = (TextView) itemView.findViewById(R.id.boardNumber);
    }

    public void bindView (NumberModel numberModel) {
        mTextView.setText(numberModel.getNumbers());

    }
}
}

I done the getters and setters here with parcelabe

public class NumberModel implements Parcelable { private String mNumbers;

public NumberModel() {}

public  String getNumbers() {
    return mNumbers;
}

public void setNumbers(String numbers) {
    mNumbers = numbers;
}


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(mNumbers);

}

private NumberModel (Parcel in) {
    mNumbers = in.readString();

}

public static final Creator<NumberModel> CREATOR = new Creator<NumberModel>() {
    @Override
    public NumberModel createFromParcel(Parcel parcel) {
        return new NumberModel(parcel);
    }

    @Override
    public NumberModel[] newArray(int i) {
        return new NumberModel[i];
    }
};
}

I stored my data in a class named BoardNumbers.class and it have a String array

public class BoardNumbers {
 public static String[] numbers = new String []{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"}; 
}

I don't understand why this code not working

Now it shows an error in BoardFragment.java

Error:(31, 49) error: incompatible types: inferred type does not conform to upper bound(s) inferred: INT#1 upper bound(s): NumberModel[],Parcelable where INT#1 is an intersection type: INT#1 extends NumberModel[],Parcelable

[MOD: edited code blocks]

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

You need to take a closer look at your types and what you are sending to the fragment in the Bundle. You're passing the Activity's Bundle along to the Fragment (possible because Bundle is Parcelable), but trying to extract it as an array of NumberModels (which causes the error). Then there is the whole trouble of wanting an array of NumberModels in the adapter but only creating one singular. Is the data supposed to represent a complete board of just a single space on the board? And why do you have a NumberModel class instead of Strings or Integers or even ints? Are you planning on adding custom logic to the class later?