puk789
puk789

Reputation: 332

Android Adapter not being updated

I want to display a list of match objects (match = two users having liked each other) in a recycler view with the help of an adapter.

This is my activity which is meant to display those matches:

public class MatchesActivity extends AppCompatActivity {

// variables:
private RecyclerView mMatchesRecyclerView;
private RecyclerView.Adapter mMatchItemAdapter;
private RecyclerView.LayoutManager mMatchesLayoutManager;

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

    mMatchesRecyclerView = findViewById(R.id.matches_recyclerView);
    mMatchesRecyclerView.setNestedScrollingEnabled(false);
    mMatchesRecyclerView.setHasFixedSize(true);

    // set layout manager & pass it to the recycler view:
    mMatchesLayoutManager = new LinearLayoutManager(MatchesActivity.this);
    mMatchesRecyclerView.setLayoutManager(mMatchesLayoutManager);

    // set match adapter & pass it to the recycler view:
    mMatchItemAdapter = new MatchItemAdapter(getMatchesList(), MatchesActivity.this);
    mMatchesRecyclerView.setAdapter(mMatchItemAdapter);

    // add test items to the recycler view:
    Match testMatch = new Match("abcdefgh");
    matchesList.add(testMatch);
    mMatchItemAdapter.notifyDataSetChanged();
    Log.d("MatchesActivity", "TEST LIST: " + matchesList.toString());
}

private ArrayList<Match> matchesList = new ArrayList<Match>();
private List<Match> getMatchesList() {
    Log.d("MatchesActivity", "getMatchesList function: " + matchesList.toString());
    return matchesList;
}
}

And this is my adapter which is supposed to inflate the relevant layout & populate it with relevant object data:

public class MatchItemAdapter extends RecyclerView.Adapter<MatchViewholder> {

private List<Match> mMatchesList;
private Context mViewContext;

public MatchItemAdapter(List<Match> matchesList, Context context) {
    this.mMatchesList = matchesList;
    this.mViewContext = context;
    Log.d("MatchItemAdapter", "Constructor: " + mMatchesList.toString());
}

// inflate the layout:
@Override
public MatchViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_matches, null, false);
    RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutView.setLayoutParams(lp);
    MatchViewholder matchViewholder = new MatchViewholder(layoutView);
    Log.d("MatchItemAdapter", "onCreateViewHolder: " + mMatchesList.toString());
    return matchViewholder;
}

// populate each row within the layout:
@Override
public void onBindViewHolder(MatchViewholder holder, int position) {
    Log.d("MatchItemAdapter", "onBindViewHolder: " + mMatchesList.toString());
    holder.mMatchID.setText(mMatchesList.get(position).getMatchID());
}

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

}

The Match class currently only takes matchID parameter which is string. An object is created with a default image and this matchID string. At the moment, I have no real match objects from database ready, so I wanted to check that the recycler view along with adapter are working as expected before i move on to that later.

However, when I go to Matches Activity, it is empty, showing nothing at all. As you can see from the MatchesActivity onCreate method, I created a test Match object with matchID = "abcdefgh" and then added that to the matchesList. So I am expecting the "abcdefgh" text to be passed to the adapter and to be shown in the MatchesActivity.

My log statements indicate that the Match object has been created and added to the list successfully, however, getMatchesList() function returns an empty list which is then used in the Adapter constructor too, (I think this is) causing Activity not show anything.

I am relatively new to Android and Java development, especially recycler view and adapters, but from what I gathered it seems to be as if the

mMatchItemAdapter.notifyDataSetChanged();

is not working properly as everything seems to be fine up until that point. Any help would be appreciated!

Upvotes: 0

Views: 34

Answers (1)

denvercoder9
denvercoder9

Reputation: 3021

You're returning 0. What you should do instead is return the length of the mMatchesList list.

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

Upvotes: 1

Related Questions