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

不羁的心 提交于 2019-12-24 22:27:30

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/24062698/arrayadapterdummycontent-dummyitem-with-android-r-layout-simple-list-item-acti

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