Java 当listview为空时显示某些内容
我有一个listview,当一个片段被保存的列表更新时会显示出来。最初,该列表为空,因此它会抛出一个错误java.lang.IndexOutOfBoundsException:索引0无效,大小为0。所以我想显示一些东西,比如一个文本,说一些空的东西,这样当列表中有东西时,它就会显示出来。我尝试过使用getEmptyView,但没有效果。我能得到一些关于如何完成这项任务的帮助吗?谢谢Java 当listview为空时显示某些内容,java,android,listview,Java,Android,Listview,我有一个listview,当一个片段被保存的列表更新时会显示出来。最初,该列表为空,因此它会抛出一个错误java.lang.IndexOutOfBoundsException:索引0无效,大小为0。所以我想显示一些东西,比如一个文本,说一些空的东西,这样当列表中有东西时,它就会显示出来。我尝试过使用getEmptyView,但没有效果。我能得到一些关于如何完成这项任务的帮助吗?谢谢 @Override public View onCreateView(LayoutInflater infla
@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;
}
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图customView=充气机。充气(R.layout.fragment\u reading\u monday,container,false);
addNewButton=(Button)customView.findViewById(R.id.btnaddNewRoadingMonday);
relativeLayoutMonday=(RelativeLayout)customView.findViewById(R.id.frameLayoutMonday);
listViewMonday=(ListView)customView.findViewById(R.id.listViewMonday);
listContainerMonday=(RelativeLayout)customView.findViewById(R.id.listContainerMonday);
tinyDB=新的tinyDB(getContext());
addNewSubject=(按钮)customView.findViewById(R.id.btnAddNewSubjectMonday);
dataModels=新的ArrayList();
arrayListSubjectsRead=新的ArrayList();
timeOne=新的ArrayList();
timeTwo=newarraylist();
adapter=new CustomListAdapterReading(getContext(),dataModels);
TextView empty=(TextView)customView.findViewById(R.id.emptyTextView);
listViewMonday.setEmptyView(空);
arrayListSubjectsRead=tinyDB.getListString(“ArrayForSubjects”);
timeOne=tinyDB.getListString(“ArrayForTimeOne”);
timeTwo=tinyDB.getListString(“ArrayForTimeTwo”);
添加(新的DataModelReading(arrayListSubjectsRead,timeOne,timeTwo));
adapter=new CustomListAdapterReading(getContext(),dataModels);
listViewMonday.setAdapter(适配器);
adapter.notifyDataSetChanged();
System.out.println(“主题:“+arrayListSubjectsRead”);
System.out.println(“timeone:+timeone”);
System.out.println(“timetwo:+timetwo”);
addNewButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
relativeLayoutMonday.removeView(NoEventExtView);
Intent Intent=new Intent(getActivity(),ReadMain.class);
星触觉(意向);
}
});
addNewSubject.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
}
});
返回自定义视图;
}
像这样的东西
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
}
像这样的
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
}
我想让你看看这篇文章,它应该可以解决你的空视图问题。 如果您想防止应用程序抛出IndexOutOfBoundsException,只需在设置适配器之前检查
dataModels
数组的大小即可
if (dataModels.size() > 0)
{
//Setup our adapter here
}
希望这有帮助:)我想让你看看这篇文章,它应该能解决你的空视图问题。 如果您想防止应用程序抛出IndexOutOfBoundsException,只需在设置适配器之前检查
dataModels
数组的大小即可
if (dataModels.size() > 0)
{
//Setup our adapter here
}
希望这有帮助:)我在应用程序中使用这种方式
ListView listView = (ListView)findViewById(android.R.id.listView);
TextView noData = (TextView)findViewById(android.R.id.noData);
listView.setEmptyView(noData);
当适配器为空时,Listview将自动设置没有数据文本。我在应用程序中使用这种方式
ListView listView = (ListView)findViewById(android.R.id.listView);
TextView noData = (TextView)findViewById(android.R.id.noData);
listView.setEmptyView(noData);
当适配器为空时,Listview将自动设置无数据文本