0

I am new to Android, so this may seem like a basic question. But what I got is a very simple class called SingleItem, which has an integer and a String and a getter and setter for each. In my application, I got an ArrayList which holds a collection of SingleItem objects. I also have a layout with a ListView widget.

What I am trying to do is populate the ListView with my String value in SingleItem, but when a user selects an item from the ListView, I need the integer ID from that SingleItem value. How do I do this in Android development?

Icemanind
  • 47,519
  • 50
  • 171
  • 296

2 Answers2

1

If you are using your own adapter to populate the list then in the getView() function when building the view to return you can call setTag() on the view you are returning and store the entire "SingleItem" object. Then in the onClickListener of the views you return you can retrieve your info using the getTag() method of the view that has been clicked.

EDIT: Specified which onClickListener I am referring to

snctln
  • 12,175
  • 6
  • 45
  • 42
  • onCLickListener is for the selection of the entire listview. To return an item for a specific item, you should use OnItemSelectedListener – jkhouw1 May 11 '11 at 02:02
  • oi I love downvotes... I use 1 onClickListener in all of my custom adapters, in the getView() function I simply set the onClickListener of view I build and return to the 1 onClickListener() in the adapter. This works great because I am able to store all the data I need in an object using setTag() – snctln May 11 '11 at 04:37
0

here is a bunch of pseudo code: create your own adapter. This will give the flexibility to do all kinds of things but important to you here is displaying only the relevant fields from your custom class and make more complicated listviews. a decent tutorial is here: http://developerlife.com/tutorials/?p=327

You will have to handle the other overrides of baseadapter but the key is assigning the value of singleItem.getString()

public class SingleItemAdapter extends BaseAdapter{
    private ArrayList<SingleItem> m_items= new ArrayList<SingleItem>();
          private Context mContext;
          public SingleItemAdapter (Context c,ArrayList<SingleItem> items) {
        mContext = c;
        m_items= items;
    }
.
.
.

    @Override
    public Object getItem(int arg0) {
        return m_items.get(arg0);
    }

           @Override
    public View getView(int position, View convertView, ViewGroup parent) {

             View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.singleitemview, null);
        }
            SingleItem i=(SingleItem) getITem(position)
            if(v!=null){
                TextView tv=(TextView) v.findViewById(R.id.yourListItemView);
                tv.setText(i.getStringValue());
}

}
}

After defining your custom adapter, you can then assign it to the listview and assign a listener to the OnItemSelectedListener. since this returns the position, you can tie that back to the position in your ArrayList of SingleItems.

.
.
.
SingleItemAdapter sia=new SingleItemAdapter(this,yourArray);
yourArrayList.setAdapter(sia);
yourArrayList.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
    public void onItemSelected(AdapterView<?> parent, View v, int position, long row) {
          SingleItem si= yourArray.getItem(position);
           //do something with si.getValue();

}

.
.
.

});
jkhouw1
  • 7,320
  • 3
  • 32
  • 24
  • Giving working code fragments is not how people learn jk. The proper way is have to give them some explanation and then some code fragments, anyone can look up examples.. but only a select few will understand it. Its then up to the programmer to learn to adapt what they are using and give it context. NEVER BE a copy and paste programmer, it will destroy you as a programmer. Always ask questions, don't just take it for what it is. Understand it. – JoxTraex May 11 '11 at 01:43
  • good point. I didn't think this was too complicated and would point him in the right direction. I'll edit the post with some more color. – jkhouw1 May 11 '11 at 01:45
  • Thanks, this is a learning/troubleshooting environment, not a solution center. – JoxTraex May 11 '11 at 01:47
  • thanks for the downvote :P an interesting commentary from someone who last posted this answer: http://stackoverflow.com/questions/5520175/storing-lists-to-a-database-and-retrieving-them-all-together-android/5520241#5520241 – jkhouw1 May 11 '11 at 01:57
  • lol.. too many people do people question databases, so understanding to modularize is it very important. So I created that post to help clarify the issue. However; code with explanation goes a long way. Specifically I didn't give him the solution, I gave him the structure to do what he needed. :) HUGE difference. – JoxTraex May 11 '11 at 02:01
  • +1...I thought the downvote was unfair for this helpful answer. :) – Ben Jakuben May 11 '11 at 02:40