org.deviceconnect.android.client.fragment.DrawerListFragment.java Source code

Java tutorial

Introduction

Here is the source code for org.deviceconnect.android.client.fragment.DrawerListFragment.java

Source

/*
 DrawerListFragment.java
 Copyright (c) 2014 NTT DOCOMO,INC.
 Released under the MIT license
 http://opensource.org/licenses/mit-license.php
 */
package org.deviceconnect.android.client.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * .
 */
public class DrawerListFragment extends ListFragment {

    /**
     * .
     */
    private List<Object> mDataList = new ArrayList<Object>();

    /**
     * .
     */
    private ArrayAdapter<Object> mListAdapter = null;

    /**
     * .
     */
    private OnDataListSelectedListener mListListener = null;

    /**
     * ??.
     * @param devices ?
     * @return 
     */
    public static DrawerListFragment newInstance(final List<Object> devices) {
        DrawerListFragment fragment = new DrawerListFragment();
        return fragment;
    }

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mListAdapter = new ArrayAdapter<Object>(getActivity(), android.R.layout.simple_list_item_1, mDataList);
        setListAdapter(mListAdapter);
    }

    @Override
    public void onListItemClick(final ListView parent, final View view, final int position, final long id) {
        super.onListItemClick(parent, view, position, id);
        if (mListListener != null) {
            mListListener.onDataListSelected(parent, view, position, id);
        }
    }

    /**
     * ?.
     * @param data 
     */
    public void setDataList(final List<? extends Object> data) {

        mListAdapter.clear();

        if (data != null && data.size() > 0) {

            List<String> titleList = new ArrayList<String>();
            for (Object obj : data) {
                if (obj instanceof Fragment && ((Fragment) obj).getArguments() != null) {
                    titleList.add(((Fragment) obj).getArguments().getString(Intent.EXTRA_TITLE, obj.toString()));
                } else {
                    titleList.add(obj.toString());
                }
            }
            mListAdapter.addAll(titleList);
            mListAdapter.notifyDataSetChanged();

        }

    }

    /**
     * ??.
     * @param listener 
     */
    public void setDataListSelectedListener(final OnDataListSelectedListener listener) {
        mListListener = listener;
    }

    /**
     * ?.
     */
    public interface OnDataListSelectedListener {
        /**
         * ??.
         * @param parent View
         * @param view ???View
         * @param position ?????
         * @param id ????ID
         */
        void onDataListSelected(ListView parent, View view, int position, long id);
    }

}