Android- Data is allocated in spinner but when sel

2019-06-22 09:12发布

Problem I am fetching some product category from mysql using Retrofit. The data is coming and allocated in spinner but when I select an item its not displayed. In the drop down menu the items are allocated and the setOnItemSelected listener is also working. But the selected item not showing up in the spinner. I have tried almost everything also related stackoverflow questions but not working. Pls help me. Thanks in advance.

Code

This is the OrderActivity

public class OrderActivity extends AppCompatActivity  {

Spinner sp_category,sp_product;
public static final String ROOT_URL = "http://10.0.2.2/sizzle/";
private List<Product> books;
List<String> prod_cat = new ArrayList<String>();
ArrayAdapter<String> adapter_prod_cat;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_order);

    //sp_product = (Spinner)findViewById(R.id.spinner_product);
    getBooks();
    sp_category = (Spinner)findViewById(R.id.spinner_category);
    sp_category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String category = parent.getSelectedItem().toString();
            Toast.makeText(getApplicationContext(),category,Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}


private void getBooks(){
    final ProgressDialog loading = ProgressDialog.show(this, "Fetching Data", "Please wait...", false, false);

    RestAdapter adapter = new RestAdapter.Builder()
            .setEndpoint(ROOT_URL)
            .build();

    ProductAPI api = adapter.create(ProductAPI.class);

    api.getProduct(new Callback<List<Product>>() {
        @Override
        public void success(List<Product> list, Response response) {
//Dismissing the loading progressbar
            loading.dismiss();
            books = list;
            for (int i = 0; i < books.size(); i++) {
                prod_cat.add(books.get(i).getProductCategory());
                System.out.println("added");
            }
            Set<String> citySet = new HashSet<String>(prod_cat);
            prod_cat.clear();
            prod_cat.addAll(citySet);
            prod_cat.add("Select Category");
            showListinSpinner();
        }

        @Override
        public void failure(RetrofitError error) {
            Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_LONG).show();
        }
    });
}

private void showListinSpinner(){
    //String array to store all the book names
    String[] items = new String[prod_cat.size()];

    //Traversing through the whole list to get all the names
    for(int i=0; i<prod_cat.size(); i++){
        //Storing names to string array
        items[i] = prod_cat.get(i);
    }

    //Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<String> adapter;
    adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, items);
    //setting adapter to spinner
    adapter.notifyDataSetChanged();
    sp_category.setAdapter(adapter);
    //Creating an array adapter for list view
}

}

This is the layout file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res   /android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.skipper.ash.ashtet.OrderActivity">
 <Spinner
    android:layout_width="300dp"
    android:layout_height="80dp"
    android:id="@+id/spinner_category"
    android:spinnerMode="dropdown"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="43dp"
    android:textAlignment="center"
    android:visibility="visible" />
</RelativeLayout>

1条回答
爷的心禁止访问
2楼-- · 2019-06-22 09:37

Create xml file in layout folder named item_spinner and write the code below in it

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15sp"
    android:textColor="#000000" >

and change in your code while creating adapter.

adapter = new ArrayAdapter<String>(getApplicationContext(), 
                      R.layout.item_spinner, items);

also change the sequence of the statements

sp_category.setAdapter(adapter);
adapter.notifyDataSetChanged();

First set adapter to spinner and then fire data set changed event.

Hope it'll help you.

EDIT

I observed lot of time that if we set the adapter to ListView or Spinner programatically using ArrayAdapter and uses android.R.layout.simple_spinner_item_1 or any other build in layout, then it sets the text color of item to white. I didn't get it why it sets the white color to text by default.

查看更多
登录 后发表回答