如何在Android中的Json Array中显示Spinner中的数据

时间:2014-12-11 10:36:06

标签: android

class roleitem extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        RoleItem = HttpHitter.ExecuteData(RoleItem);
        JSONArray jsonArray;
        try {
            jsonArray = new JSONArray(RoleItem);

     stringArray = new ArrayList<String>();
            for (int i = 0, count = jsonArray.length(); i < count; i++) {

                JSONObject jsonObject = jsonArray.getJSONObject(i);
                stringArray.add(jsonObject.toString());

            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println(RoleItem);

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                R.layout.spinner_item, stringArray);

        dataAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // set the ArrayAdapter to the spinner
        rolespinner.setAdapter(dataAdapter);
    }
}

这是解析后得到的asynk任务RoleItem = [&#34; A&#34;,&#34; B]

并且在Postexcute上我希望在Spinner中绑定此数据但是我在RoleItem中收到错误构造函数ArrayAdapter&lt; String&gt;(roleeditpage.roleitem,int,ArrayList)未定义

1 个答案:

答案 0 :(得分:0)

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                R.layout.spinner_item, stringArray);
ArrayAdapter构造函数中的 引用AsyncTask类,而AsyncTask不以任何方式扩展Context。将 this 替换为 getApplicationContext() getActivity()(当AsyncTask是片段的内部类时,仅使用getActivity())应该执行此操作这个技巧是因为Activity正在扩展Context而ArrayAdapter的第一个参数是要求扩展Context的类。