在从ArrayList转换为String []之后的Android中,在spinner中填充空值

时间:2014-09-23 13:01:10

标签: java android arraylist android-arrayadapter android-spinner

我从mysql数据库动态填充值并将数据添加到Arraylist。现在,如果我想在spinner中填充数据,则必须将其转换为我已使用

转换的String []数组

AidStr是一个arraylist变量,包含数据库中的值。

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

并且

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

但它不起作用我检查了字符串数组的大小,它在logcat中显示为null,如下所示

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

转换后数组的长度:0 转换后的阵列长度为AIDSTR:0

当我检查arraylist值时,我在logcat中获得正确的输出,当我转换为string []时,它的堆栈是空的,长度等于0

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

打印arraylist变量后的Logcat输出。

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

请帮助将值转换为字符串[]并将正确的数据动态填充到微调器

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;
        }}}

我应该如何将Arraylist Aidstr转换为String [] Stringarray(假设)变量,以便我可以将varable链接到ArrayAdapter并填充值

字符串数组必须如下所示

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" />

这是实现微调器的xml文件

1 个答案:

答案 0 :(得分:1)

您正在执行在后台运行的AsyncTask。 将此代码放在一边 的 @覆盖         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);