2017-12-12 37 views
1

RecyclerViewを使用してデータをリスト形式で表示していますが、データは表示されません。RecyclerViewはデータを表示しません

私は

toolbar_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" 
    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> 

activity_main 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(); 
    } 
} 

を実装するために使用しています、私のコードが続きます210

私のRecyclerViewは空白です。

私は、RecyclerViewのMainActivityに記載されている配列を表示しています。私はたくさん試しましたが、何が間違っているのか分かりませんでしたか?どんな助けでも大歓迎です。

+0

getItemCount()は、そのサイズがリストか0かを確認します。 – ADM

+0

@ADMそれは表示しています6 – Pritish

+0

@abuあなたのXMLレイアウトを共有 –

答えて

4

親のレイアウトに方向性がないと思います。

<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> 
+0

あなたに適したトレーニングがあるかどうかを確認してください。さもなければ私はあなたのコードを調べます。 – ADM

+0

ありがとう。どのようなダムミス – Pritish

+1

ええ、タグのため。この場合、IDEは方向性のためにXMLコンパイルエラーを出しません。 – ADM

関連する問題