ListView and hidden Id. How it is possible?

限于喜欢 提交于 2019-12-03 16:35:25
Samuel

change your array adapter like this.

private ArrayAdapter<String> result = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1){
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        v.setTag(getMyIdForPosition(position));
        return convertView;
    }
};

and have an item click handler to recieve the selected ids

   private OnItemClickListener itemClickedHandler = new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id)
    {
        String myId = (String)v.getTag();
        doYourStuff(myId);
    }
    };

assign the listener to the list

myList= (ListView)findViewById(R.id.history);
myList.setOnItemClickListener(itemClickedHandler); 

You can store the id in a hidden TextView. In the list item XML, add 'android:visibility="gone"' to the TextView. Likewise in the click handler you can read the id from the textview.

You can also store id using setTag(Object object) method of a View. Use getTag() method to extract that id from that view.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!