4

我有一个ArrayAdapter如下

ArrayAdapter<String>dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);

这是我要用于我的 .我的文件Spinner中的值集(字符串数组)的适配器。Spinnerstrings.xml

如何务实地设置微调器<string-array>string.xml请帮忙!

4

5 回答 5

5

只需在创建时将其添加到您的 ArrayAdapter 中。

String[] yourArray = getResources().getStringArray(R.array.array_name);

ArrayAdapter<String>dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, yourArray);
于 2013-01-28T19:24:56.300 回答
4

你可以尝试这样的事情:

String[] mTestArray;
ArrayAdapter<String>dataAdapter;

mTestArray = getResources().getStringArray(R.array.testArray);
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,mTestArray);

然后将适配器连接到 Spinner。

spinner.setAdapter(dataAdapter);

希望这可以帮助!!!

于 2013-01-28T19:25:36.887 回答
2
spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);  
String[] itemNames = getResources().getStringArray(R.array.stringArrayName); 

   for (int i = 0; i < 16; i++) // Maximum size of i upto --> Your Array Size 
    {  
    dataAdapter.add(itemNames[i]);  
     } 

spinner2.setAdapter(dataAdapter);
于 2013-01-28T19:30:15.043 回答
2

本教程:Android Spinners指出您将使用以下代码根据资源设置 Spinner 数组:

ArrayAdapter<CharSequence> dataAdapter = ArrayAdapter.createFromResource(this,
    R.array.string-array-id, android.R.layout.simple_spinner_item);
dataAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

使用该createFromResource()方法而不是new提供数组的资源 ID 应该可以提供预期的结果。有关更多详细信息,请参阅教程。

于 2013-01-28T19:24:39.333 回答
1

如果您想从 Web 服务加载数据并在微调器中设置该数据,您可以使用以下代码:-

public class MainActivity extends AppCompatActivity {

    Spinner spinner;
    List<String> usersList;
    ArrayAdapter<String> catAdapter;

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

        spinner = (Spinner)findViewById(R.id.spinner);
        usersList = new ArrayList<String>();

        catAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, usersList);
        spinner.setAdapter(catAdapter);

        getHttpResponse();

    }

    public void getHttpResponse() {

        String url = "https://cakeapi.trinitytuts.com/api/listuser";

        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .header("Accept", "application/json")
                .header("Content-Type", "application/json")
                .build();

        client.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                String mMessage = e.getMessage().toString();
                Log.w("failure Response", mMessage);
                //call.cancel();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                final String mMessage = response.body().string();

                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.e("Response", mMessage);
                        try {
                            JSONObject users = new JSONObject(mMessage);
                            JSONArray usersArr = users.getJSONArray("users");

                            for(int i =0; i < usersArr.length(); i++){
                                JSONObject user = usersArr.getJSONObject(i);
                                Log.e("user", user.getString("username"));
                                usersList.add(user.getString("username"));
                            }

                            catAdapter.notifyDataSetChanged();


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
            }
        });
    }
}

您可以在这篇文章中阅读完整信息:-将动态数据添加到微调器

于 2020-01-09T09:39:19.557 回答