cj1098
cj1098

Reputation: 1610

Having a Custom ListView with a spinner and checkbox Implemented

All I'm looking to do, is have a ListView with a textView and EITHER a spinner on the right side or a check box. I'm having so much difficulty with this it's ridiculous. Can anyone help me please? :(.

Here's what i have so far that implements a spinner by itself. (I created R.array for planets. so it does work.)

public class AlarmOptions extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        Spinner mySpinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.planets, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mySpinner.setAdapter(adapter);
        Spinner mySpinner1 = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this, R.array.planets1, android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mySpinner1.setAdapter(adapter1);
    }

Here is the XML file main2.xml

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

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Please select a planet:"
/>

<Spinner 
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="@string/planet_prompt"
/>
<Spinner 
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="@string/planet_prompt1"
/>

</LinearLayout>

PLEASE HELP :'(

Upvotes: 1

Views: 1824

Answers (1)

ahodder
ahodder

Reputation: 11439

What you could do is: Create a list view like normal. Then create a separate xml file with the layout your would like for each item in the list view. This would work for both the spinner and the checkbox and is simple and easy to create.

Please consider:

<...xml header stuff .../>

<LinearLayout android:id="@+id/ll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Spinner (or Checkbox) android:id="@+id/spinny"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <TextView android:id="@+is/texty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

And then for your activity you would create a ListView just like you normally would. inflating this layout for each ListView item in the getView(...args...) method of the ListView adapter.

Upvotes: 2

Related Questions