【问题标题】:show something when listview is empty当 listview 为空时显示一些东西
【发布时间】:2017-05-25 23:52:20
【问题描述】:

我有一个列表视图,当一个片段被保存的列表更新时,它会显示出来。最初该列表是空的,所以它吐出一个错误 java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0. 所以我想显示类似文本的东西,说类似空的东西,这样当列表中有东西时它会展示。我曾尝试使用 getEmptyView 但无济于事。可以请我帮忙看看如何完成这个谢谢。

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

    View customView = inflater.inflate(R.layout.fragment_reading_monday, container, false);

    addNewButton = (Button) customView.findViewById(R.id.btnAddNewReadingMonday);
    relativeLayoutMonday = (RelativeLayout) customView.findViewById(R.id.frameLayoutMonday);
    listViewMonday = (ListView)customView.findViewById(R.id.listViewMonday);
    listContainerMonday = (RelativeLayout)customView.findViewById(R.id.listContainerMonday);
    tinyDB = new TinyDB(getContext());
    addNewSubject = (Button)customView.findViewById(R.id.btnAddNewSubjectMonday);
    dataModels = new ArrayList<>();
    arrayListSubjectsRead = new ArrayList<>();
    timeOne = new ArrayList<>();
    timeTwo = new ArrayList<>();
    adapter = new CustomListAdapterReading(getContext(), dataModels);

        TextView empty = (TextView) customView.findViewById(R.id.emptyTextView);
        listViewMonday.setEmptyView(empty);


            arrayListSubjectsRead =  tinyDB.getListString("ArrayForSubjects");
            timeOne = tinyDB.getListString("ArrayForTimeOne");
            timeTwo = tinyDB.getListString("ArrayForTimeTwo");

            dataModels.add(new DataModelReading(arrayListSubjectsRead, timeOne, timeTwo));

            adapter = new CustomListAdapterReading(getContext(), dataModels);
            listViewMonday.setAdapter(adapter);
            adapter.notifyDataSetChanged();

            System.out.println(" subjects: "+arrayListSubjectsRead);
            System.out.println("timeone: "+ timeOne);
            System.out.println("timetwo: "+timeTwo);









    addNewButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            relativeLayoutMonday.removeView(noEventTextView);
            Intent intent = new Intent(getActivity(),ReadMain.class);
            startActivity(intent);

        }
    });

    addNewSubject.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });




    return customView;
}

【问题讨论】:

标签: java android listview


【解决方案1】:

我希望您查看此帖子setEmptyView on ListView not showing its view in a android app,它应该可以解决您的空视图问题。 如果您想防止应用抛出 IndexOutOfBoundsException,您只需在设置适配器之前检查 dataModels 数组的大小即可。

if (dataModels.size() > 0)
{
    //Setup our adapter here
}

希望这会有所帮助:)

【讨论】:

    【解决方案2】:

    这样的?

    ImageListAdapter imageListAdapter= new ImageListAdapter();
    listviewObject.setAdapter(imageListAdapter);
    
    if (imageListAdapter!= null)
        if (imageListAdapter.getCount() > 0){
            // implement your work
        }else {
            // do whatever you want on empty list adapter 
        }
    

    【讨论】:

      【解决方案3】:

      我在我的应用程序中使用这种方式

      ListView listView = (ListView)findViewById(android.R.id.listView);
      TextView noData = (TextView)findViewById(android.R.id.noData);
      listView.setEmptyView(noData);
      

      当适配器为空时,Listview 将自动设置无数据文本。

      【讨论】:

        猜你喜欢
        • 2014-10-01
        • 2011-05-09
        • 1970-01-01
        • 2011-05-07
        • 2011-12-09
        • 1970-01-01
        • 2021-06-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多