【问题标题】:Android empty ArrayList and SpinnerAndroid 空 ArrayList 和 Spinner
【发布时间】:2018-02-02 11:28:54
【问题描述】:

嘿,我正在创建与我的数据库 MySQL 连接的 Android 应用程序。我创建了一个带有类别的 ArrayList,并将其添加到 Adapter,然后添加到 Spinner。我看到 Spinner 项目,但在 onClick 之后没有任何反应,当我尝试检查 ArrayList 中的内容时,我看到它是空的。数据已下载。

点击 Spinner 后没有任何变化

这是我的代码:

public class InsertProductActivity extends AppCompatActivity {
    Spinner categories;
    String categoriesURL = "myURL";
    ArrayList<String> downloadedCategories = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert_product);
        categories = findViewById(R.id.categories);
        getCategories();
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,downloadedCategories);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        categories.setAdapter(adapter);
    }
    protected void getCategories(){
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, categoriesURL
                , new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("categories");
                            for(int i=0;i<jsonArray.length();i++){
                                downloadedCategories.add(jsonArray.getJSONObject(i).getString("name"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
        });
        requestQueue.add(jsonObjectRequest);
    }
}

【问题讨论】:

    标签: java android mysql arraylist spinner


    【解决方案1】:

    试试这个。

    protected void getCategories(){
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, categoriesURL, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArray = response.getJSONArray("categories");
                    downloadedCategories = new ArrayList<>(); // try to add your instantiation of your arrayList here.
                    for(int i=0;i<jsonArray.length();i++) {
                        downloadedCategories.add(jsonArray.getJSONObject(i).getString("name"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
    
        }
    });
    //this block of code will prompt after your downloadedCategories finish storing your objects to your arrayList because if you put this in onCreate this wil call before your getCategories process not finish yet. So much better its much safer here.
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,downloadedCategories);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        categories.setAdapter(adapter);
        requestQueue.add(jsonObjectRequest);
    }
    

    【讨论】:

    • 在您的解决方案中,当我尝试将 downloadCategories 添加到 adapetr 时它是空的,我在 try 块中的 for 循环之后添加了初始化,现在它正在工作:)
    • 很好 :) 我在 for loop 之前添加了我的实例化,如果这对你有帮助的话。您可以将此标记为答案谢谢:)
    猜你喜欢
    • 2013-11-18
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 2012-04-21
    相关资源
    最近更新 更多