Android:根据第一个微调器的选择填充第二个微调器
如何做到这一点:我的应用程序中有两个微调器,一个用于科室,另一个用于医生,当我在科室微调器中选择一个科室时,我希望我的第二个微调器仅显示属于该科室的医生。我使用MYSQL数据库从两个不同的表中获取数据。我用URLConction填充这两个表。这是我的密码:Android:根据第一个微调器的选择填充第二个微调器,android,Android,如何做到这一点:我的应用程序中有两个微调器,一个用于科室,另一个用于医生,当我在科室微调器中选择一个科室时,我希望我的第二个微调器仅显示属于该科室的医生。我使用MYSQL数据库从两个不同的表中获取数据。我用URLConction填充这两个表。这是我的密码: int id_departament; String[] nume_doctor = null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup c
int id_departament;
String[] nume_doctor = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.programare_online, container, false);
sp = (Spinner) view.findViewById(R.id.spinner1);
adapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner1_layout, R.id.txt1, listItems);
sp.setAdapter(adapter);
sp.setFocusable(true);
sp.clearFocus();
sp2 = (Spinner) view.findViewById(R.id.spinner2);
adapter2 = new ArrayAdapter<String>(getActivity(), R.layout.spinner2_layout, R.id.txt2, listItems2);
sp2.setAdapter(adapter2);
sp2.setFocusable(true);
sp2.clearFocus();
return view;
}
this is the BlackTask
private class BackTask extends AsyncTask<Void, Void, Void> {
ArrayList<String> list;
ArrayList<String> list2;
protected void onPreExecute() {
super.onPreExecute();
list = new ArrayList<>();
list2 = new ArrayList<>();
}
protected Void doInBackground(Void... params) {
InputStream is = null;
String result = "";
InputStream is2 = null;
String result2 = "";
so here I open the connection with database
try {
URL url = new URL("http://192.168.1.5/clinicco/departament.php");
urlConnection = (HttpURLConnection) url.openConnection(); //here open the connection with database
urlConnection.connect(); //here connect to database
is = urlConnection.getInputStream(); //here open the stream for reading data from database
URL url2 = new URL("http://192.168.1.5/clinicco/doctori.php");
urlConnection2 = (HttpURLConnection) url2.openConnection();
urlConnection2.connect(); //here connect to database
is2 = urlConnection2.getInputStream(); //here open the stream for reading data from database
} catch (IOException e) {
e.printStackTrace();
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
result += line;
}
is.close();
BufferedReader reader2 = new BufferedReader(new InputStreamReader(is2, "utf-8"));
String line2 = null;
while ((line2 = reader2.readLine()) != null) {
result2 += line2;
}
is2.close();
//result=sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
// parse json data
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
//id_departament = jsonObject.getInt("id_departament");
// add departament's name to arraylist
list.add(jsonObject.getString("nume_departament"));
}
JSONArray jArray2 = new JSONArray(result2);
JSONObject jsonObject2=null;
nume_doctor=new String[jArray2.length()];
id_departament = jsonObject2.getInt("id_departament");
for (int i = 0; i < jArray2.length(); i++) {
jsonObject2 = jArray2.getJSONObject(i);
list2.add(jsonObject2.getString("nume_doctor"));
id_departament = jsonObject2.getInt("id_departament");
nume_doctor[i] = jsonObject2.getString("nume_doctor");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
this is onPostExecute with the OnItemselectedLiSTENER
protected void onPostExecute(Void result) {
listItems.addAll(list);
adapter.notifyDataSetChanged();
listItems2.addAll(list2);
adapter2.notifyDataSetChanged();
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
// TODO Auto-generated method stu
// sp.getSelectedItem().toString();
int pos = sp.getSelectedItemPosition();
sp2.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
// sp2.getSelectedItem().toString();
sp.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
public void onStart() {
super.onStart();
BackTask bt = new BackTask();
bt.execute();
}
intid\u部门;
字符串[]nume\u doctor=null;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.programmare_online,container,false);
sp=(微调器)view.findViewById(R.id.spinner1);
adapter=new ArrayAdapter(getActivity(),R.layout.spinner1_layout,R.id.txt1,listItems);
sp.setAdapter(适配器);
sp.setFocusable(真);
sp.clearFocus();
sp2=(微调器)view.findViewById(R.id.spinner2);
adapter2=新阵列适配器(getActivity(),R.layout.spinner2_layout,R.id.txt2,listItems2);
sp2.设置适配器(适配器2);
sp2.setFocusable(真);
sp2.clearFocus();
返回视图;
}
这是一项艰巨的任务
私有类BackTask扩展了AsyncTask{
数组列表;
ArrayList列表2;
受保护的void onPreExecute(){
super.onPreExecute();
列表=新的ArrayList();
list2=新的ArrayList();
}
受保护的Void doInBackground(Void…参数){
InputStream=null;
字符串结果=”;
InputStream为2=null;
字符串result2=“”;
所以这里我打开了与数据库的连接
试一试{
URL=新URL(“http://192.168.1.5/clinicco/departament.php");
urlConnection=(HttpURLConnection)url.openConnection();//在这里打开与数据库的连接
urlConnection.connect();//此处连接到数据库
is=urlConnection.getInputStream();//在这里打开从数据库读取数据的流
URL url2=新URL(“http://192.168.1.5/clinicco/doctori.php");
urlConnection2=(HttpURLConnection)url2.openConnection();
urlConnection2.connect();//此处连接到数据库
is2=urlConnection2.getInputStream();//在这里打开从数据库读取数据的流
}捕获(IOE异常){
e、 printStackTrace();
}
//将响应转换为字符串
试一试{
BufferedReader=新的BufferedReader(新的InputStreamReader(即“utf-8”));
字符串行=null;
而((line=reader.readLine())!=null){
结果+=行;
}
is.close();
BufferedReader Reader 2=新的BufferedReader(新的InputStreamReader(is2,“utf-8”);
字符串line2=null;
而((line2=reader2.readLine())!=null){
结果2+=line2;
}
is2.close();
//结果=sb.toString();
}捕获(例外e){
e、 printStackTrace();
}
//解析json数据
试一试{
JSONArray jArray=新JSONArray(结果);
for(int i=0;i