2016-04-20 70 views
1

我有一個列表視圖,其中的項目從服務器加載和有一個頁腳在列表視圖,問題是,當沒有項目,即列表視圖是空的頁腳是用戶可見的。我想要什麼如果沒有項目在listview那麼footer對用戶是不可見的。我該怎麼做纔好心幫助我。如何在Android中列表視圖爲空時隱藏頁腳?

我將非常感激。

這裏是我的代碼: -

protected void onPostExecute(final String result) { 

     m_ProgressView.setVisibility(View.GONE); 
     try { 
      m_oResponseobject = new JSONObject(result);// getting response from server 
      JSONArray posts = m_oResponseobject.optJSONArray("dealList"); 


      for (int i = 0; i < posts.length(); i++) { 
       JSONObject post = posts.getJSONObject(i); 
       item = new CDealAppDatastorage(); 
       item.setM_szHeaderText(post.getString("dealname")); 
       item.setM_szsubHeaderText(post.getString("dealcode")); 
       item.setM_n_Image(m_n_FormImage[i]); 
       s_oDataset.add(item); 

      } 

      // LoadMore button 
      Button btnLoadMore = new Button(getActivity()); 
      btnLoadMore.setText("LOAD MORE DEALS"); 
      btnLoadMore.setBackgroundResource(R.drawable.button_boarder); 
      btnLoadMore.setTextAppearance(getActivity(), android.R.style.TextAppearance_DeviceDefault_Small); 
      btnLoadMore.setTextColor(Color.WHITE); 
      btnLoadMore.setGravity(Gravity.CENTER); 



      // Adding Load More button to lisview at bottom 
      m_ListView.addFooterView(btnLoadMore); 
      m_oAdapter = new CDealAppListingAdapter(getActivity(), s_oDataset);// create adapter object and add arraylist to adapter 
      m_ListView.setAdapter(m_oAdapter);//adding adapter to recyclerview 
      m_oAdapter.notifyDataSetChanged(); 
      } 
+0

[隱藏的可能的複製在ListView的頁腳](http://stackoverflow.com/questions/7576099/hiding-footer-in-listview) – Sufian

回答

1

隱藏你的頁腳是這樣的:

// Firs check the list size is greater than 0: 

if(s_oDataset.size > 0) 
{ 
// Add your footer here 

}else 
{ 
// Dont not add your footer or Hide your footer 
} 
0

你可以得到你的ListView

listview.getCount(); 

隨後的大小,如果它等於爲零,然後改變你的按鈕的可見性

btnLoadMore.setVisibility(View.GONE); 
+0

感謝老闆它的工作 – niraj

+0

很高興幫助你,你可以標記爲正確的:D –

0

請參見下面的代碼,可以通過電子郵件,你可以隱藏ListView頁腳時ListView是空

if(!s_oDataset.isEmpty()){ 
     m_ListView.addFooterView(btnLoadMore); 
     m_oAdapter = new CDealAppListingAdapter(getActivity(), s_oDataset);// create adapter object and add arraylist to adapter 
     m_ListView.setAdapter(m_oAdapter);//adding adapter to recyclerview 
     m_oAdapter.notifyDataSetChanged(); 
    } 
0

下面是代碼,希望這樣將工作

protected void onPostExecute(final String result) { 

    m_ProgressView.setVisibility(View.GONE); 
    try { 
     m_oResponseobject = new JSONObject(result);// getting response from server 
     JSONArray posts = m_oResponseobject.optJSONArray("dealList"); 


     for (int i = 0; i < posts.length(); i++) { 
      JSONObject post = posts.getJSONObject(i); 
      item = new CDealAppDatastorage(); 
      item.setM_szHeaderText(post.getString("dealname")); 
      item.setM_szsubHeaderText(post.getString("dealcode")); 
      item.setM_n_Image(m_n_FormImage[i]); 
      s_oDataset.add(item); 

     } 

     // LoadMore button 
     Button btnLoadMore = new Button(getActivity()); 
     btnLoadMore.setText("LOAD MORE DEALS"); 
     btnLoadMore.setBackgroundResource(R.drawable.button_boarder); 
     btnLoadMore.setTextAppearance(getActivity(), android.R.style.TextAppearance_DeviceDefault_Small); 
     btnLoadMore.setTextColor(Color.WHITE); 
     btnLoadMore.setGravity(Gravity.CENTER); 



     // Adding Load More button to lisview at bottom 
     if(listview.getCount()>0){ 
     m_ListView.addFooterView(btnLoadMore); 
     } 


     m_oAdapter = new CDealAppListingAdapter(getActivity(), s_oDataset);// create adapter object and add arraylist to adapter 
     m_ListView.setAdapter(m_oAdapter);//adding adapter to recyclerview 
     m_oAdapter.notifyDataSetChanged(); 
     } 
0
protected void onPostExecute(final String result) { 

    m_ProgressView.setVisibility(View.GONE); 
    try { 
     m_oResponseobject = new JSONObject(result);// getting response from server 
     JSONArray posts = m_oResponseobject.optJSONArray("dealList"); 


     for (int i = 0; i < posts.length(); i++) { 
      JSONObject post = posts.getJSONObject(i); 
      item = new CDealAppDatastorage(); 
      item.setM_szHeaderText(post.getString("dealname")); 
      item.setM_szsubHeaderText(post.getString("dealcode")); 
      item.setM_n_Image(m_n_FormImage[i]); 
      s_oDataset.add(item); 

     } 

     // LoadMore button 
     Button btnLoadMore = new Button(getActivity()); 
     btnLoadMore.setText("LOAD MORE DEALS"); 
     btnLoadMore.setBackgroundResource(R.drawable.button_boarder); 
     btnLoadMore.setTextAppearance(getActivity(), android.R.style.TextAppearance_DeviceDefault_Small); 
     btnLoadMore.setTextColor(Color.WHITE); 
     btnLoadMore.setGravity(Gravity.CENTER); 



     // Adding Load More button to lisview at bottom 

if(!s_oDataset.size()==0) 
{ 
m_ListView.addFooterView(btnLoadMore); 
} 

     m_oAdapter = new CDealAppListingAdapter(getActivity(),  s_oDataset);// create adapter object and add arraylist to adapter 
     m_ListView.setAdapter(m_oAdapter);//adding adapter to recyclerview 
     m_oAdapter.notifyDataSetChanged(); 
     } 
相關問題