[英]How to Make ArrayAdapter for Spinner with ArrayList<Hashmap<String, String>>()
i just made a connection with arraylist, but i want to show it into spinner.我刚刚与 arraylist 建立了连接,但我想将它显示到微调器中。 I already done it with recyclerview but this time i want to show only String "kode" (ITEMDATA0) in spinner adapter, and after choosing the String "kode" from spinner, the rest of the data (nama, sp1, sp2, etc) will showing in edittext under the spinner.
我已经用 recyclerview 完成了,但这次我只想在微调适配器中显示字符串“kode”(ITEMDATA0),从微调器中选择字符串“kode”后,其余数据(nama、sp1、sp2等)将显示在微调器下的编辑文本中。 Here is my form layout :
这是我的表单布局:
Here is my program :这是我的程序:
private void showItem(){
list.clear();
JSONObject jsonObject = null;
//ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
try {
jsonObject = new JSONObject(JSON_STRING);
JSONArray result = jsonObject.getJSONArray(Item_Konfigurasi.TAG_JSON_ARRAY);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
final String id = jo.getString(Item_Konfigurasi.TAG_ITEMXCODE);
final String kode = jo.getString(Item_Konfigurasi.TAG_ITEMDATA0);
final String nama = jo.getString(Item_Konfigurasi.TAG_ITEMDATA1);
final String sp1 = jo.getString(Item_Konfigurasi.TAG_ITEMDATA2);
final String sp2 = jo.getString(Item_Konfigurasi.TAG_ITEMDATA3);
final String ket = jo.getString(Item_Konfigurasi.TAG_ITEMDATA4);
HashMap<String,String> item = new HashMap<>();
item.put(Item_Konfigurasi.TAG_ITEMXCODE,id);
item.put(Item_Konfigurasi.TAG_ITEMDATA0,kode);
item.put(Item_Konfigurasi.TAG_ITEMDATA1,nama);
item.put(Item_Konfigurasi.TAG_ITEMDATA2,sp1);
item.put(Item_Konfigurasi.TAG_ITEMDATA3,sp2);
item.put(Item_Konfigurasi.TAG_ITEMDATA4,ket);
list.add(item);
}
//THIS WILL BE THE PLACE FOR CUSTOM SPINNER ADAPTER
/*CustomAdapter mAdapter = new CustomAdapter(Transaksi_Add.this,
R.layout.listspinnertransaksi_layout, R.id.title, list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));
mRecyclerView.setAdapter(mAdapter);*/
} catch (JSONException e) {
e.printStackTrace();
}
}
private void getJSON(){
class GetJSON extends AsyncTask<Void,Void,String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
JSON_STRING = s;
showItem();
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(Item_Konfigurasi.URL_GET_ALL);
return s;
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
Edit Question : How to make ArrayAdapter for Spinner if i Used ArrayList>?编辑问题:如果我使用 ArrayList>,如何为 Spinner 制作 ArrayAdapter? meanwhile i just want 1 of 6 data in array to showing in Spinner (kode/ITEMDATA0)..
同时我只希望数组中的 6 个数据中的 1 个显示在 Spinner (kode/ITEMDATA0) 中。
Edit : oh yeah i used ArrayList>, it must be different from ArrayList, right?编辑:哦,是的,我使用了 ArrayList>,它必须与 ArrayList 不同,对吗?
to create a custom spinner you must create a custom adapter.要创建自定义微调器,您必须创建自定义适配器。
so you must create a class for you'r adapter and extends from ArrayAdapter
.所以你必须为你的适配器创建一个类并从
ArrayAdapter
扩展。
also you need a layout for each item of spinner so you should create a layout
for item of spinner.您还需要为每个微调器项目创建一个
layout
,因此您应该为微调器项目创建一个layout
。
after you design layout and make item of spinner you must go to adapter class and override a getview
method.in this method you should return a view object.在设计布局并制作微调项目后,您必须转到适配器类并覆盖
getview
方法。在此方法中,您应该返回一个视图对象。
pseudo code of adapter class is in below :适配器类的伪代码如下:
public class adapter extends ArrayAdapter<OBJECT MUST BE SHOW IN SPINNER>{
public adapter(@NonNull Context context , @NonNull List objects)
{
super(context, R.layout.YOUR CUSTOM LAYOUT , objects);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(YOUR CUSTOM LAYOUT , null , false);
//you can fill you'r view item according to list of object and position
return convertView;
}}
after you complete this class you can use this adapter for you'r spinner完成本课程后,您可以将此适配器用于微调器
1.first of declare the ArrayList
string:- 1.首先声明
ArrayList
字符串:-
ArrayList<String> label = new ArrayList<String>();
2.second call the API and get data using retrofit
2.second调用API,并使用获得的数据
retrofit
private void list_API() {
label .clear();
callapi().enqueue(new Callback<GetModel>() {
@Override
public void onResponse(Call<GetModel> call, Response<GetModel> response) {
if (response.body().getStatus().equals("error")) {
} else {
List<List> getlistdata = fetchResultsSort(response);
for (int i = 0; i < getlistdata.size(); i++) {
label.add(getlistdata.get(i).getLabel());
Log.e("sort_innerrr_label", "" + getlistdata.get(i).getLabel());
}
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(parent, R.layout.simple_spinner_item, label);
spinnerArrayAdapter.setDropDownViewResource(R.layout.simple_spinner_item); // The drop down view
spinnerview.setAdapter(spinnerArrayAdapter);
}
}
@Override
public void onFailure(Call<GetSortModel> call, Throwable t) {
}
});
}
3.Third set the spinnerview setOnItemSelectedListener
and on select to get label 3.Third设置spinnerview
setOnItemSelectedListener
和on select获取标签
spinnerview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
int selected_item_position = spinnerview.getSelectedItemPosition();
String selected = label.get(selected_item_position);
Log.e("selected_name", "" +selected);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.