Skip to content Skip to sidebar Skip to footer

Spinner Item Is Not Visible (data Is Coming From Server) Android

here i save data in Array listthat is semesterlist :- JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, 'http://www.mytat.in/API/semester?tokenno

Solution 1:

try change getActivity() to getBaseContext()

ArrayAdapter<String> studentsem= newArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item,semesterlist);
studentsem.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

i try it with your adapter and it worked.

Solution 2:

trythispublicclassMainActivityextendsAppCompatActivity {

    publicSpinner spinner;
    publicArrayList<SpinnerItems> arr_spiner;
    publicSpinnerItems  spinnerItem;
    publicSpinnerAdapterClass adapter;
    publicString sSelectedItem;
    publicTextView txtSpinnerValue;
    publicstaticString sTestData ;
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        arr_spiner = newArrayList<SpinnerItems>();
        getData("http://www.mytat.in/API/semester?tokenno=xvub4jey66c7ec6tb4j7g83ghjls353");
        initailazation();
        spinnerItem = newSpinnerItems();
        spinnerItem.setId(null);
        spinnerItem.setName("Select Semester");
        arr_spiner.add(0, spinnerItem);
        adapter =newSpinnerAdapterClass(MainActivity.this ,arr_spiner);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
            @OverridepublicvoidonItemSelected(AdapterView<?> parent, View view, int position, long id) {
                sSelectedItem = arr_spiner.get(spinner.getSelectedItemPosition())
                        .getName();
                txtSpinnerValue.setText(sSelectedItem);
            }
            @OverridepublicvoidonNothingSelected(AdapterView<?> parent) {
            }
        });
    }
    privatevoidinitailazation() {
        spinner=(Spinner)findViewById(R.id.sp_terminal);
        txtSpinnerValue=(TextView)findViewById(R.id.txt_serSpinnerValue);
    }
    publicvoidgetData(String s){
        StringRequest stringRequest =newStringRequest(Request.Method.GET, s, newResponse.Listener<String>() {
            @OverridepublicvoidonResponse(String response) {
                try {
                    JSONObject jsonObject = newJSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("output");
                    for(int i=0 ; jsonArray.length()>0; i++){
                        JSONObject data = jsonArray.getJSONObject(i);
                        String semester = data.getString("semester");
                        String id = data.getString("id");
                        spinnerItem = newSpinnerItems();
                        spinnerItem.setId(id);
                        spinnerItem.setName(semester);
                        arr_spiner.add(spinnerItem);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, newResponse.ErrorListener() {
            @OverridepublicvoidonErrorResponse(VolleyError error) {
            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }}

MainLayout file 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="nfcdemo.equesttect.com.spinnerdemo.MainActivity"><Spinnerandroid:id="@+id/sp_terminal"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_centerHorizontal="true" /><TextViewandroid:id="@+id/txt_serSpinnerValue"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_centerInParent="true"android:gravity="center"android:textColor="@color/black" /></RelativeLayout>SpinnerItem file 

package nfcdemo.equesttect.com.spinnerdemo;

publicclassSpinnerItems {

    privateString id = null;
    privateString name = null;

    publicStringgetId() {
        return id;
    }

    publicvoidsetId(String id) {
        this.id = id;
    }

    publicStringgetName() {
        return name;
    }

    publicvoidsetName(String name) {
        this.name = name;
    }

}


SpinnerAdapterFilepublicclassSpinnerAdapterClassextendsBaseAdapter {

    ArrayList<SpinnerItems> item_list;
    Context mContext;
    ArrayList<String> arrayList_string;
    publicSpinnerAdapterClass(Context context,
                               ArrayList<SpinnerItems> item_list) {
        super();
        this.item_list = item_list;
        this.mContext = context;
    }
    @Overridepublic int getCount() {
        return item_list.size();
    }
    @OverridepublicObjectgetItem(int position) {
        return item_list.get(position);
    }
    @Overridepublic long getItemId(int position) {
        return position;
    }
    privateclassViewHolder {
        TextView txt_name;
    }
    @OverridepublicViewgetView(int position, View convertView, ViewGroup container) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            // if it's not recycled, initialize some attributesLayoutInflater mInflater = (LayoutInflater) mContext
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_raw,
                    container, false);
            viewHolder = newViewHolder();
            viewHolder.txt_name = (TextView) convertView
                    .findViewById(R.id.txt_name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.txt_name.setText("" + item_list.get(position).getName());
        return convertView;
    }
}

row file 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_vertical"android:orientation="vertical" ><TextViewandroid:id="@+id/txt_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"android:padding="10dp"android:textAllCaps="true"android:textAppearance="?android:attr/textAppearanceSmall"android:textColor="@color/black" /></LinearLayout><uses-permissionandroid:name="android.permission.INTERNET" />Hope its work 

Solution 3:

I try to run your code and I had same problem because the background color of my activity was black so as soon as I change the color of the Activity to white it worked perfectly. So try to change the background color of your Activity/Fragment Background any color other than black.

Post a Comment for "Spinner Item Is Not Visible (data Is Coming From Server) Android"