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