Search code examples
androidlistviewlistadapter

Change layout of listview item onclick


I have a ListView with custom adaptor that I molded off the example shown here.

I want to take the user's selection and inflate a different layout for that selection. So that I can expand the view to display more information when an item is selected. Can I simply inflate a new layout onclick using something like this?

protected void onListItemClick(ListView list, View v, int position, long id){
    super.onListItemClick(list, v, position, id);
        LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.new_layout, null);

After this section of code I would populate the individual TextView and ImageView objects, just as I did in the initial list only this time I would include more fields. Is this right or is there a better approach?


Solution

  • this won't work since you haven't done anything to the view that you got . you only replaced its reference (and didn't do anything with the newly inflated view) and not really modified anything in the "views tree" .

    what you can do is to empty the view from its content and add new content into it .

    don't forget that in the getView you will have to do the same thing and return a view with this content.