当getCount()返回0时,设置"No结果“消息有问题。
我有一个自定义的列表视图布局,当列表视图上没有数据时,我只想得到一个"No Results“文本视图。
知道如何做到这一点吗?
我看到了一些带有空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;
}
}片段代码(在异步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>发布于 2015-06-08 13:54:39
添加
<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
发布于 2015-06-08 14:00:28
将TextView添加到listview布局中,并将其setVisibility添加到GONE中,并检查适配器:
if(adapter==null){
listView.setVisiblity(View.GONE);
tvEmpty.setVisiblity(View.VISIBLE);
} 希望这能帮上忙。
https://stackoverflow.com/questions/30710907
复制相似问题