0

i just made a connection with arraylist, but i want to show it into spinner. 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. Here is my form layout :

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>? meanwhile i just want 1 of 6 data in array to showing in Spinner (kode/ITEMDATA0)..

Edit : oh yeah i used ArrayList>, it must be different from ArrayList, right?

Dioo B
  • 82
  • 9
  • Please follow this query https://stackoverflow.com/questions/16694786/how-to-customize-a-spinner-in-android – Prathik Mar 05 '20 at 04:24
  • @Dioo B [Here](https://github.com/MdFarhanRaja/SearchableSpinner) is Searchable spinner third party library, you can use it in `AutoCompleteTextView` – Shreeya Chhatrala Mar 05 '20 at 04:31
  • @Prathik sorry if i'm new here but i need more spesific answer to showing only the "kode" (ITEMDATA0) to show in the spinner, meanwhile i have 5 more data.. – Dioo B Mar 05 '20 at 04:32
  • @AndroidPlayer cool, i'll try it later, after i done with this – Dioo B Mar 05 '20 at 04:48

2 Answers2

1

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.
also you need a layout for each item of spinner so you should create a layout for item of spinner.
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.
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

Sina Soheili
  • 121
  • 1
  • 1
  • 11
  • i'm sorry, i apologize, i already edited my question .. is there any short way to make ArrayAdapter for Spinner if i used ArrayList> ..? – Dioo B Mar 05 '20 at 06:30
  • you'r welcome.test this code : "spinner.setAdapter(new ArrayAdapter() { NonNull Override public View getView(int position, View convertView, ViewGroup parent) { return super.getView(position, convertView, parent); } } );"i don't tried this code but i think work – Sina Soheili Mar 05 '20 at 07:25
0

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.Third set the spinnerview setOnItemSelectedListener and on select to get label

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) {
            }
        });
Shreeya Chhatrala
  • 1,441
  • 18
  • 33
  • that is looks good.. i don't know about retrofit, i just make it in my activity, but one question... how to make ArrayAdapter if i use ArrayList> .. in yours it's just ArrayList.. – Dioo B Mar 05 '20 at 06:21
  • you are just create different different Arraylist like ArrayList id = new ArrayList(); ArrayList kode = new ArrayList(); ArrayList nama = new ArrayList(); ect... and Add the data in for loop and you can use all data – Vinod Prajapati Mar 05 '20 at 07:39
  • but how? cuz i must using arraylist hashmap to get data from my other activity that connected to my database mysql right..? or am i must change the hashmap or just adding more arraylist just to put it into spinner? – Dioo B Mar 05 '20 at 08:49
  • you can use the model class ArrayList arraylist = new ArrayList(); – Vinod Prajapati Mar 05 '20 at 09:05