2015-06-08 24 views
2

我有一個問題,設置了「無結果」消息時getCount將()返回0獲取「無結果」消息定義適配器的ListView的Android

我爲ListView自定義佈局,我只是想在列表視圖中沒有數據時獲取「無結果」文本視圖。

任何想法如何實現這一目標?

我看到了一個空ID的東西,但需要在一個列表視圖,因爲這是一個自定義佈局,它沒有那個「標籤」。

定製適配器代碼:

public class ListScheduleAdapter extends BaseAdapter { 

     Context context; 

     protected List<Schedule> listSchedules; 
     LayoutInflater inflater; 

     public ListScheduleAdapter(Context context, List<Schedule> listSchedules) { 
      this.listSchedules = listSchedules; 
      this.inflater = LayoutInflater.from(context); 
      this.context = context; 
     } 

     public int getCount() { 
      return listSchedules.size(); 
     } 

     public Schedule getItem(int position) { 
      return listSchedules.get(position); 
     } 

     public long getItemId(int position) { 
      return listSchedules.get(position).getCod_Horario(); 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      ViewHolder holder; 
      if (convertView == null) { 

       holder = new ViewHolder(); 
       convertView = this.inflater.inflate(R.layout.layout_list_item, 
         parent, false); 

       holder.txtTeacher = (TextView) convertView 
         .findViewById(R.id.txt_teacher); 
       holder.txtDayofWeek = (TextView) convertView 
         .findViewById(R.id.txt_dayofweek); 
       holder.txtSubject = (TextView) convertView 
         .findViewById(R.id.txt_subject); 
       holder.txtTime = (TextView) convertView 
         .findViewById(R.id.txt_time); 
       holder.txtRoom = (TextView) convertView 
         .findViewById(R.id.txt_room); 
       holder.txtClass = (TextView) convertView 
         .findViewById(R.id.txt_class); 

       convertView.setTag(holder); 
      } else { 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      Schedule sche = listSchedules.get(position); 
      String fullTeacher = sche.getProfessor(); 
      String[] names = fullTeacher.split(" "); 

      holder.txtTeacher.setText(names[0] + " " + names[names.length-1]); 
      holder.txtDayofWeek.setText(sche.getDia_Semana()); 
      holder.txtSubject.setText(sche.getDisciplina()); 
      holder.txtTime.setText(sche.getT_Entrada() + "h-" + sche.getT_Saida()+"h"); 
      holder.txtRoom.setText(sche.getSala()); 
      holder.txtClass.setText(sche.getTurma()); 


      return convertView; 
     } 

     private class ViewHolder { 
      TextView txtTeacher; 
      TextView txtDayofWeek; 
      TextView txtSubject; 
      TextView txtTime; 
      TextView txtRoom; 
      TextView txtClass; 
     } 

    } 

片段代碼(方法稱爲上的異步OnPostExecute法):

private void populateListView() { 

    ListScheduleAdapter adapter = new ListScheduleAdapter(getActivity(), ScheduleList); 
    listviewHorarios.setAdapter(adapter); 

} 

佈局代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="8dp" > 

     <LinearLayout 
      android:id="@+id/layout_row1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <TextView 
       android:id="@+id/txt_teacher" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textStyle="normal" /> 

      <TextView 
       android:id="@+id/txt_dayofweek" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textStyle="normal" /> 

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/layout_row2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/layout_row1" 
      android:orientation="horizontal" > 


      <TextView 
       android:id="@+id/txt_subject" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textStyle="normal" /> 

      <TextView 
       android:id="@+id/txt_time" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textStyle="normal" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/layout_row3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/layout_row2" 
      android:orientation="horizontal" > 

      <TextView 
       android:id="@+id/txt_room" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textStyle="normal" /> 

      <TextView 
       android:id="@+id/txt_class" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:textStyle="normal" /> 

     </LinearLayout> 

<TextView 
     android:id="@+id/empty" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:drawablePadding="5dp" 
     android:gravity="center" 
     android:text="@string/no_data_available" 
     android:textColor="@android:color/black" 
     android:visibility="gone" /> 

</RelativeLayout> 
+0

顯示初始化'Adapter'的位置? – hrskrs

回答

2

添加

<TextView 
     android:id="@+id/empty" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:drawablePadding="5dp" 
     android:gravity="center" 
     android:text="@string/no_data_available" 
     android:textColor="@android:color/black" 
     android:visibility="gone" /> 

到你宣佈你的ListView

上ListView的實例調用(在FrameLayout包裹兩者)相同的佈局

listView.setEmptyView(findViewById(R.id.empty)); 

當適配器是空的,你會看到TextView衝擊片雷管

+0

嗨,因爲我有一個自定義adapterclass,我該怎麼做?我不能簡單地放置「listView.setEmptyView(findViewById(R.id.empty));」在populateListView()方法上。 – Exprove

+0

它與適配器沒有任何關係。在活動/片段中添加該行。 ListView負責改變空視圖的可見性 – Blackbelt

+0

是的,我知道這與適配器無關。感謝它現在正在工作,我已經在適配器佈局,它是在片段佈局,我必須把空的領域。 – Exprove

1

TextView添加到您的listview佈局和setVisibility它到GONE並檢查適配器:

if(adapter==null){ 
listView.setVisiblity(View.GONE); 
tvEmpty.setVisiblity(View.VISIBLE); 
} 

希望這會有所幫助。

相關問題