首页 > 解决方案 > 微调器数组列表

问题描述

微调器项目来自 JSON 对象。我已经得到 JSON 对象并放入微调器。但是我在android中的微调器上遇到了这种问题,如果我从微调器选项中选择一只猫并且在我点击后,它不会显示在选定的微调器项目上。请参阅图片以获取示例。有什么问题或者我忘记添加一些代码行。

在此处输入图像描述

更新:图像和 XML 在此处输入图像描述

XML

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tool="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="40dp"
   android:layout_marginTop="5dp"
   android:layout_gravity="center"
   android:orientation="vertical">
    <Spinner
        android:id="@+id/spinnerdropdown"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:padding="3dp"
        android:spinnerMode="dropdown"
        android:textColor="#fff"
        android:layout_centerInParent="true"
        android:background="@drawable/bckg_spinner"/>
</LinearLayout>

JAVA

public class ProfileDept extends AppCompatActivity {

ArrayList<String> ArrayListSpinner = new ArrayList<String>();
Spinner spinner;

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

    Spinner spinner = findViewById(R.id.spinnerdropdown);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ArrayListSpinner);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Log.v("item", (String) parent.getItemAtPosition(position));
            ((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
}
    class fetchData extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... voids) {

            try {
                URL url = new URL("https://Account/api/Profile?TestAccount");
                HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();

                httpsURLConnection.setRequestProperty("Content-Type", "application/json");
                httpsURLConnection.setRequestMethod("GET");   //POST or GET
                httpsURLConnection.connect();

                int statusCode = httpsURLConnection.getResponseCode();
                statusMsg = httpsURLConnection.getResponseMessage();

                if (statusCode == 200) {
                    InputStream it = new BufferedInputStream(httpsURLConnection.getInputStream());
                    InputStreamReader read = new InputStreamReader(it);
                    BufferedReader buff = new BufferedReader(read);
                    StringBuilder dta = new StringBuilder();
                    String chunks;

                    while ((chunks = buff.readLine()) != null) {
                        dta.append(chunks);
                        JSONArray jsonArray = new JSONArray(dta.toString());

                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonpass = (JSONObject) jsonArray.get(i);
                            ArrayListSpinner.add(jsonpass.getString("Username"));
                        }

                    }
                    return dta.toString();

                } else {

                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute (String aVoid) {
            super.onPostExecute(aVoid);
        }

    }
}

标签: androidjson

解决方案


你应该这样做

public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
int index = adapterView.getSelectedItemPosition();
((TextView) spinner.getSelectedView()).setTextColor(getResources().getColor(R.color.Blue)); //<----}

推荐阅读