首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用ExpandableListView启动子类?

如何使用ExpandableListView启动子类?
EN

Stack Overflow用户
提问于 2015-11-21 02:42:36
回答 2查看 37关注 0票数 0

我有一个ExpandableListView,我想设置一个ASyncTask用来解析JSON的url,具体取决于单击哪个子程序。在设置url之后,应该执行,然后启动显示该信息的子类。我尝试了一个if语句,但它想要一个布尔值,也是一个实验性的祝酒词消息,但什么也没做。

适配器:

代码语言:javascript
复制
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;
    }

}

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

代码语言:javascript
复制
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);
    }

}

主要布局:

代码语言:javascript
复制
<?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>

小组案文:

代码语言:javascript
复制
<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>

儿童:

代码语言:javascript
复制
<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"/>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-24 14:07:27

通过将activity.startActivity(new Intent(activity, class.class));放在ListAdapter的onClickListener中,而不是通过活动类来解决问题。

票数 0
EN

Stack Overflow用户

发布于 2015-11-21 03:06:26

试试..。

代码语言:javascript
复制
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;
                }


        });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33839125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档