我想显示一棵树,它具有其父节点、第二个节点及其子节点的单独名称。我已经使用 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;
}
}
}
我希望树的每个第二个和子节点都有一个唯一的名称和不同的颜色。