我在android中创建了动态微调器,该动态微调器显示了来自网址的json
数据,我在微调器中正确设置了数据,但我得到了所选项目的ID
我该怎么做
我设置微调器值的代码是:
private void setSpinData(){
if (isNetworkAvailable()) {
AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(800000);
final RequestParams params = new RequestParams();
client.get("Here is url", new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode,
cz.msebera.android.httpclient.Header[]
headers, String responseString,
Throwable throwable) {
Toast.makeText(SpinnerActivity.this, responseString,
Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int statusCode,
cz.msebera.android.httpclient.Header[]
headers, String responseString) {
if (responseString != null) {
try {
JSONObject response = new
JSONObject(responseString);
Log.e("responseString",""+responseString);
String status = response.getString("status");
if (status.equals("success")) {
JSONArray jsonArray =
response.getJSONArray("car_rate");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject
jsonObject1=jsonArray.getJSONObject(i);
String type = jsonObject1.getString("type");
btclist1.add(type);
cartype_id= jsonObject1.getString("cartype_id");
}
}
} catch (Exception e) {
e.printStackTrace();
}
spin_value.setAdapter(new ArrayAdapter<String>(SpinnerActivity.this, android.R.layout.simple_spinner_dropdown_item, btclist1));
} else {
Toast.makeText(SpinnerActivity.this, "data is not available!", Toast.LENGTH_SHORT).show();
}
}
});
}else {
Toast.makeText(SpinnerActivity.this, "No Internet connection",
Toast.LENGTH_SHORT).show();
}
}
我的json
答复是:
{
"status": "success",
"car_rate": [
{
"cartype_id": "5",
"type": "SUV PerMiles",
"rate_type": "1",
"rate_in": "12"
},
{
"cartype_id": "6",
"type": "SUV Hourly",
"rate_type": "0",
"rate_in": "50"
}
]
}
如何获得cartype_id
的选定微调器项目?
答案 0 :(得分:1)
像这样创建 MyModel
public class MyModel
{
String cartype_id,type,rate_type,rate_in;
public String getCartype_id() {
return cartype_id;
}
public void setCartype_id(String cartype_id) {
this.cartype_id = cartype_id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getRate_type() {
return rate_type;
}
public void setRate_type(String rate_type) {
this.rate_type = rate_type;
}
public String getRate_in() {
return rate_in;
}
public void setRate_in(String rate_in) {
this.rate_in = rate_in;
}
}
比创建模型类的
ArrayList
ArrayList<MyModel> arrayList = new ArrayList<>();
像这样解析您的json
try {
JSONArray jsonArray =
response.getJSONArray("car_rate");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
MyModel myModel= new MyModel();
myModel.setCartype_id(obj.getString("cartype_id"));
myModel.setType(obj.getString("type"));
myModel.setRate_in(obj.getString("rate_type"));
myModel.setRate_in(obj.getString("rate_in"));
arrayList.add(myModel);
}
} catch (JSONException e) {
e.printStackTrace();
}
现在创建这样的自定义适配器
class SpinnerAdapter extends ArrayAdapter<MyModel> {
private ArrayList<MyModel> myarrayList;
SpinnerAdapter(Context context, int textViewResourceId, ArrayList<MyModel> modelArrayList) {
super(context, textViewResourceId, modelArrayList);
this.myarrayList = modelArrayList;
}
@Override
public View getDropDownView(int position, View convertView, @NonNull ViewGroup parent) {
return getCustomView(position, parent);
}
@Nullable
@Override
public MyModel getItem(int position) {
return myarrayList.get(position);
}
@Override
public int getCount() {
int count = myarrayList.size();
//return count > 0 ? count - 1 : count;
return count;
}
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
return getCustomView(position, parent);
}
private View getCustomView(int position, ViewGroup parent) {
MyModel model = getItem(position);
View spinnerRow = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
TextView label = spinnerRow.findViewById(android.R.id.text1);
label.setText(String.format("%s", model != null ? model.getType() : ""));
return spinnerRow;
}
}
现在像这样在旋转器中设置适配器
SpinnerAdapter adapter = new SpinnerAdapter(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, arrayList);
mySpinner.setAdapter(adapter);
现在像这样从微调器中获取选定的项目
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
MyModel myModel=(MyModel) parent.getSelectedItem();
Log.e("DATA",myModel.getCartype_id());
Log.e("DATA",myModel.getRate_in());
Log.e("DATA",myModel.getRate_type());
Log.e("DATA",myModel.getType());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
示例代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
JSONArray jsonArray =
response.getJSONArray("car_rate");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
MyModel myModel= new MyModel();
myModel.setCartype_id(obj.getString("cartype_id"));
myModel.setType(obj.getString("type"));
myModel.setRate_in(obj.getString("rate_type"));
myModel.setRate_in(obj.getString("rate_in"));
arrayList.add(myModel);
}
SpinnerAdapter adapter = new SpinnerAdapter(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, arrayList);
mySpinner.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
MyModel myModel=(MyModel) parent.getSelectedItem();
Log.e("DATA",myModel.getCartype_id());
Log.e("DATA",myModel.getRate_in());
Log.e("DATA",myModel.getRate_type());
Log.e("DATA",myModel.getType());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}