3

I am developing android app in which I used two spinner view.I fetched data from web services and stored it in two list array and those two list array I assigned to spinner view respectively,but I have to assign some default value to spinner view like "select category" and "select sub category" respectively before assigning list array .So how to achieve this? Please help me out.

Thanks in advance...

पone
  • 99
  • 1
  • 9

3 Answers3

3

Use ArrayList instead array. Before adding values from webservices, add the default value to the array list first.

You can use as follows.

ArraList<String> list=new ArrayList<String>();
list.add("Select Category");

//Your logic to get data from web services

//Add the data to array list.

list.add("data1");
list.add("data2");

Hope this helps.

Vilas
  • 1,695
  • 1
  • 13
  • 13
1

Try this solution. It has 26 answers and many different techniques for the task you want to accomplish.

Community
  • 1
  • 1
Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
0

You can use this code.

String defaultTextForSpinner = "Select Category";
Spinner spinnerCategory = (Spinner)
            .findViewById(R.id.spinner2);

After that you need to make custom adapter for spinner

CategoryAdapter cat_adapter = new CategoryAdapter(this, categoryList,
                    R.layout.spinner_row, defaultTextForSpinner);
spinnerCategory.setAdapter(cat_adapter);
cat_adapter.notifyDataSetChanged();

Adapter class :

public class CategoryAdapter extends ArrayAdapter<String> {

Activity context;
ArrayList<HashMap<String, String>> categoryList;
String firstElement;
boolean isFirstTime;
LayoutInflater mInflater;
String[] objects;

public CategoryAdapter(FragmentActivity context,
        ArrayList<HashMap<String, String>> categoryList, int spinnerRow,
        String defaultTextForSpinner) {

    super(context, spinnerRow);
    this.context = context;
    this.categoryList = categoryList;
    this.constant = new Const(context);
    mInflater = LayoutInflater.from(context);
    objects = new String[this.categoryList.size()];
    setDefaultText(defaultTextForSpinner);


}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    for (int i = 0; i < categoryList.size(); i++) {
        objects[i] = categoryList.get(i).get("category_name");
    }
    if (isFirstTime) {
        objects[0] = firstElement;
        isFirstTime = false;
    }
    return getCustomView(position, convertView, parent);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    notifyDataSetChanged();
    return getCustomView(position, convertView, parent);
}

public void setDefaultText(String defaultText) {
    this.firstElement = objects[0];
    objects[0] = defaultText;
}

public View getCustomView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.spinner_row, parent, false);
    TextView label = (TextView) row.findViewById(R.id.txtSpinner);
    label.setText(objects[position]);

    return row;
}

@Override
public int getCount() { // TODO Auto-generated method stub
    return objects.length;
}

}
RockStar
  • 409
  • 2
  • 6