0

我想显示一棵树,它具有其父节点、第二个节点及其子节点的单独名称。我已经使用 Google 帮助进行了编码。此代码显示树具有同名的所有第二个和子节点。如何为树的每个节点赋予唯一的名称?

我的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;
      }

        }

}

我希望树的每个第二个和子节点都有一个唯一的名称和不同的颜色。

4

1 回答 1

1

意味着您想根据父名称查看数据树。?所以你使用了可扩展的列表视图。?对..?

使用这些代码..

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>

并创建一个 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;
}

}

为孩子添加另一个 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;
}

}

并将这些代码添加到您的 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);
}

}

同时创建 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;
}

}

并为自定义 configure_list_raw_group.xml 布局创建两个 xml。所以它是

<?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>

第二个是组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>
于 2013-03-14T05:38:12.700 回答