如何使用ArrayList创建自定义微调适配器 >()

问题描述 投票:-1回答:2

我刚刚与arraylist建立了联系,但我想将其展示给Spinner。我已经用recyclerview完成了,但是这次我只想在微调器适配器中显示字符串“ kode”(ITEMDATA0),并从微调器中选择字符串“ kode”之后,其余数据(nama,sp1,sp2等)将显示在微调框下方的edittext中。这是我的表单布局:

My Form Layout

这是我的程序:

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();
}

我必须使用布局xml创建Java适配器类和项目类吗?怎么做 ?非常感谢你

android android-spinner
2个回答
1
投票
1.first of declare the Arraylist string:-  ArrayList<String> label = new ArrayList<String>();

2.second call the API and get data using 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。第三项设置spinnerview setOnItemSelectedListener并在选择时获得标签

-spinnerview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){@Overridepublic 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) {
            }
        });

0
投票

要创建自定义微调器,必须创建一个自定义适配器。因此,您必须为适配器创建一个类,并从ArrayAdapter开始扩展。另外,您还需要为每个微调框项目设置布局,因此您应该为微调框项目创建一个layout。设计布局并制作微调器项目后,必须转到适配器类并重写getview方法。在此方法中,您应该返回视图对象。适配器类的伪代码在下面:

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;
}}

完成本课程后,您可以为您的微调器使用此适配器

© www.soinside.com 2019 - 2024. All rights reserved.