1

I am creating a RecyclerView for my android application with some dummy data which has two set of datas. But the layout is not iterating twice instead it shows the first data correctly.

public class HomeScreenFragment extends Fragment {

    private ArrayList<String> flags = new ArrayList<>();
    private ArrayList<String> leagues = new ArrayList<>();
    private ArrayList<String> dates = new ArrayList<>();

    private Context context;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        initData();

    }

    private void initData() {
        leagues.add("Indian premiere league");
        dates.add("03-Dec");

        leagues.add("Srilankan Premiere league");
        dates.add("04-Dec");

        initRecyclerView();
    }

    private void initRecyclerView() {
        RecyclerView recyclerView = getView().findViewById(R.id.homeRecyclerView);
        HomeScreenRVAdapter adapter = new HomeScreenRVAdapter(flags,leagues,dates,context);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
    }
}

RecyclerView

public class HomeScreenRVAdapter extends RecyclerView.Adapter<HomeScreenRVAdapter.ViewHolder> {
    private static final String TAG = "HomeScreenRVAdapter";

    private String BASE_URL, API_KEY;

    private ArrayList<String> flags = new ArrayList<>();
    private ArrayList<String> leagues = new ArrayList<>();
    private ArrayList<String> dates = new ArrayList<>();
    private Context context;

    public HomeScreenRVAdapter(ArrayList<String> flags, ArrayList<String> leagues, ArrayList<String> dates, Context context) {
        this.flags = flags;
        this.leagues = leagues;
        this.dates = dates;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_home, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        holder.league.setText(leagues.get(position));
        holder.date.setText(dates.get(position));
    }

    @Override
    public int getItemCount() {
        return leagues.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        // HEADER WIDGETS
        ExpansionHeader expansionHeader;
        ImageView flag;
        TextView league, date;

        // LAYOUT WIDGETS
        ExpansionLayout expansionLayout;
        TextView minutes, local, visitor, localScore, visitorScore;
        ImageView favourite;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            expansionHeader = itemView.findViewById(R.id.expansionHeader);
            flag = itemView.findViewById(R.id.headerFlag);
            league = itemView.findViewById(R.id.expansionHeaderLeague);
            date = itemView.findViewById(R.id.expansionHeaderDate);

        }
    }
}

I want to make the app iterate the layout (layout_home) as many times as data comes. But it is not iterating. Otherwise is there anyway to iterate only the expansion layout alone?

deluxan
  • 21
  • 6
  • your recyclerView and your adapter are not properly implemented. And you don't need to separate your flag, league, and date into 3 ArrayList. You can do these only into one ArrayList. – Mak-Mak Basaya Dec 14 '19 at 12:52
  • @Mak-MakBasaya Would you be able to expand on the implementation issues? If it's just the three different arrays, then I do not think that would cause such an error. – Karishnu Poddar Dec 14 '19 at 13:02
  • The problem here is the layout is not iterating – deluxan Dec 14 '19 at 13:03
  • @deluxan It would be helpful if you posted your layout file too (layout_home) – Karishnu Poddar Dec 14 '19 at 13:05
  • It is working after changing from `android:layout_height="match_parent"` to `android:layout_height="wrap_content"` in layout_home.xml – deluxan Dec 14 '19 at 13:07

2 Answers2

0

Your code looks fine. There is most probably an issue in layout_home. Make sure android:layout_height is set to wrap_content.

Moreover, you could implement a custom object ArrayList instead of creating three separate ones. The custom object would hold the properties of each item. However, this is a suggestion and I do not think this is the cause of the bug you are facing.

Karishnu Poddar
  • 313
  • 2
  • 11
0

The accepted answer is the correct answers but Another approach and good coding practices I made it for you.

First, we need to initialize the RecyclerView

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        RecyclerView recyclerView = getView().findViewById(R.id.homeRecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        HomeScreenRVAdapter adapter = new HomeScreenRVAdapter();
        recyclerView.setAdapter(adapter);

    }

Then create the recyclerView Adapter called HomeScreenRVAdapter.java

public class HomeScreenRVAdapter extends RecyclerView.Adapter<HomeScreenRVAdapter.ViewHolder> {

    private List<League> leagues = new ArrayList<>();

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        League league = leagues.get(position);
        holder.league.setText(league.getLeague());
        holder.date.setText(league.getDate());
    }

    @Override
    public int getItemCount() {
        return leagues.size();
    }

    void setList(List<League> list) {
        this.leagues = list;
        notifyDataSetChanged();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView flag;
        TextView league, date;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            flag = itemView.findViewById(R.id.headerFlag);
            league = itemView.findViewById(R.id.expansionHeaderLeague);
            date = itemView.findViewById(R.id.expansionHeaderDate);
        }
    }
}

create getter and setter class called League.java

class League {
    private String flag;
    private String league;
    private String date;

    public League(String flag, String league, String date) {
        this.flag = flag;
        this.league = league;
        this.date = date;
    }

    public String getFlag() {
        return flag;
    }

    public String getLeague() {
        return league;
    }

    public String getDate() {
        return date;
    }
}

then in your onViewCreated() add your data like this.

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        ...

        List<League> leagues = new ArrayList<>();
        leagues.add(new League("Flag1","Indian premiere league","03-Dec"));
        leagues.add(new League("Flag2","Srilankan Premiere league","04-Dec"));

        adapter.setList(leagues);
    }

notifyDataSetChanged();

is very important. don't forget to add after data changed.

  • Thank you for your suggestion. I'll try with it. Assume If I am getting a string from api and I stored it in a variable and I am doing the following coding but it showing empty. Anyhow the string is showing data in Log. `new Response.Listener() { @Override public void onResponse(JSONObject response) { try { JSONArray jsonArray = response.getJSONArray("data"); for(int i = 0; i < jsonArray.length(); i++) { JSONObject data = jsonArray.getJSONObject(i); Log.d("League ID", league_id); leagues.add(league_id); // This thing is not working}` – deluxan Dec 14 '19 at 14:13