RecyclerView不会显示数据

时间:2017-12-12 12:24:28

标签: android android-recyclerview

我使用RecyclerView以列表形式显示某些数据,但数据不会显示。

以下是我的代码,我用来实现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>

我的RecyclerView是空白的。

我在RecyclerView中显示了MainActivity中提到的数组。我尝试了很多,但无法弄清楚我做错了什么?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:4)

您在父布局中缺少orientation

<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>
相关问题