如何将我的异步任务数据放在微调器中?

时间:2017-06-13 07:47:45

标签: java android asynchronous android-asynctask

我正在使用我自己创建的一些API的Android应用程序。我差不多完成但是我找不到将数据放入我的异步任务(获取我的json数据的人)里面的方法。

这是我的代码:

String example;

static final String API_URL2 = "https://xxxxxxxx.xx";
//Start Used for spinner with different value and display
String[] textfordropdown = { "A",
        "B",
        "C",
};
String[] valueofdropdowtext =
        { "1",
                "2",
                "3",
        };
Spinner  spinnerdynamic;

OnCreate我已经调用了我的异步任务,看看我是否显示了一些东西,所以它是否有效:

new GetList().execute();

我也在这里创建我的微调器

spinnerdynamic = (Spinner)findViewById(R.id.dynamic_spinner);
ArrayAdapter<String> adapter1 =
      new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, textfordropdown);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerdynamic.setAdapter(adapter1);
spinnerdynamic.setOnItemSelectedListener(onItemSelectedListener1);

这是函数onItemSelectedListener1:

OnItemSelectedListener onItemSelectedListener1 =
        new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                example = String.valueOf(valueofdropdowtext[position]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}

        };

这是我的异步任务:

 class GetList extends AsyncTask<Void, Void, String> {


    protected void onPreExecute() {
    }

    protected String doInBackground(Void... urls) {
        try {
            URL url = new URL(API_URL2);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                StringBuilder stringBuilder = new StringBuilder();
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append(line).append("\n");
                }
                bufferedReader.close();
                return stringBuilder.toString();
            } finally {
                urlConnection.disconnect();
            }
        } catch (Exception e) {
            Log.e("ERROR", e.getMessage(), e);
            return null;
        }
    }

    protected void onPostExecute(String response) {
        if (response == null) {
            response = "Une erreur c'est produite";
        }

        Log.i("INFO", response);

        try {
            JSONObject object = (JSONObject) new JSONTokener(response).nextValue();
            JSONArray prestationlist = object.getJSONArray("WhatIWant");

            //Permet de compter le nombre d'éléments dans le json array
            int arrSize = prestationlist.length();

            ArrayList<String> value = new ArrayList<String>(arrSize);
            ArrayList<String> name = new ArrayList<String>(arrSize);

            for(int i=0;i<arrSize;i++) {
                object = prestationlist.getJSONObject(i);
                value.add(object.getString("Value"));
                name.add(object.getString("Text"));
                //Here i've made some display to see if it works, i get the data.
                responseView.setText(value.toString());
            }

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

那么如何将我的数据放入String [] textfordropdown或valueofdropdowntext?

非常感谢!

1 个答案:

答案 0 :(得分:1)

创建一个字符串数组列表并在文件顶部

初始化它

在你的微调器中使用那个arraylist

adapter1 =
  new ArrayAdapter<String>(MainActivity.this,
         android.R.layout.simple_spinner_item, yourarraylist);

并将您的值添加到onPostExecute方法

中的数组列表中
for(int i=0;i<arrSize;i++) {
            object = prestationlist.getJSONObject(i);
            value.add(object.getString("Value"));
            name.add(object.getString("Text"));
            //Here i've made some display to see if it works, i get the data.
           yourarraylist.add(object.getString("Text"));
            responseView.setText(value.toString());
        }
       adapter1.notifyDataSetChanged();

注意:您还需要在文件顶部初始化微调器适配器