2017-05-25 85 views
-1

我有一個列表視圖,當打開一個片段時,顯示一個列表視圖,它將被保存的列表更新。最初那個列表是空的,所以它吐出一個錯誤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; 
} 
+0

https://stackoverflow.com/questions/10017088/android-displaying-text-when-listview-is-empty – Pavan

回答

1

我想讓你檢查這個帖子setEmptyView on ListView not showing its view in a android app,它應該解決你的空視圖問題。 如果您想防止應用程序拋出IndexOutOfBoundsException,那麼在設置適配器之前,您可以簡單地檢查dataModels陣列的大小。

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

希望這有助於:)

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

我使用這種方式在我的應用程序時,適配器是空

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

列表視圖會自動設置沒有數據文本。