【问题标题】:ExpandableListView OnClick change activityExpandableListView OnClick 更改活动
【发布时间】:2014-03-16 19:14:38
【问题描述】:

我的问题很简单,但我无法解决。我有 ExpandableListView,我只想在我的子项上单击转到 SecondActivity(当我单击 America 时转到 SecondActivity,同样的巴西等)。 吐司消息效果很好,但我想去 SecondActivity OnClick,当我尝试像这样Intent intent0 = new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent0); 它不起作用..

我对这段代码有点困惑,有人能说我必须在这段代码中改变什么来解决这个问题吗? 我是新手,非常感谢!:)

这是我的 MainActivity

public class MainActivity extends ExpandableListActivity{

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

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // this is not really  necessary as ExpandableListActivity contains an ExpandableList
        //setContentView(R.layout.main);


        ExpandableListView expandableList = getExpandableListView(); // you can use (ExpandableListView) findViewById(R.id.list)

        expandableList.setDividerHeight(2);
        expandableList.setGroupIndicator(null);
        expandableList.setClickable(true);

        setGroupParents();
        setChildData();

        MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems, childItems);

        adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
        expandableList.setAdapter(adapter);
        expandableList.setOnChildClickListener(this);
    }

    public void setGroupParents() {
        parentItems.add("A");
        parentItems.add("B");
        parentItems.add("C");
        parentItems.add("D");
    }

    public void setChildData() {

        // A
        ArrayList<String> child = new ArrayList<String>();
        child.add("America");
        childItems.add(child);


        // B
        child = new ArrayList<String>();
        child.add("Brazil");
        childItems.add(child);

        // C
        child = new ArrayList<String>();
        child.add("Chile");
        childItems.add(child);


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

}

这是我的 MyExpandableAdapter.java

public class MyExpandableAdapter extends BaseExpandableListAdapter {

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


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

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


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

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

        TextView textView = null;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.group, 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.row, null);
        }

        ((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
        ((CheckedTextView) convertView).setChecked(isExpanded);

        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>) childtems.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 true;
    }

}

这是我的 group.xml

<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" />
    </LinearLayout>


</LinearLayout>

MainActivity.xml

<?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/list"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:groupIndicator="@null" />   

</LinearLayout>

行.xml

<CheckedTextView 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:textColor="#000000"
    android:textSize="15sp"
    android:textStyle="bold" 
    android:background="#CCFF99"
    />

【问题讨论】:

    标签: android onclick expandablelistview onclicklistener


    【解决方案1】:

    尝试使用您的适配器 getView 而不是

     convertView.setOnClickListener(new OnClickListener()...
    

    类似

     textView.setOnClickListener(new OnClickListener()...
    

    //编辑

    textView.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View view) {
            Intent intent0 = new Intent(activity, SecondActivity.class);
            activity.startActivity(intent0);
        }
    });
    

    【讨论】:

    • 我试过了,但它仍然不起作用,它说:方法 getApplicationContext() 未定义类型 new View.OnClickListener(){} 当我尝试将 Toastmessage 代码替换为此代码 Intent intent0 = new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent0);
    • 我修复了它...查看编辑 ....convertview.setOn... 将代码粘贴到那里...我在手机上尝试过,它可以工作...
    【解决方案2】:

    也许您可以尝试覆盖 MainActivity 中的方法 onChildClick() 并编写代码以在那里启动其他活动。

    【讨论】:

    • 我试图替换这段代码 expandableList.setOnChildClickListener(this);在 MainActivity 到此代码 expandableList.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Intent intent0 = new Intent(getApplicationContext(), Vysledek.class) ; startActivity(intent0); return false; } });它不起作用(应用程序工作但只出现吐司消息)
    • 您是否尝试过在活动中用 this 而不是 getApplicationContext() 替换您的意图的第一个参数(例如:Intent intent0 = new Intent(this, Vysledek.class);) ?
    • 它说:构造函数 Intent(new ExpandableListView.OnChildClickListener(){}, Class) 未定义,知道如何解决吗?
    • 是的,很抱歉我忘了在这种情况下 this 指的是你所在的监听器,所以你应该写 MainActivity.this (Intent intent0 = new Intent(MainActivity.this, Vysledek.class); )。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多