获取“无结果”消息自定义适配器ListView Android

时间:2015-06-08 13:49:50

标签: android android-listview android-adapter

当getCount()返回0时,我遇到设置“无结果”消息的问题。

我有listview的自定义布局,我只想在listview上没有数据的情况下获得“无结果”文本视图。

知道如何实现这个目标吗?

我看到了一个空ID的东西,但是需要在listview上,因为这是一个自定义布局,它没有那个“标签”。

自定义适配器代码:

  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;
        }

    }

Fragment Code(在异步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>

2 个答案:

答案 0 :(得分: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的同一布局(在FrameLayout中包装)

ListView的实例调用

上的

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

当适配器为空时,您将看到TextView apper

答案 1 :(得分:1)

TextView添加到您的listview布局,将setVisibility添加到GONE并检查适配器:

if(adapter==null){
listView.setVisiblity(View.GONE);
tvEmpty.setVisiblity(View.VISIBLE);
} 
希望这会有所帮助。