gpt4 book ai didi

android - 微调器的 Onitemclicklistner 如何将 id 传递给 android 中的下一个微调器

转载 作者:行者123 更新时间:2023-11-29 16:28:57 24 4
gpt4 key购买 nike

您好,下面的代码描述了微调器。在微调器中,我显示不同类型的建筑物名称以调用 Api。根据响应,我将建筑物的名称设置为微调器。

来自回复:身份证和姓名

现在,我的问题是用户何时从微调器中选择了任何建筑物名称。我想从微调器中获取 ID,然后将其传递给下一个微调器。

谁能帮我做一下。

   private void selectBuilding() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Write code for your refresh logic

progressDialog = new ProgressDialog (getActivity ());
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Communicating...");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setCancelable(false);
progressDialog.show();
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API.URL_BASE)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client (client)
.build();
API service = retrofit.create (API.class);
final Call<Managebuilding> userCall = service.getbuildinglist ();
userCall.enqueue(new Callback<Managebuilding> () {

@Override
public void onResponse(Call <Managebuilding> call, Response <Managebuilding> response) {

if (response != null && response.code ( ) == 200 && response.isSuccessful ( )) {
Managebuilding building = response.body ();
String Name = null,Id;
String Lists=new Gson ( ).toJson (response.body ());
// JSONArray jsonArray=new JSONArray ();
// JSONObject jsonObject = null;
try {
JSONObject jsonObject = new JSONObject (Lists);
JSONArray result = jsonObject.getJSONArray ("list");
System.out.println (result);
//JSONArray result = jsonArray.getJSONArray ("ID");

arrayList = new ArrayList <> ( );
arrayList1 = new ArrayList <> ( );
for (int i = 0; i <result.length(); i++) {

Id=result.getJSONObject (i).getString ("ID");

Name=result.getJSONObject (i).getString ("Name");

arrayList.add (Name);
arrayList1.add (Id);
}

ArrayAdapter <String> adapter = new ArrayAdapter <String> (getContext (),android.R.layout.simple_spinner_item, arrayList);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);

} catch (JSONException e) {
e.printStackTrace ( );
}


progressDialog.dismiss ( );

} else {
progressDialog.dismiss ( );
Log.d ("Response errorBody", String.valueOf (response.errorBody ( )));
}

}


@Override
public void onFailure(Call<Managebuilding> call, Throwable t) {
// lv.setAdapter (adapter);
System.out.println("onFailure");
System.out.println(t.fillInStackTrace());
progressDialog.dismiss();
Toast.makeText(getActivity (), "Some error occurred -> ", Toast.LENGTH_LONG).show();;
// progressDialog.dismiss();



}

});
}


}, 5000);
return ;
}

OnitemclickListner:

  spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener () {

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
int pos=spinner.getSelectedItemPosition ();

String ids= String.valueOf (arrayList1.get (pos));

//String obj;

System.out.println (ids);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

}
});

}

最佳答案

首先创建一个模型类,如Building

public class Building{
private String id;
private String name:

public Building(String id, String name){
this.id = id;
this.name = name;
}

public String getId(){
return id;
}
public String getName(){
return name;
}
}

然后在您的 Activity 中创建一个ArrayList类型的Building,如下所示

// declear it in global scope
ArrayList<Building> buildingList;

// and initialize it in `onCreate`
buildingList = new ArrayList <> ();

并在您的 API 响应中使用此 arraylist,如下所示。

userCall.enqueue(new Callback<Managebuilding> () {

@Override
public void onResponse(Call <Managebuilding> call, Response <Managebuilding> response) {

if (response != null && response.code ( ) == 200 && response.isSuccessful ( )) {
Managebuilding building = response.body ();

String Lists=new Gson ( ).toJson (response.body ());
// JSONArray jsonArray=new JSONArray ();
// JSONObject jsonObject = null;
try {
JSONObject jsonObject = new JSONObject (Lists);
JSONArray result = jsonObject.getJSONArray ("list");
System.out.println (result);
//JSONArray result = jsonArray.getJSONArray ("ID");

arrayList = new ArrayList <> ();

for (int i = 0; i <result.length(); i++) {

String id=result.getJSONObject (i).getString ("ID");
String name =result.getJSONObject (i).getString ("Name");

// here you have create object of building using id and name
Building building = new Building(id, name)
buildingList.add(building);

arrayList.add(name);

}

ArrayAdapter <String> adapter = new ArrayAdapter <String> (getContext (),android.R.layout.simple_spinner_item, arrayList);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);

}catch (JSONException e) {
e.printStackTrace ( );
}


progressDialog.dismiss ( );
} else {
progressDialog.dismiss ( );
Log.d ("Response errorBody", String.valueOf (response.errorBody ( )));
}

}


@Override
public void onFailure(Call<Managebuilding> call, Throwable t) {
// lv.setAdapter (adapter);
System.out.println("onFailure");
System.out.println(t.fillInStackTrace());
progressDialog.dismiss();
Toast.makeText(getActivity (), "Some error occurred -> ", Toast.LENGTH_LONG).show();;
// progressDialog.dismiss();



}

});
}

现在您必须修改您的 setOnItemSelectedListener,如下所示。

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener () {

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// here you can retrive you building info by positon
Building building = buildingList.get(position)

String id = building.getId();
String name = building.getName();

// Now you can use your id or name to do whatever you want.
System.out.println (id);
System.out.println (name);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

}
});

}

关于android - 微调器的 Onitemclicklistner 如何将 id 传递给 android 中的下一个微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58004356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com