开发者

Help on a simple Android ListView problem

I am new to Android, so this may seem like a basic question. But what I got is a very simple class call开发者_如何学Goed 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?


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


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();

}

.
.
.

});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜