A list view that demonstrates the use of setEmptyView. This example alos uses a custom layout file that adds some extra buttons to the screen. : Button « UI « Android

Home
Android
1.2D Graphics
2.Animation
3.Core Class
4.Database
5.Date Type
6.Development
7.File
8.Game
9.Hardware
10.Media
11.Network
12.Security
13.UI
14.User Event
Android » UI » Button 
A list view that demonstrates the use of setEmptyView. This example alos uses a custom layout file that adds some extra buttons to the screen.
 
 
 
/*
 * Copyright (C) 2007 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 com.example.android.apis.view;

import com.example.android.apis.R;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.AbsListView;

import java.util.ArrayList;


/**
 * A list view that demonstrates the use of setEmptyView. This example alos uses
 * a custom layout file that adds some extra buttons to the screen.
 */
public class List8 extends ListActivity {

    PhotoAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Use a custom layout file
        setContentView(R.layout.list_8);
        
        // Tell the list view which view to display when the list is empty
        getListView().setEmptyView(findViewById(R.id.empty));
        
        // Set up our adapter
        mAdapter = new PhotoAdapter(this);
        setListAdapter(mAdapter);
        
        // Wire up the clear button to remove all photos
        Button clear = (ButtonfindViewById(R.id.clear);
        clear.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                mAdapter.clearPhotos();
            } });
        
        // Wire up the add button to add a new photo
        Button add = (ButtonfindViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                mAdapter.addPhotos();
            } });
    }

    /**
     * A simple adapter which maintains an ArrayList of photo resource Ids. 
     * Each photo is displayed as an image. This adapter supports clearing the
     * list of photos and adding a new photo.
     *
     */
    public class PhotoAdapter extends BaseAdapter {

        private Integer[] mPhotoPool = {
                R.drawable.sample_thumb_0, R.drawable.sample_thumb_1, R.drawable.sample_thumb_2,
                R.drawable.sample_thumb_3, R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                R.drawable.sample_thumb_6, R.drawable.sample_thumb_7};

        private ArrayList<Integer> mPhotos = new ArrayList<Integer>();
        
        public PhotoAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mPhotos.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // Make an ImageView to show a photo
            ImageView i = new ImageView(mContext);

            i.setImageResource(mPhotos.get(position));
            i.setAdjustViewBounds(true);
            i.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            // Give it a nice background
            i.setBackgroundResource(R.drawable.picture_frame);
            return i;
        }

        private Context mContext;

        public void clearPhotos() {
            mPhotos.clear();
            notifyDataSetChanged();
        }
        
        public void addPhotos() {
            int whichPhoto = (int)Math.round(Math.random() (mPhotoPool.length - 1));
            int newPhoto = mPhotoPool[whichPhoto];
            mPhotos.add(newPhoto);
            notifyDataSetChanged();
        }

    }
}
//list_8.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C2007 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.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent" 
    android:layout_height="match_parent">
    
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content">
        
        <Button android:id="@+id/add"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:text="@string/list_8_new_photo"/>
            
        <Button android:id="@+id/clear"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:text="@string/list_8_clear_photos"/>
            
    </LinearLayout>
    
    <!-- The frame layout is here since we will be showing either
    the empty view or the list view.  -->
    <FrameLayout
        android:layout_width="match_parent" 
        android:layout_height="0dip"
        android:layout_weight="1" >
        <!-- Here is the list. Since we are using a ListActivity, we
             have to call it "@android:id/list" so ListActivity will
             find it -->
        <ListView android:id="@android:id/list"
            android:layout_width="match_parent" 
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false"/>
        
        <!-- Here is the view to show if the list is emtpy -->
        <TextView android:id="@+id/empty"
            android:layout_width="match_parent" 
            android:layout_height="match_parent"
            android:text="list_8_no_photos"/>
            
    </FrameLayout>
        
</LinearLayout>

   
  
Related examples in the same category
1.Add Button to Activity
2.Add Button from layout xml file
3.Add button on click listener
4.Button on click event
5.Get data from RadioButton
6.Set one of the three radio buttons to be selected by default, using android:checked = "true".
7.Load Button from layout xml file and add action listener
8.Add listener to Toggle Button
9.Layout Radio buttons with RadioGroup
10.Layout Button inside Activity
11.Using switch statement to check the RadioButton selection
12.ImageButton Demo
13.Set Button text in xml file
14.Is ToggleButton selected
15.Set Button width and height
16.Show or hide button
17.Using ImageButton
18.Clear RadioButton selection
19.Add key listener to add the new todo item when the middle D-pad button is pressed.
20.Set ToggleButton on/off text
21.Using Button to control MediaPlayer
22.Button Gallery
23.A gallery of basic controls: Button, EditText, RadioButton, Checkbox, Spinner. This example uses the light theme.
24.This demonstrates the use of the PopupMenu class. Clicking the button will inflate and show a popup menu from an XML resource.
25.Adding a radio button programmatically
26.extends Button
27.Create Button
28.Create Image Button
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.