Spinner在android studio中选择Item [使用解析]

时间:2015-10-01 07:44:53

标签: java android-studio parse-platform onclicklistener android-spinner

我在android studio中使用Spinner时遇到问题 我从Parse.com中检索了我的数据,之后我拿了列表数组并将其用作微调器,它正在工作,但是当我尝试获取用户将选择的值时,问题我尝试了许多使用[onItemSelectedListner方法]的代码当我在旋转器中按下项时它将不会被选中并且不会显示为选中

所以最后我使用了这个代码来查看Query.findInTheBackground中的微调器ITEM,但问题是我想获得用户选择的值,但我不能!我该怎么办 ?

还有一个问题,当显示微调器时,列表是白色的,那么如何更改颜色?!

这是我的代码

public class AppointmentDetailSelection extends Activity {

protected Spinner aSpinner;
protected  ArrayList<String> nameList =new ArrayList<>();

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

    aSpinner = (Spinner)findViewById(R.id.spinner);

    getDepartmentList();

}

public void getDepartmentList (){
    ParseQuery<ParseObject> query = ParseQuery.getQuery("Doctors");
    query.selectKeys(Arrays.asList("Department"));
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> list, com.parse.ParseException e) {
            if (list == null) {
                Log.d("request failed.");
            } else {
                for (ParseObject comment : list) {
                    nameList.add(comment.getString("Department"));
                    Log.d("appointDate", "retrieved a related post");
                }
                ArrayAdapter adap = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_list_item_1,nameList);
                aSpinner.setAdapter(adap);
            }
        }
    });
}

}

1 个答案:

答案 0 :(得分:0)

我建议调查ParseQueryAdapter。我要做的是:

AppointmentDetailSelection:

public class AppointmentDetailSelection extends Activity {

protected Spinner aSpinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_appointment_detail_selection);
    aSpinner = (Spinner)findViewById(R.id.spinner);
    DoctorsAdapter adapter = new DoctorsAdapter(this);
    aSpinner.setAdapter(adapter);
    aSpinner.setSelection(0);

}

private class DoctorsAdapter extends ParseQueryAdapter {

        public DoctorsAdapter(final Context context) {
            super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {

                public ParseQuery create() {
                    ParseQuery query = new ParseQuery("Doctors");
                    query.selectKeys(Arrays.asList("Department"));
                    return query;
                }
            });
        }

        public class ViewHolder {
            TextView textView1;
            TextView textView2;
        }

        @Override
        public View getItemView(ParseObject object, View v, ViewGroup parent) {
            ViewHolder holder;
            if (v == null) {
                v = View.inflate(getContext(), R.layout.your_custom_layout, null);
                holder = new ViewHolder();
                holder.textView1 = (TextView)v.findViewById(R.id.textView1);
                holder.textView2 = (TextView)v.findViewById(R.id.textView2);
                v.setTag(holder);
            }
            else
                holder = (ViewHolder)v.getTag();

            holder.textView1.setText(object.getString("field_from_db1"));
            holder.textView2.setText(object.getString("field_from_db2"));

            return v;
        }
    }
}

your_custom_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/black"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_gravity="center"
        android:textSize="18sp"
        android:id="@+id/textView1" />

    <TextView
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_gravity="center"
        android:textSize="18sp"
        android:id="@+id/textView2" />

</LinearLayout>