Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 also 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?

like image 496
Giulio Avatar asked Dec 13 '22 14:12

Giulio


1 Answers

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); 
like image 116
Samuel Avatar answered Jan 13 '23 20:01

Samuel