我想从服务器访问字符串列表。 以下是我的代码。当我在第二个微调器显示提取中选择第一个微调器值时 从服务器。当我在输出中选择任何旋转器的值时,我希望由服务器发送值 请解释一下。
import android.os.Bundle;
import android.app.Activity;
import android.widget.Spinner;
import android.widget.Toast;
public class Library11 extends Activity implements OnItemSelectedListener{
/** Called when the activity is first created. */
Spinner sp;
Spinner sp1;
Spinner sp2;
Spinner sp3;
Spinner sp4;
Spinner sp5;
String[] menu;
ArrayAdapter<String> adp2;
ArrayAdapter<String> adp1;
ArrayAdapter<String> adp;
ArrayAdapter<String> adp3;
ArrayAdapter<String> adp4;
ArrayAdapter<String> adp5;
ArrayAdapter<String> adp6;
ArrayAdapter<String> adp7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_library11);
sp=(Spinner)findViewById(R.id.Spinner01);
sp1=(Spinner)findViewById(R.id.spinner1);
menu= new String[]{"Select Subject","Engineering Physics-I","Engineering Chemistry","Engineering", "Mathematics-I","Communicative English","Fundamentals of Computer Programming"
};
}
在上部,微调器调用该值,然后我为第二个微调器使用数组适配器,但在这里我想访问Web服务器值。如果可以编辑或者我必须更改某些内容,请告诉我。
String[] Author=new String[]{"Author"};
String[] Physics=new String[]{"H.c. Jain(320)"};
String[] Chemistry=new String[]{"Yasmin Ali(180)"};
String[] Maths=new String[]{"Gokhru(200)"};
String[] English=new String[]{"Jain & Jain(200)"};
String[] Mechanics=new String[]{"Jaz(320)"};
String[] Programming=new String[]{"Jaz(320)"};
adp= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
adp1= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Author);
adp2= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Physics);
adp3= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Chemistry);
adp4=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Maths);
adp5=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,English);
adp6=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Mechanics);
adp7=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Programming);
sp.setAdapter(adp);
sp.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
menu[0]="Select Subject";
Toast.makeText(getApplicationContext(), "Selected the book & Author available", 1500).show();
if(sp.getSelectedItemPosition()==0)
sp1.setAdapter(adp1);
if(sp.getSelectedItemPosition()==1){
sp1.setAdapter(adp2);
}
if(sp.getSelectedItemPosition()==2){
sp1.setAdapter(adp3);
}
if(sp.getSelectedItemPosition()==3){
sp1.setAdapter(adp4);
}
if(sp.getSelectedItemPosition()==4){
sp1.setAdapter(adp5);
}
if(sp.getSelectedItemPosition()==5){
sp1.setAdapter(adp6);
}
if(sp.getSelectedItemPosition()==5){
sp1.setAdapter(adp7);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// sp.setAdapter(adp3);
}
}
答案 0 :(得分:0)
这是我的代码。它有点类似于你的。我的代码从Facebook API中检索Facebook好友,并将它们添加到spinner
。
public class Friends extends AsyncTask<String, Void, Void>{
@Override
protected Void doInBackground(String... params) {
try{
String fql = "SELECT uid, name FROM user WHERE is_app_user AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me())";
Bundle parameters = new Bundle();
parameters.putString("query", fql);
parameters.putString("method", "fql.query");
parameters.putString("access_token", fb.getAccessToken());
String Response = fb.request(parameters);
JSONArray json = new JSONArray(Response);
list = new ArrayList<String[][]>();
String[][] friendsToList;
SpinnerList = new ArrayList<String>();
String TempToSpinnerList=new String();
for(int i = 0; i < json.length(); i++){
friendsToList = new String[1][2];
friendsToList[0][0] = json.getJSONObject(i).getString("uid");
friendsToList[0][1] = json.getJSONObject(i).getString("name");
TempToSpinnerList= friendsToList[0][1];
list.add(i,friendsToList);
SpinnerList.add(TempToSpinnerList);
}
return null;
}catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FacebookError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void v)
{
dataAdapter = new ArrayAdapter<CharSequence>(getApplicationContext(),android.R.layout.simple_spinner_item);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Iterator<String> it = SpinnerList.iterator();
while(it.hasNext()){
String ve = (String) it.next();
dataAdapter.add(ve);
}
spinner.setAdapter(dataAdapter);
Log.e("Hh","Done");
}
}
您可以使用连接到服务器套接字的类似AsyncTask来获取所需的数据。