Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView shows everything except first item

I have a RecyclerView where each item is a LinearLayout containing two EditTexts and an ImageButton. The RecyclerView is showing every item except the first one of my data set.

Can anyone tell me what's going on?

public class EditListAdapter extends RecyclerView.Adapter<EditListAdapter.ListViewHolder> {

private ArrayList mListCardset;

public static class ListViewHolder extends RecyclerView.ViewHolder{
    public EditText frontEditText;
    public EditText backEditText;
    public ImageButton editActionButton;

    public ListViewHolder(View view) {
        super(view);
        frontEditText = (EditText)view.findViewById(R.id.front_edit_text);
        backEditText = (EditText)view.findViewById(R.id.back_edit_text);
        editActionButton = (ImageButton) view.findViewById(R.id.edit_action_button);
    }
}

// Provide a suitable constructor (depends on the kind of dataset)
public EditListAdapter(ArrayList aList) {
    mListCardset = aList;
}

// Create new views (invoked by the layout manager)
@Override
public EditListAdapter.ListViewHolder onCreateViewHolder(ViewGroup parent,
                                                 int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_edit_item, parent, false);
    // set the view's size, margins, paddings and layout parameters

    ListViewHolder vh = new ListViewHolder((LinearLayout)v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ListViewHolder listHolder, int pos) {

    String[] eListItem = (String[]) mListCardset.get(pos); //get individual card @position
    listHolder.frontEditText.setText(eListItem[0]); 
    listHolder.backEditText.setText(eListItem[1]); 

}


private String[] strArrayListHelper(String frontString, String backString){
    String[] returnStrings = new String[2];
    returnStrings[0] = frontString;
    returnStrings[1] = backString;

    return returnStrings;
}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return mListCardset.size();
}
}

This will only show items 2 through 5:

 mRecyclerView = (RecyclerView)findViewById(R.id.list_edit_recycler_view);

    ArrayList testListCards = new ArrayList<String[]>();
    testListCards.add(strArrayListHelper("Test 1","Test 1"));
    testListCards.add(strArrayListHelper("Test 2","Test 2"));
    testListCards.add(strArrayListHelper("Test 3","Test 3"));
    testListCards.add(strArrayListHelper("Test 4","Test 4"));
    testListCards.add(strArrayListHelper("Test 5","Test 5"));

    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(EditActivity.this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // specify an adapter
    mAdapter = new EditListAdapter(testListCards);
    mRecyclerView.setAdapter(mAdapter);
like image 730
Sammy T Avatar asked Apr 26 '16 01:04

Sammy T


People also ask

What does onCreateViewHolder do?

This method calls onCreateViewHolder to create a new ViewHolder and initializes some private fields to be used by RecyclerView. Returns the position of the given ViewHolder in the given Adapter . Returns the total number of items in the data set held by the adapter. Return the stable ID for the item at position .

When onCreateViewHolder is Called?

onCreateViewHolder is called when you need a new View. by Pavlos-Petros Tournaris | Medium. This is not the way a recycling works.

How RecyclerView adapter works in android?

The adapter is a class. It takes the data set that needs to be shown to the user in RecyclerView. It acts as the main responsible class for binding the views and displaying them. The majority of the work is done within the RecyclerView's adapter class.


1 Answers

Maybe your Toolbar hides your first item of your RecyclerView.

Try to use this attribute

android:layout_marginTop="?attr/actionBarSize"

in your RecyclerView xml.

Hope that helps!

like image 72
Naveen T P Avatar answered Oct 09 '22 22:10

Naveen T P