Подтвердить что ты не робот

Установка макета для использования в качестве пустого представления для ListView в случае, когда адаптер имеет нулевые элементы в Activity

Как использовать макет как пустой вид для списка, когда адаптер имеет нулевые элементы?

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>
4b9b3361

Ответ 1

Самый простой способ сделать это - поместить пустой вид в макет 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>
      <!-- empty view -->  
      <LinearLayout android:id="@+id/emptyView"
          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>

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/AddItemsButton"
        android:text="Add Items" 
        android:onClick="addItem"></Button>
   </LinearLayout>

Затем в вашем коде установите пустое представление следующим образом:

mMyListView.setEmptyView(findViewById(R.id.emptyView));

Причина, по которой ваш код не работает, заключается в том, что представление ListView и Empty должно быть в одном представлении иерархии. И ваш вызов inflate передает null в качестве родителя, чтобы они находились в разных иерархиях.

Ответ 2

Учебник Android Dev для Activity List охватывает эту тему: http://developer.android.com/reference/android/app/ListActivity.html

В основном ваш класс расширяет ListActivity. Установите идентификатор списка @id/android:list и идентификатор "пустой вид" на @id/android:empty в том же XML-макете.

Надеюсь, что это поможет:)

Ответ 3

Когда, раздувая представление, вы должны установить родительский вид представления как часть иерархии макета, а не передавать null в некоторых случаях.

Заменить

View emptyView = inflator.inflate(R.layout.empty_list_item, null);

с

View emptyView = inflator.inflate(R.layout.empty_list_item, (ViewGroup) mMyListView.getParent());

то вы установите пустой вид, как вы делали раньше.

mMyListView.setEmptyView(findViewById(R.id.emptyView));