简体   繁体   English

如何给每个节点唯一的名称

[英]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. 我已经使用Google帮助进行了编码。 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: 我的Java代码是:

    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 main.xml具有

<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 并创建一个ExpandListGroup.java文件并添加以下代码

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 为子级添加另一个ExpandListChild.java文件

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 并将这些代码添加到您的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 还要创建ConfigureMyOrderAdapter.java文件

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. 并为自定义的configure_list_raw_group.xml布局创建两个xml。 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. 第二个是组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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为10 x 10网格按钮布局中的每个JButton提供唯一的ID /名称 - How to give each JButton in a 10 x 10 grid button layout a unique ID/name 如何为文档中的每个对象提供唯一的ID? - How to give each object in a document a unique ID? 如何为用户在 JAVA 程序中创建的每个学生提供唯一 ID - How to give a unique ID to each student that is created by the user in a JAVA program Java - 如何为从数组生成的每个图块提供唯一的“ID” - Java - How to give each tile generated from an array a unique "ID" 如何为Java应用程序提供唯一的进程名称? - How can I give my Java application a unique process name? 如何为 XML 映射中的每个列表值指定不同的名称? - How to give different name for each list value in XML mapping? 如何在此代码中为每个文本字段和标签命名? JAVA - How to give each textfield and label a name in this code? JAVA 如何将JSON文件加载到每个记录具有唯一名称的Java中? - How to load a JSON file into java that has unique name for each record? 如何为Log4j中的每次运行生成唯一的文件名 - How to generate unique file name for each run in Log4j 在循环中为每个字段创建具有唯一名称的textField - Creating textField with unique name for each in a loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM