Android中的嵌套列表,带有ArrayAdapter和不同的ObjectTypes

时间:2015-05-07 15:06:43

标签: android android-listview

我有以下问题。我有3种类型的对象:

设备, 目录, 歌曲

现在我只显示我的设备 ArrayAdapter<DeviceDisplay>

如何在同一列表中显示所有3种类型?

此外,我想在列表中单击时显示给定设备的目录,单击时只显示目录中的歌曲。

我也在片段中使用所有内容。

这在某种程度上是可以实现的吗?

我片段中的代码:

public class MusicListFragment extends ListFragment {

    ArrayAdapter<DeviceDisplay> mediaServerAdapter;
    ArrayList<Device> mediaServers = new ArrayList<Device>();
    ArrayList<Container> containers = new ArrayList<Container>();
    ArrayList<Item> items = new ArrayList<Item>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.music_list, container, false);

        getMediaServers();

        mediaServerAdapter = new ArrayAdapter<DeviceDisplay>(this.getActivity(), android.R.layout.simple_list_item_1);
        setListAdapter(mediaServerAdapter);
        addDevicesToAdapter();


        return rootView;
    }



    public void getMediaServers(){
        Activity a = getActivity();
        ArrayList<Device> devices = ((MainActivity) a).getDevices();
        for (int i = 0; i < devices.size(); i++) {
            Device device = devices.get(i);
            if( device.getType().getType().equals( "MediaServer" ) ){
                if (!mediaServers.contains(device)) {
                    mediaServers.add(device);
                }
            }
        }
    }

    public void addDevicesToAdapter(){
        for (int i = 0; i < mediaServers.size() ; i++) {
            mediaServerAdapter.add(new DeviceDisplay(mediaServers.get(i)));
            listContent(mediaServers.get(i));
        }
    }

    public void listContent(Device device){

        System.out.println(device.getDisplayString());
        Service service = device.findService(new UDAServiceId("ContentDirectory"));


        ActionCallback browse = new Browse(service, "0", BrowseFlag.DIRECT_CHILDREN) {

            @Override
            public void received(ActionInvocation actionInvocation, DIDLContent didl) {

                //Containers and songs come here.

            }

            @Override
            public void updateStatus(Status status) {

            }

            @Override
            public void failure(ActionInvocation invocation,
                                UpnpResponse operation,
                                String defaultMsg) {

            }
        };



        ((MainActivity) getActivity()).upnpService.getControlPoint().execute(browse);
    }



}

1 个答案:

答案 0 :(得分:1)

也许不是最好的灵魂,但你可以为你的DeviceDisplayDirectories制作基类,等等,看起来像这样:

public abstract class BaseClass {
    public abstract int getViewType();

}

然后从中扩展您的课程,覆盖getViewType()

并将List作为适配器项目

然后在适配器中覆盖此方法:

@Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        return items.get(position).getViewType()
    }

在你的getView中你可以这样:

switch (getItemViewType(position)) {
//Here you know which type this item has and you can draw it whatever you want
}