我如何获取json数据并在微调框中显示? 我想在微调器中显示状态名称和代码 这是我的活动
下面是我正在获得州名和州代码的邮递员图像 所以现在我如何获得并在微调器中显示 请帮助我
String country_id = "in";
Call<ResponseBody> call = SignupClient
.getInstance()
.getApi()
.users(country_id);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(SignupActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
这是我的Java类
public class SignupClient {
private static final String BASE_URL = "http://74.207.233.160/";
private static SignupClient mInstance;
private Retrofit retrofit;
private SignupClient() {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static synchronized SignupClient getInstance() {
if (mInstance == null)
{
mInstance = new SignupClient();
}
return mInstance;
}
public SignupApi getApi() {
return retrofit.create(SignupApi.class);
}
}
这是我的Api
public interface SignupApi {
@FormUrlEncoded
@POST("find_states")
Call<ResponseBody> users(
@Field("country_id") String country_id
);
}
答案 0 :(得分:0)
尝试以下操作:(用您的Pojo类名称替换“ FilterTable”)
class CustomArrayAdapterForSpinner(private val filterValues: List<FilterTable>) : BaseAdapter() {
private val inflater: LayoutInflater
var name: String? = null
val count: Int
@Override
get() = filterValues.size()
init {
inflater = getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
}
@Override
fun getItem(position: Int): Object {
return filterValues[position]
}
@Override
fun getItemId(position: Int): Long {
return position.toLong()
}
@Override
fun getView(position: Int, view: View, parent: ViewGroup): View {
val holder: Holder
var convertView = view
if (convertView !=null) {
holder = convertView.getTag()
} else {
holder = Holder()
convertView = inflater.inflate(R.layout.row_filter, null)
holder.chkTextView = convertView.findViewById(R.id.checkTxt)
convertView.setTag(holder)
}
holder.chkTextView!!.setText(filterValues[position].getName())
holder.chkTextView!!.setTag(if (filterValues[position].isSelection()) "unChecked" else "checked")
return convertView
}
fun setSelection(str: String) {
for (model in filterValues)
model.setSelection(model.getName().equals(str))
}
internal inner class Holder {
var chkTextView: CheckedTextView? = null
}
}