微调器阵列列表

时间:2018-10-11 04:40:59

标签: android json

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

enter image description here

已更新:图像和XML enter image description here

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);
        }

    }
}

2 个答案:

答案 0 :(得分:0)

当选择项目时,您正在将文本颜色设置为白色。因此会显示项目,但由于您的背景和字体颜色为白色,因此不会显示该项目。将其更改为其他任何颜色并进行检查。

@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.GREEN);   // set to green 
        }

答案 1 :(得分:0)

您应该这样做

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