开发者

ListView and hidden Id. How it is possible?

I'm developing an application for android and now I have implemented a ListView that shows a list of courses, connected to a database.

I would like to know how to include, with the name, an hidden id (that come from the db) so that once the user click on the elements the app goes to the relative view of the selected courses.

And how can I maintain the id during the navigation inside the course-view?

At the moment my code just load the name of the courses from the db and set in the list view:

ArrayAdapter<String> result = new ArrayAdapter<String>(CourseActivity.this, android.R.layout.simple_list_item_1);

for (CourseRecord c : get()) 
 result.add(c.getFullname());

lv.setAdapter(result);

obviously I'm able to do als开发者_如何学Co c.getid() but I don't where to put the id.

Thank you very much.

P.S.: Maybe does someone have also a really nice graphics of list view?


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜