ArrayAdapter<DummyContent.DummyItem> with android.R.layout.simple_list_item_activated_2

StackOverflow https://stackoverflow.com//questions/24062698

  •  27-12-2019
  •  | 
  •  

Question

I'm currently using the master / detail flow in my android application project. Now I would like to not only create a list with items with only one string. I'd like to change the standard DummyItem class to the following:

     /**
     * A dummy item representing a piece of content.
     */
    public static class DummyItem {
        public String id;
        public String content;
        public String subtext; //Added subtext variable here
        public DummyItem(String id, String content, String subtext) {
            this.id = id;
            this.content = content;
            this.subtext = subtext; //And here
        }

        @Override
        public String toString() {
            return content;
        }
    }

In the ItemListFragment class I'm having this line of code pre-defined for creating the adapter for the list:

    setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(), 
                   android.R.layout.simple_list_item_activated_1, 
                   android.R.id.text1, 
                   DummyContent.ITEMS));

But I would like to change the android.R.layout.simple_list_item_activated_1 to android.R.layout.simple_list_item_activated_2

while having android.R.id.text1 as content and android.R.id.text2 as my subtext-variable.

Is there a possibility to do this?

Was it helpful?

Solution

Overwrite the getView() Method of the ArrayAdapter. Should be something like this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    //super call to create / recycle the view
    View view = super.getView(position, convertView, parent);

    TextView textView1 = (TextView) view.findViewById(android.R.id.text1);
    textView1.setText(getItem(position).getContent());

    TextView textView2 = (TextView) view.findViewById(android.R.id.text2);
    textView2.setText(getItem(position).getSubtext());

    return view;

}

Here's some further reading about ListViews, including examples: http://www.vogella.com/tutorials/AndroidListView/article.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top