如何使用ExpandableListView启动子类?

时间:2015-11-21 02:42:37

标签: java android expandablelistview

我有一个ExpandableListView,我想设置一个URL,ASyncTask根据点击的子项来解析JSON。设置url之后,它应该执行,然后启动显示带有intent的信息的子类。我尝试了一个if语句,但它想要一个布尔值,一个实验性的toast消息什么也没做。

适配器:

public class ListAdapter extends BaseExpandableListAdapter
{

    private Activity activity;
    private ArrayList<Object> childItems;
    private LayoutInflater inflater;
    private ArrayList<String> parentItems, child;

    public ListAdapter(ArrayList<String> parents, ArrayList<Object> childern) {
        this.parentItems = parents;
        this.childItems = childern;
    }

    public void setInflater(LayoutInflater inflater, Activity activity) {
        this.inflater = inflater;
        this.activity = activity;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        child = (ArrayList<String>) childItems.get(groupPosition);

        TextView textView = null;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.listitems, null);
        }

        textView = (TextView) convertView.findViewById(R.id.textView1);
        textView.setText(child.get(childPosition));

        convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    Toast.makeText(activity, child.get(childPosition),
                                   Toast.LENGTH_SHORT).show();
                }
            });

        return convertView;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.listchild, null);
        }

        ((TextView) convertView).setText(parentItems.get(groupPosition));


        return convertView;
    }

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

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return ((ArrayList<String>) childItems.get(groupPosition)).size();
    }

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

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

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);
    }

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

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

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

}

显示可扩展列表视图的类:

public class ShipList extends ExpandableListActivity 
{

    private ArrayList<String> parentItems = new ArrayList<String>();
    private ArrayList<Object> childItems = new ArrayList<Object>();

    private static String url = null;

    //tags for the parser here

    JSONObject arrayGetter;
    JSONArray shipStats = null;
    JSONArray hardpoints = null;

    private ProgressDialog status;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        //TextViews here

        ExpandableListView expandableList = getExpandableListView();
        expandableList.setDividerHeight(2);
        expandableList.setGroupIndicator(null);
        expandableList.setClickable(true);

        setGroupParents();
        setChildData();

        ListAdapter adapter = new ListAdapter(parentItems, childItems);

        adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
        expandableList.setAdapter(adapter);
        expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

                @Override
                public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
                {
                    /** What do I put here? Each child has different url so will likely need an if statement or switch **/
                    return true;
                }


        });


    }

    public void setGroupParents() {

        parentItems.add("Aegis Dynamics");
    }

    public void setChildData() {

        // Android
        ArrayList<String> child = new ArrayList<String>();
        child.add("Sabre");
        childItems.add(child);
    }

}

主要布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ExpandableListView
        android:id="@+id/shipMenu"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:groupIndicator="@null" />   

</LinearLayout>

小组文字:

<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="40dp"
    android:background="@android:color/black"
    android:clickable="true"
    android:orientation="vertical"
    android:paddingLeft="40dp"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="39dp"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:textIsSelectable="true"
            android:textColor="#FFFFFF"
            android:textSize="14sp"
            android:textStyle="bold"
            android:focusable="false"/>
    </LinearLayout>

</LinearLayout>

子:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:layout_marginLeft="5dp"
    android:gravity="center_vertical"
    android:padding="10dp"
    android:textSize="15sp"
    android:textStyle="bold"
    android:focusable="false"/>

2 个答案:

答案 0 :(得分:0)

试试..

expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

                @Override
                public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
                {
                    /** What do I put here? Each child has different url so will likely need an if statement or switch **/

                      String your_url = (String)adapter.getChild(groupPosition, childPosition);
                     // make call related to the url
                    return true;
                }


        });

答案 1 :(得分:0)

通过将activity.startActivity(new Intent(activity, class.class));放入ListAdapter的onClickListener而不是从活动类中进行解决。