尝试使用TextView作为按钮移动到ExpendableListView中的另一个活动

时间:2018-04-23 18:52:01

标签: android android-layout listview android-studio expandablelistview

当我点击ExpendableListView中的下拉列表值时,我想转到我的secondActivity。我已经看过示例显示toast消息但没有移动到另一个activity.I我很新的android.please告诉我我该怎么做。我也试过了意向课。提前谢谢。

这是我的第二个动态.java文件

package com.example.android.infoline;  

import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  

public class SecondActivity extends AppCompatActivity {  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_second);  
    }

}

secondActivity.xml文件

<?xml version="1.0" encoding="utf-8"?>
<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:context=".SecondActivity">

    <TextView
        android:id="@+id/txtCancer1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="20dp"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:theme="@style/AppTheme" />

    <TextView
        android:id="@+id/txtCancer2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/txtCancer1"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Tooltip"
        android:theme="@style/AppTheme" />

</RelativeLayout>

ExpandableListViewAdapter.java文件

package com.example.android.infoline;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ExpandableListViewAdapter extends BaseExpandableListAdapter implements View.OnClickListener{


    String[] MainTopics = {"Health", "Network","Bank"};
    String[][] subTopics = {{"Cancer","Depression","Diabeties","Rape","Road Accident"
    ,"HIV", "Periods"},{"Vodaphone","Airtel"},{"SBI","PNB","HDFC"}};

    Context context;

    public ExpandableListViewAdapter(Context context){
        this.context = context;
    }
    @Override
    public int getGroupCount() {
        return MainTopics.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return subTopics[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return MainTopics[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {

        return subTopics[groupPosition][childPosition];
    }

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

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

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

    //to return the main topics
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
        TextView txtview = new TextView(context);
        txtview.setText(MainTopics[groupPosition]);
        txtview.setPadding(100,0,0,0);
        txtview.setTextColor(Color.GREEN);
        txtview.setTextSize(40);
        return txtview;
    }

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

        TextView txtview = new TextView(context);
        txtview.setText(subTopics[groupPosition][childPosition]);
        txtview.setPadding(100,0,0,0);
        txtview.setTextColor(Color.CYAN);
        txtview.setTextSize(40);
        txtview.setOnClickListener(this);
        //txtview.setOnClickListener(Toast.makeText(context,txtview.getText().toString(),Toast.LENGTH_SHORT).show(););

        return txtview;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(MainActivity.this,SecondActivity.class);
        startActivity(intent);
    }

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

2 个答案:

答案 0 :(得分:0)

onClick(View v)方法中,写一下:

Intent intent = new Intent(context, SecondActivity.class);
startActivity(intent);

如果要从单击的按钮检索文本,请写:

// optionally, check if the view is an instance of TextView
String text = ((TextView) v).getText().toString();

如果要将字符串传递给SecondActivity,请写:

intent.putExtra(SOME_CONSTANT, text);

并在SecondActivity中检索它,写一下:

Intent intent = getIntent();
String text = intent.getStringExtra(SOME_CONSTANT);

最后一件事:确保您的SecondActivity不仅仅是一个空TextView的透明背景

答案 1 :(得分:0)

由于您没有将上下文传递给intent,因此无法正常工作。在适配器中,您需要传递Context。

尝试以下代码: https://stackoverflow.com/a/49998683/6920195