2016-09-05 4 views
0

ArrayListに10個の要素を追加し、リサイクルビューを使用してリストを表示したいのですが、アダプターを使用してアンドロイドでリストを表示すると、最初の項目のみが表示されます

Listクラス

public class MusicListName extends Fragment { 


    LinearLayoutManager ll; 
    //int[] imagesid=null; 
    List<String> musicnames=new ArrayList<String>(); 

    public void addData() { 

     for(int i=0;i<10 ;i++){ 
      musicnames.add(i,"Hello "+i); 
      //imagesid[i]=R.drawable.icon; 


     } 
     Log.d("Printing",String.valueOf(musicnames)); 

    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     ll=new LinearLayoutManager(getActivity()); 


     RecyclerView recyclerView=(RecyclerView)inflater.inflate(R.layout.fragment_music_list_name,container,false); 

//  final String path="/sdcard/Music/"; 
//  int i=0; 
//  File f=new File(path); 
//  File list[]=f.listFiles(); 
//  for(File ff:list) 
//  { 
//   if(!ff.isDirectory()) { 
//    musicnames.add(ff.getName()); 
//    // imagesid[i]=R.drawable.icon; 
//    i++; 
//   } 
// 
//  } 

     addData(); 

     //View v = inflater.inflate(R.layout.fragment_music_list_name, container, false); 
     MusicAdapter musicAdapter=new MusicAdapter(musicnames); 
     //RecyclerView recyclerView = (RecyclerView)v.findViewById(R.id.music_list_recycler); 
     recyclerView.setLayoutManager(ll); 
     ll.generateDefaultLayoutParams(); 
     recyclerView.setAdapter(musicAdapter); 


     return recyclerView; 

    } 

} 

アダプタクラス

public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.ViewHolder> { 


    private List<String> mnames; 
    // private int[] imgIds; 


    public MusicAdapter(List<String> mnames){ 
     this.mnames=mnames; 
     //this.imgIds=imgIds; 
    } 


    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,false); 
     return new ViewHolder(v); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     String itemName=mnames.get(position); 
     //int imgpos=imgIds[position]; 
     holder.tv.setText(itemName); 
     //holder.iv.setImageDrawable(imgpos); 


    } 



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

    public static class ViewHolder extends RecyclerView.ViewHolder{ 
     private final TextView tv; 
     private final ImageView iv; 


     public ViewHolder(View itemView) { 
      super(itemView); 
      tv=(TextView)itemView.findViewById(R.id.mntv); 
      iv=(ImageView)itemView.findViewById(R.id.aaiv); 

     } 
    } 
} 

リサイクルのXML

<?xml version="1.0" encoding="utf-8"?> 

    <android.support.v7.widget.RecyclerView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     tools:context="com.example.hp.musisha.MainActivity" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:id="@+id/music_list_recycler" 
     android:scrollbars="vertical" 

     > 


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

出力 Image of output in emulator

logcat:

09-04 10:41:01.140 2587-2593/com.example.hp.musisha W/art: Suspending all threads took: 9.697ms 
09-04 10:41:01.224 2587-2587/com.example.hp.musisha D/Printing: [Hello 0, Hello 1, Hello 2, Hello 3, Hello 4, Hello 5, Hello 6, Hello 7, Hello 8, Hello 9] 
09-04 10:41:01.250 2587-2927/com.example.hp.musisha D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 

    [ 09-04 10:41:01.269 2587: 2587 D/   ] 
    HostConnection::get() New Host Connection established 0xaff0a380, tid 2587 

    [ 09-04 10:41:01.374 2587: 2927 D/   ] 
    HostConnection::get() New Host Connection established 0xaff0a6b0, tid 2927 
09-04 10:41:01.385 2587-2927/com.example.hp.musisha I/OpenGLRenderer: Initialized EGL, version 1.4 
09-04 10:41:01.661 2587-2593/com.example.hp.musisha W/art: Suspending all threads took: 40.377ms 
09-04 10:41:02.390 2587-2587/com.example.hp.musisha I/Choreographer: Skipped 38 frames! The application may be doing too much work on its main thread. 
09-04 10:44:23.263 2587-2593/com.example.hp.musisha W/art: Suspending all threads took: 10.201ms 
09-04 10:46:07.519 2587-2593/com.example.hp.musisha W/art: Suspending all threads took: 5.111ms 
+0

開始します。最初に 'll.generateDefaultLayoutParams();'を削除して、何もしないで、 'mnames.size()' iside 'getItemCount()'を返す前にLogを追加して、正しい番号を取得するようにしてください。 – yshahak

+0

私はあなたのアイテムは、インフレータブルラインを見るに問題があると思う。 'View v = LayoutInflater.from(parent.getContext())。inflate(R.layout.row、null);'を試してください。 –

+0

Suhyeon Leeyoueさんありがとうございました –

答えて

0

がにLinearLayoutManagerを変更してみてください。

ll = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false); 

そして "wrap_content"

0

変更

RecyclerView recyclerView=(RecyclerView)inflater.inflate(R.layout.fragment_music_list_name,container,false); 
に高さを変更、row.xmlに忘れてはいけません

~

RecyclerView recyclerView=(RecyclerView)inflater.inflate(R.layout.fragment_music_list_name,null,false); 

また、なぜgenerateDefaultLayoutParams()を使用しましたか?それの必要はありません。

関連する問題