Gung Eka
Gung Eka

Reputation: 33

Spinner View Displaying Blank Data

I get throuble for view my Spinner Data in XML. The Toast can show data in selected item, but in spinner view can't show all item.

My code Java

public class LoadCategoryTask extends AsyncTask<Void, Void, Boolean> {
    LoadCategoryTask() {
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            return false;
        }

        List<NameValuePair> data = new ArrayList<NameValuePair>();
        JSONObject json = jParser.makeHttpRequest(url_get_category, "GET", data);

        Log.d("All Category: ", json.toString());

        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                category = json.getJSONArray(TAG_CATEGORY);

                for (int i = 0; i < category.length(); i++) {
                    JSONObject c = category.getJSONObject(i);

                    String id = c.getString(TAG_ID_CATEGORY);
                    String category = c.getString(TAG_CATEGORY);

                    HashMap<String, String> map = new HashMap<String, String>();
                    System.out.println(id + ", " + category);
                    map.put(TAG_ID_CATEGORY, id);
                    map.put(TAG_CATEGORY, category);

                    category_arraylist.add(map);
                }
            } else {
                System.out.println("Category not found");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return true;
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        mAuthTask = null;
        showProgress(false);

        if (success) {
            simpleAdapter = new SimpleAdapter(PlacesActivity.this, category_arraylist, R.layout.adapter_spinner_category,
                    new String[] { "category" },new int[]{R.id.lbl_category});
            simpleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {

                public boolean setViewValue(View view, Object data,String textRepresentation) {
                    TextView textView = (TextView) view;
                    textView.setTextColor(Color.BLACK);
                    textView.setText(textRepresentation);
                    return true;
                }
            };
            simpleAdapter.setViewBinder(viewBinder);
            sp_category.setAdapter(simpleAdapter);
            sp_category.requestFocus();
        }
    }

    @Override
    protected void onCancelled() {
        mAuthTask = null;
        showProgress(false);
    }
}

And this XML Code the Activity, activity_places.xml

<Spinner
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:id="@+id/sp_categoriy"
   android:theme="@style/Base.Widget.AppCompat.Spinner.Underlined" />

This XML Code for Adapter Spinner, adapter_spinner_category.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/lbl_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/navigationBarColor"/>

</LinearLayout>

Thanks.

Upvotes: 1

Views: 1134

Answers (2)

Alexandr Shutko
Alexandr Shutko

Reputation: 1875

simple_spinner_dropdown_item layout doesn't have 'lbl_category' id inside.

Make your custom DropDownView layout (just copy simple_spinner_dropdown_item) and set id 'lbl_category' to its CheckedTextView.

Upvotes: 1

Ahmed Shafeek
Ahmed Shafeek

Reputation: 26

Try This:

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(Contex,   R.layout.spinner_textview, ArrayOfElements);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);

Upvotes: 1

Related Questions