简体   繁体   中英

How to give each node unique name

I want to display a tree having the individual name of its parent, second node, and its child nodes. I've coded by using Google help. This code display tree has all second and child nodes with the same names. How can I give an unique name to each node of a tree?

My Java code is:

    package com.example.tree;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {

        ExpandableListView explvlist;

        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            explvlist = (ExpandableListView)findViewById(R.id.ParentLevel);
            explvlist.setAdapter(new ParentLevel());

        }

        public class ParentLevel extends BaseExpandableListAdapter
        {

      @Override
      public Object getChild(int arg0, int arg1) 
      {   
       return arg1;
      }

      @Override
      public long getChildId(int groupPosition, int childPosition) 
      {
       return childPosition;
      }

      @Override
      public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) 
      {
       CustExpListview SecondLevelexplv = new CustExpListview(MainActivity.this);
       SecondLevelexplv.setAdapter(new SecondLevelAdapter());
       SecondLevelexplv.setGroupIndicator(null);   
       return SecondLevelexplv;
      }

      @Override
      public int getChildrenCount(int groupPosition) 
      {   
       return 4;
      }

      @Override
      public Object getGroup(int groupPosition) 
      {
       return groupPosition;
      }

      @Override
      public int getGroupCount() 
      {   
       return 1;
      }

      @Override
      public long getGroupId(int groupPosition) 
      {   
       return groupPosition;
      }

      @Override
      public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) 
      {
       TextView tv = new TextView(MainActivity.this);
       tv.setText("Unit Testing");
       tv.setBackgroundColor(Color.BLUE);
       tv.setPadding(10, 7, 7, 7); 

       return tv;
      }

      @Override
      public boolean hasStableIds() 
      {
       return true;
      }

      @Override
      public boolean isChildSelectable(int groupPosition, int childPosition) 
      {
       return true;
      }     
        }

        public class CustExpListview extends ExpandableListView
        {

      int intGroupPosition, intChildPosition, intGroupid;

      public CustExpListview(Context context) 
      {
       super(context);     
      }

      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
      {
       widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
       heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
       super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      }  
        }

        public class SecondLevelAdapter extends BaseExpandableListAdapter
        {

      @Override
      public Object getChild(int groupPosition, int childPosition) 
      {   
       return childPosition;
      }

      @Override
      public long getChildId(int groupPosition, int childPosition) 
      {   
       return childPosition;
      }

      @Override
      public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) 
      {
       TextView tv = new TextView(MainActivity.this);
       tv.setText("Campaign Page should not be null");
       tv.setPadding(15, 5, 5, 5);
       tv.setBackgroundColor(Color.RED);
       tv.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
       return tv;
      }

      @Override
      public int getChildrenCount(int groupPosition) 
      {
       return 2;
      }

      @Override
      public Object getGroup(int groupPosition) 
      {   
       return groupPosition;
      }

      @Override
      public int getGroupCount() 
      {
       return 1;
      }

      @Override
      public long getGroupId(int groupPosition) 
      {
       return groupPosition;
      }

      @Override
      public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) 
      {
       TextView tv = new TextView(MainActivity.this);
       tv.setText("Get Campaign");
       tv.setPadding(12, 7, 7, 7);
       tv.setBackgroundColor(Color.CYAN);

       return tv;
      }

      @Override
      public boolean hasStableIds() {
       // TODO Auto-generated method stub
       return true;
      }

      @Override
      public boolean isChildSelectable(int groupPosition, int childPosition) {
       // TODO Auto-generated method stub
       return true;
      }

        }

}

I want each second and child nodes of tree to have an unique name and different colors.

Means you want to see the tree of data depending on the parent name.? So you used expandable list view.? right..?

Use these code..

main.xml having

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="HardCodedText" >

<ExpandableListView
    android:id="@+id/expandableListView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="40dip"
    android:groupIndicator="@null"
    android:scrollbars="none" >
</ExpandableListView>
</RelativeLayout>

And create the one ExpandListGroup.java file and add these code

public class ExpandListGroup {

private String Name;
private ArrayList<ExpandListChild> Items;

public ExpandListGroup(String name, ArrayList<ExpandListChild> items) {
    super();
    Name = name;
    Items = items;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public ArrayList<ExpandListChild> getItems() {
    return Items;
}

public void setItems(ArrayList<ExpandListChild> items) {
    Items = items;
}

}

Add the another one ExpandListChild.java file for child

public class ExpandListChild {

private String Name;
private String Tag;

public ExpandListChild(String name, String tag) {
    super();
    Name = name;
    Tag = tag;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public String getTag() {
    return Tag;
}

public void setTag(String tag) {
    Tag = tag;
}

}

And add these code in your MainActivity.java

public class ConfigureMyOrderItem extends MainActivity {

private ArrayList<ExpandableConfigureGroup> group_list;
private ArrayList<ExpandableConfigureChild> child_list;
ExpandableListView mExpandableListView;
ConfigureMyOrderAdapter adapter;
Button btn_confirm;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_configure_my_order_item);

    initView();

}

public void initView() {
    mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListViewConfigure);
    btn_confirm = (Button) findViewById(R.id.btn_Confirm_order);
    btn_confirm.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(ConfigureMyOrderItem.this,
                    MyOrderActivity.class);
            startActivity(intent);
        }
    });

    group_list = SetStandardGroups();

    adapter = new ConfigureMyOrderAdapter(ConfigureMyOrderItem.this,
            mExpandableListView, group_list);

    mExpandableListView.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater()
            .inflate(R.menu.activity_configure_my_order_item, menu);
    return true;
}

public ArrayList<ExpandableConfigureGroup> SetStandardGroups() {

    group_list = new ArrayList<ExpandableConfigureGroup>();
    child_list = new ArrayList<ExpandableConfigureChild>();

    group_list.add(new ExpandableConfigureGroup("Group", child_list));
    child_list.add(new ExpandableConfigureChild("Child1"));
    child_list.add(new ExpandableConfigureChild("Child2"));
    child_list.add(new ExpandableConfigureChild("Child3"));
    child_list.add(new ExpandableConfigureChild("Child4"));

    child_list = new ArrayList<ExpandableConfigureChild>();
    group_list.add(new ExpandableConfigureGroup("Category",
            child_list));
    child_list.add(new ExpandableConfigureChild("Item1"));
    child_list.add(new ExpandableConfigureChild("Item2"));
    child_list.add(new ExpandableConfigureChild("Item3"));
    child_list.add(new ExpandableConfigureChild("Item4"));

}

public void HomeButton(View v) {
    startActivity(new Intent(v.getContext(), MainActivity.class));
}

@Override
public void onClickQuickView(View v) {
    // TODO Auto-generated method stub
    super.onClickQuickView(v);
}

@Override
public void onClickQuickViewStatus(View v) {
    // TODO Auto-generated method stub
    super.onClickQuickViewStatus(v);
}

}

Also create the ConfigureMyOrderAdapter.java file

public class ConfigureMyOrderAdapter extends BaseExpandableListAdapter {

private Context context;
private ArrayList<ExpandableConfigureGroup> groups;
private ExpandableListView mExpandableListView;
private int[] groupStatus;

public ConfigureMyOrderAdapter(Context context,
        ExpandableListView mExpandableListView,
        ArrayList<ExpandableConfigureGroup> groups) {
    this.context = context;
    this.groups = groups;
    this.mExpandableListView = mExpandableListView;
    groupStatus = new int[groups.size()];

    setListEvent();
}

private void setListEvent() {

    mExpandableListView
            .setOnGroupExpandListener(new OnGroupExpandListener() {
                @Override
                public void onGroupExpand(int arg0) {
                    // TODO Auto-generated method stub
                    groupStatus[arg0] = 1;
                }
            });

    mExpandableListView
            .setOnGroupCollapseListener(new OnGroupCollapseListener() {
                @Override
                public void onGroupCollapse(int arg0) {
                    // TODO Auto-generated method stub
                    groupStatus[arg0] = 0;
                }
            });
}

public void addItem(ExpandableConfigureChild item,
        ExpandableConfigureGroup group) {
    if (!groups.contains(group)) {
        groups.add(group);
    }
    int index = groups.indexOf(group);
    ArrayList<ExpandableConfigureChild> ch = groups.get(index).getItems();
    ch.add(item);
    groups.get(index).setItems(ch);
}

public Object getChild(int groupPosition, int childPosition) {
    ArrayList<ExpandableConfigureChild> chList = groups.get(groupPosition)
            .getItems();
    return chList.get(childPosition);

}

public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean arg2, View view, ViewGroup arg4) {
    ExpandableConfigureChild child = (ExpandableConfigureChild) getChild(
            groupPosition, childPosition);

    if (view == null) {
        @SuppressWarnings("static-access")
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        view = infalInflater.inflate(
                R.layout.configure_list_raw_group_item, null);
    }
     TextView tv_price = (TextView) view.findViewById(R.id.item_price);
     tv_price.setText(child.getTag().toString());
     tv_price.setTag(child.getTag());

    return view;

}

@Override
public int getChildrenCount(int groupPosition) {
    ArrayList<ExpandableConfigureChild> chList = groups.get(groupPosition)
            .getItems();
    return chList.size();
}

@Override
public Object getGroup(int groupPosition) {
    return groups.get(groupPosition);
}

@Override
public int getGroupCount() {
    return groups.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@SuppressWarnings("static-access")
@Override
public View getGroupView(int groupPosition, boolean arg1, View view,
        ViewGroup arg3) {
    ExpandableConfigureGroup group = (ExpandableConfigureGroup) getGroup(groupPosition);
    if (view == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        view = inf.inflate(R.layout.configure_list_raw_group, null);
    }
    TextView tv = (TextView) view.findViewById(R.id.txtRestaurantMenuName);
    tv.setText(group.getName());

    ImageView img = (ImageView) view.findViewById(R.id.img_rightarrow);
    if (groupStatus[groupPosition] == 0) {
        img.setImageResource(R.drawable.navigation_next);
    } else {
        img.setImageResource(R.drawable.navigation_expandable);
    }
    return view;
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public boolean isChildSelectable(int arg0, int arg1) {
    return true;
}

}

and also create the two xml for the custom configure_list_raw_group.xml layout. so it is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="45dip"
android:orientation="horizontal"
android:weightSum="100"
tools:ignore="HardCodedText" >

<LinearLayout
    android:layout_width="0dip"
    android:layout_height="45dip"
    android:layout_marginLeft="4dip"
    android:layout_weight="88"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtRestaurantMenuName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RestaurantMenu Name"
        android:textSize="14sp" />
</LinearLayout>

<ImageView
    android:id="@+id/img_rightarrow"
    android:layout_width="0dip"
    android:layout_height="45dip"
    android:layout_gravity="center_vertical"
    android:layout_weight="7"
    android:contentDescription="@string/hello_world"
    android:src="@drawable/navigation_next_item" />

</LinearLayout>

and second for the child of group configure_list_raw_group_item.xml.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/groupItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal"
android:weightSum="100"
tools:ignore="HardCodedText" >

<TextView
    android:id="@+id/item_title"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_weight="75"
    android:text="sample"
    android:textSize="12sp" />

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM