I'm using a paginator with a few Fragments
.
我使用的是带有一些片段的paginator。
I want to show a message when there are no entries in the list (Fragment
).
当列表(片段)中没有条目时,我想显示一条消息。
I tried to use Textview
with android:id="@id/android:empty
but it doesn't work.
我尝试使用Textview和android:id="@id/android:为空,但不起作用。
Custom list layout code:
自定义列表布局代码:
<?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:padding="@dimen/padding_medium" >
<TextView
android:id="@+id/label_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/padding_medium"
android:text="@string/label_value"
android:textColor="@android:color/white"
android:textSize="18sp" />
<TextView
android:id="@+id/label_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/padding_medium"
android:text="@string/title_time"
android:textColor="@android:color/white"
android:textSize="16sp" />
</RelativeLayout>
List adapter class:
列表适配器类:
public class JournalAdapter extends ArrayAdapter<String> {
private final Context context;
private final List<String> values;
private final List<String> dates;
public JournalAdapter(Context context, List<String> values,
List<String> dates) {
super(context, R.layout.layout_journal_list, values);
this.context = context;
this.values = values;
this.dates = dates;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.layout_journal_list, parent, false);
TextView value = (TextView) rowView.findViewById(R.id.label_glucose);
TextView date = (TextView) rowView.findViewById(R.id.label_date);
value.setText(values.get(position));
date.setText(dates.get(position));
return rowView;
}
}
List parsing method:
列表解析方法:
private void initListView() {
values = dataSource.getAllValues();
List<String> values = new ArrayList<String>();
List<String> dates = new ArrayList<String>();
for (Value value : values) {
values.add(Double.toString(value.getValue()));
dates.add(secondsToDate(value.getDate()));
}
setListAdapter(new JournalAdapter(context, values, dates));
listView = getListView();
listView.setEmptyView(noItems("NO ITEMS"));
listView.setOnItemClickListener(this);
}
noItems
method:
noItems方法:
private TextView noItems(String text) {
TextView emptyView = new TextView(context);
emptyView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
emptyView.setText(text);
emptyView.setTextColor(Color.WHITE);
emptyView.setTextSize(20);
emptyView.setVisibility(View.GONE);
emptyView.setGravity(Gravity.CENTER_VERTICAL
| Gravity.CENTER_HORIZONTAL);
return emptyView;
}
2 个解决方案
#1
23
This worked for me (got the solution from this question asked by William Remacle). What you might have missed is adding the created TextView
to the ListView
layout parent (second line of code from the bottom of the noItems
method below):
这对我很有效(从威廉·雷马克的问题中得到了答案)。您可能会漏掉的是将创建的TextView添加到ListView布局父类中(代码的第二行来自下面noItems方法的底部):
In my ListFragment
I used the following method to get a view for the empty view:
在我的列表片段中,我使用以下方法获取空视图的视图:
private TextView noItems(String text) {
TextView emptyView = new TextView(getActivity());
//Make sure you import android.widget.LinearLayout.LayoutParams;
emptyView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//Instead of passing resource id here I passed resolved color
//That is, getResources().getColor((R.color.gray_dark))
emptyView.setTextColor(getResources().getColor(R.color.gray_dark));
emptyView.setText(text);
emptyView.setTextSize(12);
emptyView.setVisibility(View.GONE);
emptyView.setGravity(Gravity.CENTER_VERTICAL
| Gravity.CENTER_HORIZONTAL);
//Add the view to the list view. This might be what you are missing
((ViewGroup) getListView().getParent()).addView(emptyView);
return emptyView;
}
Then in the onStart()
method of the the ListFragment
set the the empty view:
然后在ListFragment的onStart()方法中设置空视图:
@Override
public void onStart() {
super.onStart();
getListView().setEmptyView(
noItems(getResources().getString(R.string.widget_empty)));
}
#2
2
android:id="@android:id/empty"
and not
而不是
android:id="@id/android:empty"
#1
23
This worked for me (got the solution from this question asked by William Remacle). What you might have missed is adding the created TextView
to the ListView
layout parent (second line of code from the bottom of the noItems
method below):
这对我很有效(从威廉·雷马克的问题中得到了答案)。您可能会漏掉的是将创建的TextView添加到ListView布局父类中(代码的第二行来自下面noItems方法的底部):
In my ListFragment
I used the following method to get a view for the empty view:
在我的列表片段中,我使用以下方法获取空视图的视图:
private TextView noItems(String text) {
TextView emptyView = new TextView(getActivity());
//Make sure you import android.widget.LinearLayout.LayoutParams;
emptyView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//Instead of passing resource id here I passed resolved color
//That is, getResources().getColor((R.color.gray_dark))
emptyView.setTextColor(getResources().getColor(R.color.gray_dark));
emptyView.setText(text);
emptyView.setTextSize(12);
emptyView.setVisibility(View.GONE);
emptyView.setGravity(Gravity.CENTER_VERTICAL
| Gravity.CENTER_HORIZONTAL);
//Add the view to the list view. This might be what you are missing
((ViewGroup) getListView().getParent()).addView(emptyView);
return emptyView;
}
Then in the onStart()
method of the the ListFragment
set the the empty view:
然后在ListFragment的onStart()方法中设置空视图:
@Override
public void onStart() {
super.onStart();
getListView().setEmptyView(
noItems(getResources().getString(R.string.widget_empty)));
}
#2
2
android:id="@android:id/empty"
and not
而不是
android:id="@id/android:empty"