1

I want to create a Spinner with multiple choice, and every one that chooses, must add to the spinner box.my spinner has a dropDown List.pictures below will describe what I want to create:

text 1 is selected

text 1 and text 2 are selected

text 1 and 2 and 3 are selected

how can I create this Spinner???

Aswin P Ashok
  • 702
  • 8
  • 27
Azin Nilchi
  • 849
  • 1
  • 10
  • 24

2 Answers2

2

try this you can use TextView.append() method.

Convenience method to append the specified text slice to the TextView's display buffer, upgrading it to EDITABLE if it was not already editable.

sample code

textView = (TextView) findViewById(R.id.text);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        String s=destspin1.getSelectedItem().toString();
        textView.append(s);

      /* or try this
       String str=textView.getText().toString().trim();
       textView.setText(str+s);*/
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
    }

});
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • I didn't fully understand your solution! tvSchool is the TextView for the Spinner?? I tried it, but didn't work so far,can you explain more? – Azin Nilchi Sep 16 '17 at 06:55
  • @AzinNilchi replace **tvSchool** with your **Textview** – AskNilesh Sep 16 '17 at 07:06
  • I tried that,to put the spinner text view for using append that you said, but it will also crash! with this error : java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.append(java.lang.CharSequence)' on a null object reference – Azin Nilchi Sep 16 '17 at 07:14
1

I have create a full code for you just apply

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_test_code"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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="com.ncrypted.thumbpin.TestCode">

    <Spinner
        android:layout_marginTop="20dp"
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="dsasas"
        android:textColor="@color/black"
        android:textSize="20dp" />
</LinearLayout>

JAVA

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;

public class TestCode extends AppCompatActivity {

    Spinner spinner;
    TextView textView;


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

        textView = (TextView) findViewById(R.id.text);
        spinner = (Spinner) findViewById(R.id.spinner);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, new String[]{"String 1", "String 2", "String 3", "String 4", "String 5"});
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                String s = spinner.getSelectedItem().toString();
                textView.append(s);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }

        });

    }
}

NOTE: if you want to add particular item at only once than you should have to use ArrayList to store selected items in to array list and loop the array and check the item is already added or not

ND1010_
  • 3,743
  • 24
  • 41
  • I think you didn't understand my question. I don't want a separate textView to show these selection! I want to show Spinner selection in Spinner Itself! like images that I've shown – Azin Nilchi Sep 16 '17 at 07:12