0

親、2番目のノード、およびその子ノードの個別の名前を持つツリーを表示したいと思います。Googleのヘルプを使用してコーディングしました。このコード表示ツリーには、同じ名前のすべての2番目と子のノードがあります。ツリーの各ノードに一意の名前を付けるにはどうすればよいですか?

私の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 ファイルを 1 つ作成し、これらのコードを追加します。

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 レイアウト用に 2 つの 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>

2 番目はグループ 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 に答える