当listview为空时显示一些东西

时间:2017-05-25 15:33:33

标签: java android listview

我有一个列表视图,显示打开的片段何时被保存的列表更新。最初该列表是空的,因此它吐出错误java.lang.IndexOutOfBoundsException:索引0无效,大小为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;
}

3 个答案:

答案 0 :(得分:1)

这样的东西?

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 
    }

答案 1 :(得分:1)

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

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

希望这会有所帮助:)

答案 2 :(得分:1)

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

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

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