Как использовать макет как пустой вид для списка, когда адаптер имеет нулевые элементы?
setEmptyView не работает с этим кодом:
public class ZeroItemListActivity extends Activity {
private ArrayList<String> listItems=new ArrayList<String>();
private ListView mMyListView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMyListView=(ListView) findViewById(R.id.MyListView);
mMyListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listItems));
LayoutInflater inflator=getLayoutInflater();
View emptyView=inflator.inflate(R.layout.empty_list_item, null);
//Empty view is set here
mMyListView.setEmptyView(emptyView);
}
public void addItem(View v){
listItems.add("list Item");
mMyListView.invalidate();
}
}
Используемые макеты: empty_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="@+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Empty List,Click to add items"></Button>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/MyListView"></ListView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/AddItemsButton"
android:text="Add Items" android:onClick="addItem"></Button>
</LinearLayout>