how to inflate a list view to a fragment

andro-girl picture andro-girl · Mar 6, 2012 · Viewed 7.1k times · Source

i am using fragment in my application.there are 5 tabs.in 1 tab i need to inflate a list view on a button click.given below is my code for that tab.

public class TabStoreLocatorFragment extends Fragment implements OnClickListener {

private Button mapSwitcher;
private LinearLayout lastSelectedStoreButton;
private LinearLayout noSelectedStoreSection;
private TextView storeInfoName;
private TextView storeInfoAddress;
private TextView storeInfoCity;
private RelativeLayout phoneLocationButton;
private EditText searchStoreLocation;
private ListView storeList;
private ViewFlipper storeFlipper;
LinearLayout theLayout;
Store store;

@SuppressWarnings("static-access")
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
        }

     theLayout = (LinearLayout) inflater.inflate(
            R.layout.tab_store_locator_layout, container, false);

    phoneLocationButton = (RelativeLayout) theLayout.findViewById(R.id.phone_location_button);
    mapSwitcher=(Button)theLayout.findViewById(R.id.switch_state_button);
    lastSelectedStoreButton=(LinearLayout)theLayout.findViewById(R.id.last_selected_store_button);
    noSelectedStoreSection=(LinearLayout)theLayout.findViewById(R.id.no_selected_store);
    storeInfoName=(TextView)theLayout.findViewById(R.id.store_name);
    storeInfoAddress=(TextView)theLayout.findViewById(R.id.store_address);
    storeInfoCity=(TextView)theLayout.findViewById(R.id.store_city);
    searchStoreLocation=(EditText)theLayout.findViewById(R.id.search_store);
    storeFlipper = (ViewFlipper)theLayout.findViewById(R.id.store_flipper);


    store=new Store(); 
    store.setName("store1");
    store.setAddress("california");
    store.setCity("newyork");
    store.setZipCode("23");
    store.setState("california");

    phoneLocationButton.setOnClickListener(this);
    mapSwitcher.setOnClickListener(this);
    lastSelectedStoreButton.setOnClickListener(this);
    return theLayout;

}


@Override
public void onClick(View view) {
    int id = view.getId();
    if (id == R.id.phone_location_button) {

        storeList = (ListView)storeFlipper.inflate(getActivity(),   R.layout.store_list, storeFlipper);
        storeFlipper.addView(storeList);
        StoreAdapter adapter = new StoreAdapter(getActivity(), R.layout.store_list_item);
        storeList.setAdapter(adapter);


    }
    else if(id == R.id.switch_state_button){

    }
    else{


    }


}

     }

i want to inflate storelist to storeFlipper.i tried to do that.but it is not working.given below is the corresponding layout for that tab.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@android:color/white" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/find_a_store_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="15dp"
            android:layout_alignParentLeft="true"
            android:text="@string/tab_find_a_store_title"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />

        <Button android:id="@+id/switch_state_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:background="@drawable/switch_selector" />       

    </RelativeLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#E8E2D3" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#C5BFAD" />

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <LinearLayout android:id="@+id/last_selected_store_button"  android:baselineAligned="false" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="@android:style/Widget.Button"
            android:orientation="horizontal"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:paddingRight="5dp"
            android:background="@drawable/last_selected_store_background" >

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" >        

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"              
                    android:textColor="#8E8672"
                    android:text="@string/last_selected_store_label"
                    android:textSize="12sp" />

            </RelativeLayout>        

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="5dp"            
                android:orientation="vertical" >

                <TextView android:id="@+id/store_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/black"
                    android:textSize="12sp" />

                <TextView android:id="@+id/store_address"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/black"
                    android:textSize="12sp" />         

                <TextView android:id="@+id/store_city"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/black"
                    android:textSize="12sp" />          

            </LinearLayout>      

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <ImageView 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:background="@drawable/arrow" />

            </RelativeLayout>

        </LinearLayout>         

        <LinearLayout android:id="@+id/no_selected_store"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:paddingRight="5dp"
            android:background="@drawable/last_selected_store_background"  >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"                
                android:textColor="#8E8672"
                android:text="@string/no_selected_store_label"
                android:textSize="12sp" />

        </LinearLayout>             

    </FrameLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" >

        <EditText android:id="@+id/search_store"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:singleLine="true"
            android:textSize="14sp"
            android:hint="@string/search_store_placeholder"
            android:layout_toLeftOf="@+id/phone_location_button"
            android:background="@drawable/search_store_background" />

        <RelativeLayout android:id="@id/phone_location_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:background="@drawable/get_location_background" >

            <ImageView              
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/get_location" />

        </RelativeLayout>

    </RelativeLayout>        

    <ViewFlipper android:id="@+id/store_flipper"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>       

</LinearLayout>    

can anybody help me to inflate the list to flipper.

Answer

andro-girl picture andro-girl · Mar 9, 2012

it is simple.just put this code.

 storeList = (ListView) inflater.inflate(R.layout.store_list, null);

it is working