1

I am using RecyclerView to show some data in form of list, but the data won't display.

Following is my code, I am using to implement RecyclerView

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener{

    String[] c_names={"INDIA","US","UK","AUSTRALIA","CHINA","JAPAN"};
    int[] c_flags={R.drawable.india,R.drawable.india,R.drawable.india,R.drawable.india,R.drawable.india,R.drawable.india};
    private RecyclerView recyclerView;
    RecyclerAdapter recyclerAdapter;
    ArrayList<Country> arrayList=new ArrayList<>();
    private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        recyclerView=findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        recyclerView.setHasFixedSize(true);
        int count=0;
        for(String Name:c_names){

            arrayList.add(new Country(Name,c_flags[count]));
            Log.i("ggg", "onCreate: "+arrayList.get(count).getName()+" "+arrayList.get(count).getFlag_id());
            count++;
        }
        recyclerAdapter=new RecyclerAdapter(arrayList);
        recyclerView.setAdapter(recyclerAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        MenuItem menuItem=menu.findItem(R.id.ction_search);
        SearchView searchView= (SearchView) MenuItemCompat.getActionView(menuItem);
       searchView.setOnQueryTextListener(this);
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        newText=newText.toLowerCase();
        ArrayList<Country> newList=new ArrayList<>();
        for(Country country:arrayList){
            String name=country.getName().toLowerCase();
            if(name.contains(newText)){
                newList.add(country);
            }
        }
        recyclerAdapter.setFilter(newList);
        return true;
    }
}

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {

    ArrayList<Country> arrayList=new ArrayList<>();

    public RecyclerAdapter(ArrayList<Country> arrayList) {
        this.arrayList = arrayList;
    }

    @Override
    public RecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,
                parent,false);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.MyViewHolder holder, int position) {
        holder.c_flags.setImageResource(arrayList.get(position).getFlag_id());
        holder.c_name.setText(arrayList.get(position).getName());
    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder{

        ImageView c_flags;
        TextView c_name;
        public MyViewHolder(View itemView) {
            super(itemView);
            c_flags=itemView.findViewById(R.id.flag);
            c_name=itemView.findViewById(R.id.name);
        }

    }

    public void setFilter(ArrayList<Country> filter){
       // filter=new ArrayList<>();
        arrayList.addAll(filter);
        notifyDataSetChanged();
    }
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="pritish.sawant.com.filterrecyclerview.MainActivity">

    <include layout="@layout/toolbar_layout"/>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recyclerview"/>


</LinearLayout>

row_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="4dp"
    android:layout_marginBottom="8dp"
    android:layout_marginRight="4dp">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:elevation="5dp"
        app:cardCornerRadius="8dp"
        android:background="#9E9E9E"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="8dp"
            android:background="#9E9E9E">

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="8dp"
                android:scaleType="fitXY"
                android:id="@+id/flag"
                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/flag"
                android:layout_marginLeft="20dp"
                android:gravity="center"
                android:layout_centerVertical="true"
                android:textSize="15dp"
                android:textStyle="bold"
                android:id="@+id/name"
                android:textColor="#000000"
                />

        </RelativeLayout>



    </android.support.v7.widget.CardView>

</LinearLayout>

toolbar_layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="?attr/colorPrimary"
    android:id="@+id/toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.Dark">

</android.support.v7.widget.Toolbar>

My RecyclerView is blank.

I am showing the arrays mentioned in MainActivity in my RecyclerView. I tried a lot but couldn't figure out what am I doing wrong? Any help would be greatly appreciated.

11
  • Log getItemCount() see if its the size of list or 0 . Commented Dec 12, 2017 at 12:26
  • @ADM it is showing 6
    – user8412632
    Commented Dec 12, 2017 at 12:27
  • @abu share your xml layout
    – AskNilesh
    Commented Dec 12, 2017 at 12:28
  • Then there must be some issue with xml . Post your main layout and R.layout.row_layout. Commented Dec 12, 2017 at 12:28
  • @NiluI have edited the question . please have a look.
    – user8412632
    Commented Dec 12, 2017 at 12:31

1 Answer 1

4

You are missing orientation in parent layout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="pritish.sawant.com.filterrecyclerview.MainActivity">

<include layout="@layout/toolbar_layout"/>

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recyclerview"/>

</LinearLayout>
3
  • See if that workout for you . Otherwise i'll have look into your code. Commented Dec 12, 2017 at 12:37
  • Thanks alot. What a dumb mistake
    – user8412632
    Commented Dec 12, 2017 at 12:38
  • 1
    yeah that because of <include> tag . In this case IDE do not give XML compile error for orientation. Commented Dec 12, 2017 at 12:39