David Brown
David Brown

Reputation: 4823

Adding Spinner as ListView Data

I want to add the Spinner inflated in the ListView of the Main Activity.

I have ten Items in the ListView and I want to add that inflated Spinner in the ListView(10 times).

I have searched a lot but could not find anything to set the Spinner in the ListView Data.

Can anybody help me out here ?

Thanks David Brown.

Upvotes: 2

Views: 4385

Answers (2)

Md. Arafat Al Mahmud
Md. Arafat Al Mahmud

Reputation: 3214

simply override the getView method of your custom adapter. Inside this you inflate the xml for each row of the ListView where you have defined the Spinner element. I have written a sample project for you. It works fine according to your requirement. Consider this as an skeleton project and modify it as per needs.

Here goes the activity_main.xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
    </ListView>

</RelativeLayout>

Here goes the layout file for each row of the ListView I have named it list_row.xml:

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

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

And here goes the MainActivity.java :

package com.example.so;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Spinner;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView=(ListView)findViewById(R.id.listView1);
        CustomAdapter adapter=new CustomAdapter();
        listView.setAdapter(adapter);
    }

    @SuppressLint("ResourceAsColor")
    class CustomAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 10;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            LayoutInflater inflater=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

            convertView=inflater.inflate(R.layout.list_row,parent,false);

            Spinner spinner=(Spinner) convertView.findViewById(R.id.spinner1);


            String[] colors={"Red","Green","Blue"};

            ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item,colors);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
            return convertView;
        }

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Hope my effort helps you.

Upvotes: 5

KP_
KP_

Reputation: 1866

If you want to implement ICSSpinner ,Follow these steps.

Inorder to implement this ,You required ActionbarSherlock library

Add the IcsSpinner widget to your layout.xml:

    <com.actionbarsherlock.internal.widget.IcsSpinner
    android:id="@+id/spinner_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:entries="@array/entries" />

Access your spinner via code:

IcsSpinner mySpinner = (IcsSpinner)findViewById(R.id.spinner_id);

Upvotes: 1

Related Questions