0

I want to return the value of choice in String prodName[] but I always got the value of choice is always null please help sorry for my bad English.

enter image description here

String prodName[]={choice};
int Quantity[] = {};
int Total[]={};
String Price[]={choice} ;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ArrayAdapter<CharSequence> badapter = ArrayAdapter.createFromResource(
                                              this,
                                              R.array.products,
                                              android.R.layout.simple_spinner_dropdown_item);
    listView = (ListView) findViewById(R.id.list1);
    button = (Button) findViewById(R.id.button);
    spinner = (Spinner)findViewById(R.id.spinner);
    badapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(badapter);


    final MyAdapter adapter = new MyAdapter(this, prodName, Price);
    //set the adapter
    listView.setAdapter(adapter);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String sample;

            choice = spinner.getSelectedItem().toString();
            prodName = choice.split(choice);

        }
    });

}
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • i got the value of spinner in choice but when i put it on string prodName i cant get the value it give me always a null – Beginner Daddy Dec 05 '18 at 03:11
  • I believe your problem is related to your use of the java array. https://stackoverflow.com/a/13197760/8560690 – Jantzilla Dec 05 '18 at 04:38

2 Answers2

0

I guess you're using split() method in the wrong way.

String demo = "1,2,3,4";
String[] splitted = demo.split(",");  //splitted will be an array containing 4 elements [1, 2, 3, 4]

Now I guess you know what the split() method does. :)

Since you already got the selected item text as String in the variable choice. why are you calling split() method on it?

You can find more about the split() method here

Vivek Av
  • 1
  • 1
0

Try below way to initialise array

String[] prodName={choice};
Mayur Dabhi
  • 3,607
  • 2
  • 14
  • 25