我正在更新android片段中的表单。我在其中有微调器,我想在微调器中显示默认选择的值,但它不起作用。我已经搜索了很多,但没有用。在函数TruckApi.getTruckCapacity(“ update_truck”);中我已将数据添加到truckCapacityList中,并显示在微调器中,但我无法基于truck_capacity_id预先选择微调器项目。所有解决方案都说明了获取位置。任何帮助将不胜感激。 这是我的片段代码。
package com.example.narmail.truckApp.Api.Fragments;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.InputFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import com.example.narmail.truck30mint.Api.Api.TruckApi;
import com.example.narmail.truck30mint.Api.models.TruckModels.TruckBodyLength;
import com.example.narmail.truck30mint.Api.models.TruckModels.TruckBodyType;
import com.example.narmail.truck30mint.Api.models.TruckModels.TruckCapacity;
import com.example.narmail.truck30mint.R;
import java.util.ArrayList;
import java.util.List;
public class UpdateTruckFragment1 extends Fragment {
Context context;
Spinner truckCapacitySpinner;
int truck_capacity_id = 0;
public static List<TruckCapacity> truckCapacityList = new ArrayList<>();
public UpdateTruckFragment1() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_update_truck, container, false);
truck_capacity_id = getArguments().getInt("truck_capacity_id");
context = getActivity() ;
/*truck capacity spinner*/
if (truckCapacityList.size() < 1) {
truckCapacityList.add(new TruckCapacity(0, "Select Capacity"));
TruckApi.getTruckCapacity("update_truck");
}
truckCapacitySpinner = rootView.findViewById(R.id.truckCapacitySpinner);
ArrayAdapter<TruckCapacity> capacityArrayAdapter = new ArrayAdapter<TruckCapacity>(context,R.layout.support_simple_spinner_dropdown_item, truckCapacityList);
capacityArrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
truckCapacitySpinner.setAdapter(capacityArrayAdapter);
// truckCapacitySpinner.setSelection(capacityArrayAdapter.getPosition(truck_capacity_id));
truckCapacitySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TruckCapacity truckCapacity = (TruckCapacity) parent.getSelectedItem();
truck_capacity_id = truckCapacity.getId();
System.out.println("on selected id is "+truckCapacity.getId()+ " and value is "+truckCapacity.getName());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return rootView;
}
}
truck capacity has data like this
[
{
"id": 1,
"name": "500KG"
},
{
"id": 2,
"name": "1 TON"
},
{
"id": 3,
"name": "2 TON"
},
{
"id": 4,
"name": "3 TON"
},
{
"id": 5,
"name": "5 TON"
},
{
"id": 6,
"name": "7 TON"
},
{
"id": 7,
"name": "8 TON"
},
{
"id": 8,
"name": "9 TON"
},
{
"id": 9,
"name": "10 TON"
},
{
"id": 10,
"name": "12 TON"
},
{
"id": 11,
"name": "15 TON"
},
{
"id": 12,
"name": "18 TON"
},
{
"id": 13,
"name": "20 TON"
},
{
"id": 14,
"name": "22 TON"
},
{
"id": 15,
"name": "24 TON"
},
{
"id": 16,
"name": "28 TON"
},
{
"id": 17,
"name": "30 TON"
},
{
"id": 18,
"name": "32 TON"
},
{
"id": 19,
"name": "35 TON"
}
],
这是TruckApi.getTruckCapacity(“ update_truck”)函数
public static void getTruckCapacity(final String fragmentName){
apiInterface = RetrofitApi.getRetrofit().create(TruckInterface.class);
String token = User.getToken();
System.out.println(" my token is "+token);
Call<List<TruckCapacity>> call = apiInterface.getTruckCapacity(token);
call.enqueue(new Callback<List<TruckCapacity>>() {
@Override
public void onResponse(Call<List<TruckCapacity>> call, Response<List<TruckCapacity>> response) {
Log.w("response is ",new GsonBuilder().setPrettyPrinting().create().toJson(response));
if (response.isSuccessful()){
Log.w("response body is ",new GsonBuilder().setPrettyPrinting().create().toJson(response.body()));
List<TruckCapacity> list = response.body();
for (int i = 0; i<list.size(); i++) {
System.out.println("i am in for loop ");
System.out.println("raw respone is "+response.raw());
if (fragmentName.equalsIgnoreCase("add_truck")) {
AddTruckFragment.truckCapacityList.add(new TruckCapacity(list.get(i).getId(),list.get(i).getName()));
}else if(fragmentName.equalsIgnoreCase("update_truck")){
UpdateTruckFragment.truckCapacityList.add(new TruckCapacity(list.get(i).getId(),list.get(i).getName()));
}
}
// System.out.println("truckCapacityList items are "+AddTruckFragment.truckCapacityList.get(2).getId());
}else{
System.out.println("error in response");
}
}
@Override
public void onFailure(Call<List<TruckCapacity>> call, Throwable t) {
System.out.println("error in getTruckCapacity is"+t);
}
});
}
这是卡车的容量等级
package com.example.narmail.truck30mint.Api.models.TruckModels;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class TruckCapacity {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
/**
* No args constructor for use in serialization
*
*/
public TruckCapacity() {
}
/**
*
* @param id
* @param name
*/
public TruckCapacity(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//to display object as a string in spinner
@Override
public String toString() {
return name;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof TruckCapacity){
TruckCapacity c = (TruckCapacity )obj;
if(c.getName().equals(name) && c.getId()==id ) return true;
}
return false;
}
}
答案 0 :(得分:0)
有一种简单而神奇的方法可以在微调器中显示自定义对象模型
public class TruckCapacity {
...
@Override
public String toString() {
return whateverYouWantToDisplay;
}
}
一种获取所选项目的方法
TruckCapacity truckCapacity = (TruckCapacity) parent.getItemAtPosition(position);
@编辑
例如,您可以搜索微调器列表以找到搜索到的ID,循环迭代等效于数组位置,然后以编程方式设置选择
public int findSpinnerPosition(List<TruckCapacity> truckCapacityList, int searchId) {
int position = -1;
for (int i = 0; i < truckCapacityList.size(); i++) {
if (truckCapacityList.get(i).getId() == searchId) {
position = i;
break;
}
}
return position;
}
然后选择
int position = findSpinnerPosition(truckCapacityList, 7);
if (position != -1) {
truckCapacitySpinner.setSelection();
} else {
//item with this id doesn't exist
}