[英]Spinner select Item in android studio [ using parse ]
I have problem using Spinner in android studio I retreive my data from Parse.com and after that I take the array of list and use it as spinner , It was working but the problem when I try to get the value the user will select I try many codes that use [ onItemSelectedListner method ] when I press on the Item in the spinner It will not selected and doesn't appear as selected 我在android studio中使用Spinner时遇到问题,我从Parse.com中检索了数据,然后我将列表数组用作Spinner,它正在工作,但是当我尝试获取用户选择的值时出现问题,我尝试当我在微调器中的项目上按下时,许多使用[onItemSelectedListner方法]的代码将不会被选中,也不会显示为选中状态
so in the end I used this code which view the spinner ITEM inside the Query.findInTheBackground , however the problem that I want to get the value that user selected but I cant !! 所以最后我使用了这段代码,该代码查看了Query.findInTheBackground内部的微调项,但是我想获得用户选择的值但我做不到的问题! what should I do ?
我该怎么办 ?
and there is one more problem , when the spinner is shown the list is white so how can I change the color ?! 还有一个问题,当显示微调框时,列表为白色,那么如何更改颜色?
this is my code 这是我的代码
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);
}
}
});
}
} }
I would suggest looking into ParseQueryAdapter. 我建议调查ParseQueryAdapter。 What I'll do is:
我要做的是:
AppointmentDetailSelection: 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: 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.