alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Android example source code file (ListWithEmptyView.java)

This example Android source code file (ListWithEmptyView.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Android by Example" TM.

Java - Android tags/keywords

android, app, arrayadapter, arrayindexoutofboundsexception, carefuladapter, content, item, listactivity, listwithemptyview, menu_add, menu_remove, os, override, string, ui, view

The ListWithEmptyView.java Android example source code

/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.widget.listview;

import com.android.frameworks.coretests.R;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;


/**
 * Tests using an empty view with a list */
public class ListWithEmptyView extends ListActivity {
    
    private class CarefulAdapter<T> extends ArrayAdapter {

        public CarefulAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
            // TODO Auto-generated constructor stub
        }

        @Override
        public long getItemId(int position) {
            if (position <  0 || position >= this.getCount()) {
                throw new ArrayIndexOutOfBoundsException();
            }
            return super.getItemId(position);
        }
       
        
    }
    
    public static final int MENU_ADD = Menu.FIRST + 1;
    public static final int MENU_REMOVE = Menu.FIRST + 2;
    
    private CarefulAdapter<String> mAdapter;
    
    private int mNextItem = 0;
    
    private View mEmptyView;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = new CarefulAdapter<String>(this,
                android.R.layout.simple_list_item_1);
        setContentView(R.layout.list_with_empty_view);
        setListAdapter(mAdapter);
        
        mEmptyView = findViewById(R.id.empty);
        getListView().setEmptyView(mEmptyView);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_ADD, 0, R.string.menu_add)
                .setIcon(android.R.drawable.ic_menu_add);
        menu.add(0, MENU_REMOVE, 0, R.string.menu_remove)
                .setIcon(android.R.drawable.ic_menu_delete);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_ADD:
                String str = "Item + " + mNextItem++;
                mAdapter.add(str);
                return true;
            case MENU_REMOVE:
                if (mAdapter.getCount() > 0) {
                    mAdapter.remove(mAdapter.getItem(0));
                }
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public View getEmptyView() {
        return mEmptyView;
    }
   
}

Other Android examples (source code examples)

Here is a short list of links related to this Android ListWithEmptyView.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.