Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

i am populating the values dynamically from mysql database and adding the data to the Arraylist. Now if i want to populate the data in spinner it has to be converted into String [] array i have converted using

AidStr is a arraylist variable which contains values from the database.

Object[] ObjectList = AidStr.toArray();
  String[] StringArray = Arrays.copyOf(ObjectList,ObjectList.length,String[].class); 

And

String[] stringArr = AidStr.toArray(new String[AidStr.size()]);

but it is not working i checked the size of the string array and it is showing as null in logcat as shown below

System.out.println("Lengtth of an array after convertion:"+stringArr.length);
  System.out.println("Lengtth of an array after convertion AIDSTR:"+AidStr.size());

Lengtth of an array after convertion: 0 Lengtth of an array after convertion AIDSTR: 0

when i check the arraylist values i am getting the correct output in logcat when i convert to string[] its stack is empty and length is equal to 0

 System.out.println("AID STRING ALL THE VALUES:"+AidStr);

Logcat output after printing the arraylist variable.

AID STRING ALL THE VALUES: [Apple, Ball, Bat]

Please help to convert the values to string[] and populate the correct data dynamically into spinner

MainActivity.java 

public class MainActivity extends Activity  {

final ArrayList<String> AidStr=new ArrayList<String>();
String php_send = null;
String name="fruits";
@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.mer_payment);
  System.out.println("AID STRING ALL THE VALUES ITEMS:"+items);
  new AsyncTaskOperation().execute(php_send);

  /*First method returning null values it is not working */
  Object[] ObjectList = AidStr.toArray();
  String[] StringArray = Arrays.copyOf(ObjectList,ObjectList.length,String[].class);

  /*One more method it is not working */
  String[] stringArr = AidStr.toArray(new String[AidStr.size()]);

  Spinner dropdown = (Spinner)findViewById(R.id.spinner_tag);
  dropdown.setOnItemSelectedListener(this);
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,    android.R.layout.simple_spinner_item, StringArray);
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  dropdown.setAdapter(adapter);

   Button buy_m = (Button)findViewById(R.id.buy_merchan);
  buy_m.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        Intent buy_merchan = new Intent(this,PayActivity.class);
        buy_merchan.putExtra("title", name);
        startActivity(buy_merchan);
    }

  }); 
 }
public class AsyncTaskOperation extends AsyncTask <String, Void, Void>
    {
        @Override
         protected Void doInBackground(String... paramsObj) {
            php_send = "http://localhos/Android/App/mer.php";
            String str = "";
            HttpResponse response;
            HttpClient myClient = new DefaultHttpClient();
            HttpPost myConnection = new HttpPost(php_send);
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                nameValuePairs.add(new BasicNameValuePair("name", name));
                myConnection.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response = myClient.execute(myConnection);
                str = EntityUtils.toString(response.getEntity(), "UTF-8");      
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            String map; 
            try 
            {
                JSONArray data = new JSONArray(str);
                for (int i = 0; i < data.length(); i++) {
                    JSONObject c = data.getJSONObject(i);
                    map = c.getString("variation");
                    AidStr.add(map); //add the values to the arraylist
                }   
            }catch (JSONException e) {
                    e.printStackTrace();
            }
            System.out.println("AID STRING ALL THE VALUES:"+AidStr);

        return null;
        }}}

how should i convert the Arraylist Aidstr to String[] Stringarray(assumption) variable such that i can link the varable into ArrayAdapter and populate the values

String array has to look like this

String[] items = { "Apple", "Ball", "Bat"}; instead now it is populating as shown above arraylist values

XML:

<Spinner
         android:id="@+id/spinner_tag"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="24dp"
         style="@style/spinner_style"
         android:popupBackground="#D3D5D3"
         android:prompt="@string/select" />

This is xml file where spinner is implemented

share|improve this question
1  
at the time of conversion AidStr is empty ... AsyncTask.execute() is not blocking call it will not wait until doInBackground is finished ... – Selvin Sep 23 '14 at 13:16
    
@Selvin can u tell me what i have to change in this code my friend please – ANNN CHOOOR Sep 24 '14 at 1:49
up vote 1 down vote accepted

you are executing AsyncTask which run in background. put this code in side @Override protected void onPostExecute

  Object[] ObjectList = AidStr.toArray();
  String[] StringArray = Arrays.copyOf(ObjectList,ObjectList.length,String[].class);

  /*One more method it is not working */
  String[] stringArr = AidStr.toArray(new String[AidStr.size()]);

  Spinner dropdown = (Spinner)findViewById(R.id.spinner_tag);
  dropdown.setOnItemSelectedListener(this);
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,    android.R.layout.simple_spinner_item, StringArray);
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  dropdown.setAdapter(adapter);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.