如何从android

时间:2015-07-25 15:14:59

标签: android view tree

我正在创建一个包含树视图的android应用程序。我正在使用从here下载的树视图。我在树视图中有一个带有子视图的复选框。我需要得到树视图中选定的复选框值可以告诉我如何从树视图中获取选中的复选框值 这是我的树视图:

package pl.polidea.treeview.demo;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import pl.polidea.treeview.InMemoryTreeStateManager;
import pl.polidea.treeview.R;
import pl.polidea.treeview.TreeBuilder;
import pl.polidea.treeview.TreeNodeInfo;
import pl.polidea.treeview.TreeStateManager;
import pl.polidea.treeview.TreeViewList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

/**
 * Demo activity showing how the tree view can be used.
 * 
 */
public class TreeViewListDemo extends Activity {



    private enum TreeType implements Serializable {
        SIMPLE,
        FANCY
    }

    private final Set<Long> selected = new HashSet<Long>();

    private static final String TAG = TreeViewListDemo.class.getSimpleName();
    private TreeViewList treeView;

    public static final int[] DEMO_NODES = new int[] { 
                                                    0, 1, 2, 2, 2, 2, 2, 2,2,2, 1, 2, 2, 2, 2,2,2,2,2,//MR 1
                                                    0, 1, 2, 2, 2, 2, 2, 2,2,2, 1, 2, 2, 2, 2,2,2,2,2, //MR 2
                                                    0, 1, 2, 2, 2, 2, 2, 2,2,2,    //MR 3 
                                                    0, 1, 2, 2, 2, 2, 2, 2,2,2,    //MR 4
                                                    0, 1, 2, 2, 2, 2, 2, 2,2,2, 1, 2, 2, 2, 2,2,2,2,2,//MR 5
                                                    0, 1, 2, 2, 2, 2, 2, 2,2,2, 1, 2, 2, 2, 2,2,2,2,2,//MR 6
                                                    0, 1, 2, 2, 2, 2, 2, 2,2,2,    //MR 7
                                                    0, 1, 1, 1, 1, 1, 1,1,1,    //UR
                                                    0, 1, 1, 1, 1, 1, 1,1,1,   //FUSE Bucket
                                                    0, 1, 1, 1, 1, //IR_LR         
                                                    0    

};
    private static final int LEVEL_NUMBER = 4;
    private TreeStateManager<Long> manager = null;
    private FancyColouredVariousSizesAdapter fancyAdapter;
    private SimpleStandardAdapter simpleAdapter;
    private TreeType treeType;
    private boolean collapsible;

    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TreeType newTreeType = null;
        boolean newCollapsible;
        CheckBox box;
        treeView = (TreeViewList)findViewById(R.id.mainTreeView);

        if (savedInstanceState == null) {
            manager = new InMemoryTreeStateManager<Long>();
            final TreeBuilder<Long> treeBuilder = new TreeBuilder<Long>(manager);
            for (int i = 0; i < DEMO_NODES.length; i++) {

                treeBuilder.sequentiallyAddNextNode((long) i, DEMO_NODES[i]);

            }
            Log.d(TAG, manager.toString());
            newTreeType = TreeType.SIMPLE;
            newCollapsible = true;
        } 
        else 
        {
            manager = (TreeStateManager<Long>) savedInstanceState
                    .getSerializable("treeManager");
            if (manager == null) {
                manager = new InMemoryTreeStateManager<Long>();
            }
            newTreeType = (TreeType) savedInstanceState
                    .getSerializable("treeType");
            if (newTreeType == null) {
                newTreeType = TreeType.SIMPLE;
            }
            newCollapsible = savedInstanceState.getBoolean("collapsible");
        }
        setContentView(R.layout.main_demo);
        treeView = (TreeViewList) findViewById(R.id.mainTreeView);
        fancyAdapter = new FancyColouredVariousSizesAdapter(this, selected,manager, LEVEL_NUMBER);
        simpleAdapter = new SimpleStandardAdapter(this, selected, manager,LEVEL_NUMBER);
        setTreeAdapter(newTreeType);
        setCollapsible(newCollapsible);
        registerForContextMenu(treeView);



    }

    @Override
    protected void onSaveInstanceState(final Bundle outState) {
        outState.putSerializable("treeManager", manager);
        outState.putSerializable("treeType", treeType);
        outState.putBoolean("collapsible", this.collapsible);
        super.onSaveInstanceState(outState);
    }

    protected final void setTreeAdapter(final TreeType newTreeType) {
        this.treeType = newTreeType;
        switch (newTreeType) {
        case SIMPLE:
            treeView.setAdapter(simpleAdapter);
            break;
        case FANCY:
            treeView.setAdapter(fancyAdapter);
            break;
        default:
            treeView.setAdapter(simpleAdapter);
        }
    }

    protected final void setCollapsible(final boolean newCollapsible) {
        this.collapsible = newCollapsible;
        treeView.setCollapsible(this.collapsible);
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        final MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(final Menu menu) {
        final MenuItem collapsibleMenu = menu
                .findItem(R.id.collapsible_menu_item);
        if (collapsible) {
            collapsibleMenu.setTitle(R.string.collapsible_menu_disable);
            collapsibleMenu.setTitleCondensed(getResources().getString(
                    R.string.collapsible_condensed_disable));
        } else {
            collapsibleMenu.setTitle(R.string.collapsible_menu_enable);
            collapsibleMenu.setTitleCondensed(getResources().getString(
                    R.string.collapsible_condensed_enable));
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        if (item.getItemId() == R.id.simple_menu_item) {
            setTreeAdapter(TreeType.SIMPLE);
        } else if (item.getItemId() == R.id.fancy_menu_item) {
            setTreeAdapter(TreeType.FANCY);
        } else if (item.getItemId() == R.id.collapsible_menu_item) {
            setCollapsible(!this.collapsible);
        } else if (item.getItemId() == R.id.expand_all_menu_item) {
            manager.expandEverythingBelow(null);
        } else if (item.getItemId() == R.id.collapse_all_menu_item) {
            manager.collapseChildren(null);
        } else {
            return false;
        }
        return true;
    }

    @Override
    public void onCreateContextMenu(final ContextMenu menu, final View v,
            final ContextMenuInfo menuInfo) {
        final AdapterContextMenuInfo adapterInfo = (AdapterContextMenuInfo) menuInfo;
        final long id = adapterInfo.id;
        final TreeNodeInfo<Long> info = manager.getNodeInfo(id);
        final MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.context_menu, menu);
        if (info.isWithChildren()) 
        {
            if (info.isExpanded()) 
            {
                menu.findItem(R.id.Test).setVisible(false);
                menu.findItem(R.id.Clear).setVisible(false);
            } 
        } 
             super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(final MenuItem item) {
        final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
                .getMenuInfo();
        final long id = info.id;


        if(item.getItemId()==R.id.Test)
        {
            Toast.makeText(TreeViewListDemo.this, "SAMPLE",Toast.LENGTH_LONG).show();
            StringBuffer text = new StringBuffer();
            text.append("Selected Items : \n");



        }

        /*if (item.getItemId() == R.id.context_menu_collapse) {
            manager.collapseChildren(id);
            return true;
        } else if (item.getItemId() == R.id.context_menu_expand_all) {
            manager.expandEverythingBelow(id);
            return true;
        } else if (item.getItemId() == R.id.context_menu_expand_item) {
            manager.expandDirectChildren(id);
            return true;
        } else if (item.getItemId() == R.id.context_menu_delete) {
            manager.removeNodeRecursively(id);
            return true;
        } *///else {
            return super.onContextItemSelected(item);
        }
    //}
}

这是我的适配器:

package pl.polidea.treeview.demo;

import java.util.Arrays;
import java.util.Set;

import pl.polidea.treeview.AbstractTreeViewAdapter;
import pl.polidea.treeview.R;
import pl.polidea.treeview.TreeNodeInfo;
import pl.polidea.treeview.TreeStateManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * This is a very simple adapter that provides very basic tree view with a
 * checkboxes and simple item description.
 * 
 */
public class SimpleStandardAdapter extends AbstractTreeViewAdapter<Long> 
{
    public TreeViewListDemo tree_view_list;

    private final Set<Long> selected;
    public int[] selected_items;
    CheckBox box;
    String Relay_Name[]={
                         "Measurment Relay Card 1","Relay Power 1","Relay 1  (K1)","Relay 2  (K2)","Relay 3  (K3)","Relay 4  (K4)","Relay 5  (K5)","Relay 6  (K6)","Relay 7  (K7)","Relay 8  (K8)",
                                                   "Relay Power 2","Relay 9  (K9)","Relay 10 (K10)","Relay 11 (K11)","Relay 12 (K12)","Relay 13 (K13)","Relay 14 (K14)","Relay 15 (K15)","Relay 16 (K16)",     
                         "Measurment Relay Card 2","Relay Power 1","Relay 1  (K1)","Relay 2  (K2)","Relay 3  (K3)","Relay 4  (K4)","Relay 5  (K5)","Relay 6  (K6)","Relay 7  (K7)","Relay 8  (K8)",
                                                   "Relay Power 2","Relay 9  (K9)","Relay 10 (K10)","Relay 11 (K11)","Relay 12 (K12)","Relay 13 (K13)","Relay 14 (K14)","Relay 15 (K15)","Relay 16 (K16)",
                         "Measurment Relay Card 3","Relay Power 1","Relay 1  (K1)","Relay 2  (K2)","Relay 3  (K3)","Relay 4  (K4)","Relay 5  (K5)","Relay 6  (K6)","Relay 7  (K7)","Relay 8  (K8)",
                         "Measurment Relay Card 4","Relay Power 1","Relay 1  (K1)","Relay 2  (K2)","Relay 3  (K3)","Relay 4  (K4)","Relay 5  (K5)","Relay 6  (K6)","Relay 7  (K7)","Relay 8  (K8)",
                         "Measurment Relay Card 5","Relay Power 1","Relay 1  (K1)","Relay 2  (K2)","Relay 3  (K3)","Relay 4  (K4)","Relay 5  (K5)","Relay 6  (K6)","Relay 7  (K7)","Relay 8  (K8)",
                                                   "Relay Power 2","Relay 9  (K9)","Relay 10 (K10)","Relay 11 (K11)","Relay 12 (K12)","Relay 13 (K13)","Relay 14 (K14)","Relay 15 (K15)","Relay 16 (K16)",
                         "Measurment Relay Card 6","Relay Power 1","Relay 1  (K1)","Relay 2  (K2)","Relay 3  (K3)","Relay 4  (K4)","Relay 5  (K5)","Relay 6  (K6)","Relay 7  (K7)","Relay 8  (K8)",
                                                   "Relay Power 2","Relay 9  (K9)","Relay 10 (K10)","Relay 11 (K11)","Relay 12 (K12)","Relay 13 (K13)","Relay 14 (K14)","Relay 15 (K15)","Relay 16 (K16)",                     
                         "Measurment Relay Card 7","Relay Power 1","Relay 1  (K1)","Relay 2  (K2)","Relay 3  (K3)","Relay 4  (K4)","Relay 5  (K5)","Relay 6  (K6)","Relay 7  (K7)","Relay 8  (K8)",                         
                         "Universal  Relay Card  ","Relay 1  (K1)","Relay 2  (K2)","Relay 3  (K3)","Relay 4  (K4)","Relay 5  (K5)","Relay 6  (K6)","Relay 7  (K7)","Relay 8  (K8)",                             
                         "Safety Fuse Relay Cards","Relay 1  (K1)","Relay 2  (K2)","Relay 3  (K3)","Relay 4  (K4)","Relay 5  (K5)","Relay 6  (K6)","Relay 7  (K7)","Relay 8  (K8)",                                 
                         "IR LR CARD             ","Relay 1  (K1)","Relay 2  (K2)","Relay 3  (K3)","Relay 4  (K4)", 
                         "Buzzer"    
                    };

    private final OnCheckedChangeListener onCheckedChange = new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(final CompoundButton buttonView,
                final boolean isChecked) {
            final Long id = (Long) buttonView.getTag();
           // changeSelected(isChecked, id);





        }

    };


    private void changeSelected(final boolean isChecked, final Long id) {
        if (isChecked) {
            selected.add(id);
        } else {
            selected.remove(id);
        }
    }

    public SimpleStandardAdapter(final TreeViewListDemo treeViewListDemo,
            final Set<Long> selected,
            final TreeStateManager<Long> treeStateManager,
            final int numberOfLevels) 
    {
        super(treeViewListDemo, treeStateManager, numberOfLevels);
        this.selected = selected;
    }

    private String getDescription(final long id) {
        final Integer[] hierarchy = getManager().getHierarchyDescription(id);
        return Relay_Name[(int) id];
    }

    @Override
    public View getNewChildView(final TreeNodeInfo<Long> treeNodeInfo) {
        final LinearLayout viewLayout = (LinearLayout) getActivity()
                .getLayoutInflater().inflate(R.layout.demo_list_item, null);
        return updateView(viewLayout, treeNodeInfo);
    }

    @Override
    public LinearLayout updateView(final View view,
            final TreeNodeInfo<Long> treeNodeInfo) 
    {
        final LinearLayout viewLayout = (LinearLayout) view;
        final TextView descriptionView = (TextView) viewLayout.findViewById(R.id.demo_list_item_description);
        //final TextView levelView = (TextView) viewLayout.findViewById(R.id.demo_list_item_level);
        ;
        descriptionView.setText(getDescription(treeNodeInfo.getId()));

      //  levelView.setText(Integer.toString(treeNodeInfo.getLevel()));

        box = (CheckBox) viewLayout.findViewById(R.id.demo_list_checkbox);

        //box.setTag(treeNodeInfo.getId());

        if (treeNodeInfo.isWithChildren()) 
        {
            box.setVisibility(View.GONE);
        } 
        else 
        {
            box.setVisibility(View.VISIBLE);
            box.setChecked(selected.contains(treeNodeInfo.getId()));
        }
        box.setOnCheckedChangeListener(onCheckedChange);

        return viewLayout;
    }

    @Override
    public void handleItemClick(final View view, final Object id) {
        final Long longId = (Long) id;
        final TreeNodeInfo<Long> info = getManager().getNodeInfo(longId);
        if (info.isWithChildren()) {
            super.handleItemClick(view, id);
        } else {
            final ViewGroup vg = (ViewGroup) view;
            final CheckBox cb = (CheckBox) vg.findViewById(R.id.demo_list_checkbox);
            cb.performClick();
        }
    }

    @Override
    public long getItemId(final int position) {
        return getTreeId(position);
    }
}

0 个答案:

没有答案