gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:43:23 25 4
gpt4 key购买 nike

当getCount()返回0时,我无法设置“无结果”消息。
我为listview定制了一个布局,当listview上没有数据时,我只想得到一个“没有结果”的textview。
你知道怎么做到吗?
我看到了一个空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>

最佳答案

添加

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

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

关于android - 获取“无结果”消息自定义适配器ListView Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30710907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com