【问题标题】:display of a list in android using adapter shows only first item使用适配器在android中显示列表仅显示第一项
【发布时间】:2016-09-05 03:01:51
【问题描述】:

我正在向 ArrayList 添加 10 个元素,并希望使用回收器视图显示列表,但是,当使用适配器显示它时,它只显示第一个元素。

列表类

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

日志猫:

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

【问题讨论】:

  • 作为开始。首先删除ll.generateDefaultLayoutParams();,它什么都不做,然后添加Log,在你返回mnames.size()之前,在getItemCount()旁边,为了看到你在那里得到正确的数字。
  • 我认为您的商品视图充气线有问题。试试View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.row, null);
  • 谢谢 Suhyeon Leeyoue 解决方案有效

标签: android arraylist android-recyclerview


【解决方案1】:

尝试将 LinearLayoutManager 更改为:

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

并且不要忘记在row.xml中,将高度改为“wrap_content”

【讨论】:

    【解决方案2】:

    改变

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

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

    还有,你为什么用generateDefaultLayoutParams()。没必要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-19
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 2016-12-27
      • 1970-01-01
      相关资源
      最近更新 更多