-1

I am working currently on Android app and cannot figure out why recycler view is displaying sth like this: Item 0 Item 1 ... Item 9

My Main Activity:

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

private RecyclerView recView;
private DerpAdapter adapter;
private List<ListItem>  itemList = new ArrayList<>();


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

    MakeList();

    recView = (RecyclerView)findViewById(R.id.rec_list);
    recView.setLayoutManager(new LinearLayoutManager(this));
    recView.setHasFixedSize(true);

    adapter = new DerpAdapter(itemList, this);
    recView.setAdapter(adapter);


}

private void MakeList()
{
    ListItem item = new ListItem("1", "2");
    itemList.add(item);

    item = new ListItem("2", "3");
    itemList.add(item);

    item = new ListItem("3", "2");
    itemList.add(item);

    item = new ListItem("4", "1");
    itemList.add(item);

    //adapter.notifyDataSetChanged();
} }

My Adapter:

public class DerpAdapter extends RecyclerView.Adapter<DerpAdapter.DerpHolder> {

private List<ListItem> listData;
private LayoutInflater inflater;

public DerpAdapter(List<ListItem> listData, Context c){
    inflater = LayoutInflater.from(c);
    this.listData = listData;
}

@Override
public DerpHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.list_item, parent, false);
    return new DerpHolder(view);
}

@Override
public void onBindViewHolder(DerpHolder holder, int position) {
    ListItem item = listData.get(position);
    holder.counter.setText(item.getCount());
    holder.circle.setText(item.getNumber());
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

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

public class DerpHolder extends RecyclerView.ViewHolder {

    private TextView counter;
    private TextView circle;
   // private View container;

    public DerpHolder(View itemView) {
        super(itemView);

        counter = (TextView) itemView.findViewById(R.id.counter);
        circle = (TextView) itemView.findViewById(R.id.circle);
        //container = itemView.findViewById(R.id.cont_item_root);
    }
} }

XML file for list item

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cont_item_root">

<TextView
    android:id="@+id/counter"
    android:layout_width="wrap_content"
    android:layout_height="36dp"
    android:gravity="center"
    android:paddingLeft="8dp"
    android:text="1" />

<TextView
    android:id="@+id/circle"
    android:layout_width="36dp"
    android:layout_height="36dp"
    android:tint="@color/colorPrimary"
    android:background = "@drawable/rounded_button"
    android:text="1"/> </LinearLayout>

XML File for Main Activity

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ui.MainActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start"
    android:id="@+id/start"
    android:background = "@drawable/rounded_button"
    android:layout_alignTop="@+id/stop"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="49dp"
    android:layout_marginStart="49dp"
    android:layout_marginBottom="30dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Stop"
    android:id="@+id/stop"
    android:background = "@drawable/rounded_button"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/start"
    android:layout_toEndOf="@+id/start"
    android:layout_marginLeft="56dp"
    android:layout_marginStart="56dp" />

<android.support.v7.widget.RecyclerView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/rec_list"
    android:layout_below="@id/start"
    >

</android.support.v7.widget.RecyclerView> </RelativeLayout>
borowka
  • 1
  • 1

2 Answers2

0

what you need? in this case recyclerview have only 4 items (item0 to item 3)but you display like one textview below second textview that type display like item 0 to item9 change in layout file and text alignment if any problem then just comment

  • Ok, so when I change number of items in the list, nothing changes in the view. It's seems for me that recycler view doesn't see content of my list or my list item class at all – borowka May 26 '17 at 10:17
0

You need to call notifyDataSetChanged on your RecyclerView.Adapter after you updated your list. This method tells the adapter to update the view content with the new data structure.

Maxime Flament
  • 563
  • 5
  • 19
  • Unfortunately it doesn't work. adapter = new DerpAdapter(itemList, this); MakeList(); adapter.notifyDataSetChanged(); recView.setAdapter(adapter); I also tried with any other order but without success – borowka May 26 '17 at 11:38
  • Tha'ts normal. You need to call it on the **existing** adapter. Not a new one. You should look at the Android documentation. – Maxime Flament May 26 '17 at 11:51
  • Take a look a this post: https://stackoverflow.com/questions/31367599/how-to-update-recyclerview-adapter-data – Maxime Flament May 26 '17 at 11:57
  • I also tried to pass already filled list, but this also isn't working – borowka May 26 '17 at 11:57
  • I made it like this recView.getAdapter().notifyDataSetChanged(); but still nothing – borowka May 26 '17 at 12:05