0

I'm looking a bit of help on how to get a spinner show multiple items but, get the data from a SQLitedatabase.

I'm already able to get data from the database and put it into a spinner with one item. I do this by pulling a column from the database into an ArrayList then adding the ArrayList to the Array Adapter like below, this is my current code:

spinner = (Spinner) view.findViewById(R.id.Spinner);
ArrayAdapter<String> catAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, ArrayListNames);
catAdapter.setDropDownViewResource(R.layout.spinner_item);
spinner.setAdapter(catAdapter);

What I need however is for the spinner to show two items for each row. Right now I'm pulling a column called 'Names' from the database. I want to be able to show the column 'Age' along with that.

So the spinner will look like this:

Item 1: John
        33

Item 2: Dave
        24

The user can then select either John or Dave from the spinner. I just want to be able to show more than one bit of information in each row. The issue for me, is that it's coming from a database into an ArrayList then into the spinner's adapter.

Does anyone know how I can have that extra row for Age?

I would really appreciate some help, thanks!

EDIT: How I'm getting the data right now.

ArrayList<String> ArrayListNames = new ArrayList<String>();

    Cursor c = db.rawQuery("SELECT * FROM table_one", null);
    if (c.moveToFirst()) {
        do {
            ArrayListNames.add(c.getString(c.getColumnIndex("name")));
            ArrayListNames.add(c.getString(c.getColumnIndex("age")));
        }
        while (c.moveToNext());
    }
    c.close();
RED_
  • 2,997
  • 4
  • 40
  • 59
  • so you want to show name + age in your spinner? – Rohan Kandwal Jan 19 '14 at 17:54
  • Yes. Name + age for each item. – RED_ Jan 19 '14 at 17:55
  • what's the problem? you can make a loop from list item 0 of `ArrayListNames` to the end and at each step concat the age along with the name. – Rohan Kandwal Jan 19 '14 at 17:58
  • If I understand you correctly that's what I'm doing right now. But each name and age are in different items. So the spinner lets the user chose from name or age. I'll update my question to show you how the data is being put into the ArrayList with the Cursor. – RED_ Jan 19 '14 at 18:00
  • I am still confused, will `John 33` or similar work for you or you want `John` and `33` on two different line but treated as one item? – Rohan Kandwal Jan 19 '14 at 18:06
  • Yes, two different lines but treated as one item. I found an image: http://3.bp.blogspot.com/-RfoYPfyzTNY/UAU1uxw2nhI/AAAAAAAAAFs/EdFzxKoXb1g/s1600/4.png Like this but without the icons on the left. – RED_ Jan 19 '14 at 18:07
  • please see this question http://stackoverflow.com/q/15754099/1979347 – Rohan Kandwal Jan 19 '14 at 18:08
  • I think you're misunderstanding me. Have you seen the image in my comment above? it has two lines for each item? – RED_ Jan 19 '14 at 18:13
  • First of all that is not spinner and secondly if you understand the link that I provided I think you will get your answer. – Rohan Kandwal Jan 19 '14 at 18:14
  • That is not a spinner but a listview which is shown in a dialog. – Rohan Kandwal Jan 19 '14 at 18:15
  • @RohanKandwal I did a reverse image search and found the tutorial, it's a spinner. I need to figure out how to get the ArrayList to work with that, should have done this before.. http://hasmukhbhadani.blogspot.co.uk/2012/05/spinner-in-android.html – RED_ Jan 19 '14 at 18:18
  • If you are following the tutorial then you should have seen that it is using a custom adapter. – Rohan Kandwal Jan 19 '14 at 18:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45588/discussion-between-rohan-kandwal-and-red) – Rohan Kandwal Jan 19 '14 at 18:23

2 Answers2

0

I have managed to do this is an app myself. Change:

catAdapter.setDropDownViewResource(R.layout.spinner_item);
spinner.setAdapter(catAdapter);

to

catAdapter.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
spinner.setAdapter(catAdapter);

and create an xml file called multiline_spinner_dropdown_item.xml in the layout folder with the following in it

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="none"
android:singleLine="false"
android:textSize="15sp" />

EDIT: It would be like the code below

Spinner spinner = (Spinner)findViewById(R.id.spinner);
    ArrayAdapter<String> catAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, android.R.id.text1);
    catAdapter.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
    spinner.setAdapter(catAdapter);
    catAdapter.add("value");
    catAdapter.add("v \n alue");
    catAdapter.add("value");
    catAdapter.add("value");
    catAdapter.notifyDataSetChanged();

The second entry in the array will have "v" on one line and "alu" on the 2nd.

Howli
  • 12,291
  • 19
  • 47
  • 72
0

I have posted the code and explaination in the chat room. The code below is modified for personal use from custom spinner in android

import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;

public class customSpinner extends Activity
{
ArrayList<String> name = new ArrayList<String>();
ArrayList<String> age = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.customspinner);
for (int i =0 ;i<=2;i++){
name.add(""+i);
age.add(""+i);
}
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
mySpinner.setAdapter(new MyAdapter(this, R.layout.spinnerrow, name));
}

public class MyAdapter extends ArrayAdapter<String>
{

public MyAdapter(Context context, int textViewResourceId, ArrayList<String> objects)
{
super(context, textViewResourceId, objects);
}

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

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

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

LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.spinnerrow, parent, false);
TextView label=(TextView)row.findViewById(R.id.company);
label.setText(name.get(position));

TextView sub=(TextView)row.findViewById(R.id.sub);
sub.setText(age.get(position));


return row;
}
}
}

Instead of using for loop to add item to ArrayList, you can get values from database using cursors or give it manually. Just modify according to your own need.

Community
  • 1
  • 1
Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107