Jonathon Charles Loch Jonathon Charles Loch - 1 month ago 17
Android Question

Change the text on spinners created by a CustomAdapter (extends BaseAdapter)

My app creates a ListView that creates list items based on the number of strings in my array. However, I would like each Spinner item to have the appropriate item selected.

Example:

String[] items = {"item1", "item2", "item3"}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String[] items = {"item1", "item2", "item3"}

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ListView listView = (ListView) findViewById(R.id.requestItems_listView);
listView.setAdapter(new CustomAdapter(this));
}

private class CustomAdapter extends BaseAdapter {
LayoutInflater inflater;

public CustomAdapter(Context context) {
inflater = LayoutInflater.from(context);
}

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

public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public View getView(int arg0, View convertview, ViewGroup arg2) {
ViewHolder viewHolder;
if (convertview == null) {
convertview = inflater.inflate(R.layout.request_item_list_item, null);
viewHolder = new ViewHolder();
viewHolder.spinner = (Spinner) convertview.findViewById(R.id.itemSpinner);
viewHolder.spinner.setAdapter(adapter);
convertview.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertview.getTag();
}
return convertview;
}

public class ViewHolder {
Spinner spinner;
}
}
}


So this should create 3 (because there are 3 items in the String[] "items") "request_item_list_item" items within the ListView.

And it has each spinner (1 part of request_item_list_item) in position 0 so:

"item1"

"item1"

"item1"

I would like it to do the following though:

"item1"

"item2"

"item3"

Would this be accomplished in the CustomAdapter?

BONUS Question:
If I wanted the user to be able to press an "Add Button" to increase the number of list items by 1 (so that there would be 4 request_item_list_items, 5, 6, etc.) is there a .add method I should be using? or do I just change the getCount and refresh the list (this wouldn't be ideal... if the user had already filled out information in the request_item_list_items, they would then lose all of their inputs when it refreshes)?

Answer

For the first question you just need to change getView like this: (as I_A_Mok commented)

public View getView(int arg0, View convertview, ViewGroup arg2) {
    ViewHolder viewHolder;
    if (convertview == null) {
        convertview = inflater.inflate(R.layout.request_item_list_item, null);
        viewHolder = new ViewHolder();
        viewHolder.spinner = (Spinner) convertview.findViewById(R.id.itemSpinner);
        viewHolder.spinner.setAdapter(adapter);
        convertview.setTag(viewHolder);
        viewHolder.spinner.setSelection(arg0);
    } else {
        viewHolder = (ViewHolder) convertview.getTag();
    }
    return convertview;
}

and for second question:

CustomAdapter myAdapter;
ArrayList<String> items;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    items = new ArrayList<>();
    items.add("item1");
    items.add("item2");
    items.add("item3");

    ListView listView = (ListView) findViewById(R.id.requestItems_listView);
    myAdapter = new CustomAdapter(this, items);
    listView.setAdapter(myAdapter);

    Button bt_add_new = (Button)findViewById(R.id.bt_add_new);
    bt_add_new.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            items.add("item" + items.size() + 1);
            myAdapter.setItems(items);
            myAdapter.notifyDataSetChanged();
        }
    });
}

private class CustomAdapter extends BaseAdapter {
    LayoutInflater inflater;

    private ArrayList<String> items;

    public CustomAdapter(Context context, ArrayList<String> items) {
        inflater = LayoutInflater.from(context);
        this.items = items;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return items.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int arg0, View convertview, ViewGroup arg2) {
        ViewHolder viewHolder;

        convertview = inflater.inflate(R.layout.request_item_list_item, null);
        viewHolder = new ViewHolder();
        viewHolder.spinner = (Spinner) convertview.findViewById(R.id.itemSpinner);

        ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        viewHolder.spinner.setAdapter(adapter);
        convertview.setTag(viewHolder);

        return convertview;
    }

    public ArrayList<String> getItems() {
        return items;
    }

    public void setItems(ArrayList<String> items) {
        this.items = items;
    }

    public class ViewHolder {
        Spinner spinner;
    }
}