android - 将变量值从 BaseExpandableListAdapter 类转移到新 Activity

标签 android

我有以下扩展到 BaseExpandableListAdapter 的 ExtendableListAdapter 类的代码。 我正在尝试将字符串变量“FullTrainsURL”的值传输到新 Activity 。正如您在代码中看到的那样,每个子列表项的 URL 值都是不同的,并且使用 OnClick() 函数附加了特定代码。 我试过了

Intent i = new Intent(AdapterForStations.this, TestMain.class);

Bundle b = new Bundle();

b.putString("url", fullTrainsURL);

i.putExtras(b);

startActivity(i);    //Gives error at this line...

但是我想将变量从中传输到新 Activity 的类扩展到“Activity”以外的“BaseExpandableListAdapter”。我认为这是我不能使用 intent.putExtras(); 的原因。方法,我得到了错误。

能否指导我如何使用 OnClick() 方法将“fullTrainsURL”变量传输到新 Activity 。

::::::我的实际文件代码::::::

package com.multilayerexpandable.view;

import java.util.ArrayList;
import java.util.LinkedList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.javacodegeeks.android.xml.pull.R;
import com.javacodegeeks.android.xml.pull.XmlParsingStationsData;
import com.javacodegeeks.android.xml.pull.model.Station;

@SuppressWarnings("unchecked")
public class AdapterForStations extends BaseExpandableListAdapter {

private static final String baseUrlForTrains = "http://cloud.tfl.gov.uk/TrackerNet/PredictionDetailed/";

XmlParsingStationsData parsingStations = new XmlParsingStationsData();
MainStationsActivity stationActivity = new MainStationsActivity();
LinkedList<Station> stationsList = new LinkedList<Station>();

public ArrayList<String> groupItem, tempChild;
public ArrayList<Object> Childtem = new ArrayList<Object>();
public LayoutInflater minflater;
public Activity activity;
private final String TAG = getClass().getSimpleName();



public AdapterForStations(ArrayList<String> grList, ArrayList<Object> childItem) {
    groupItem = grList;
    this.Childtem = childItem;
}

public void setInflater(LayoutInflater mInflater, Activity act) {
    this.minflater = mInflater;
    activity = act;
}

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

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

@Override
public View getChildView(final int groupPosition, final int childPosition,
    boolean isLastChild, View convertView, ViewGroup parent) {
    tempChild = (ArrayList<String>) Childtem.get(groupPosition);

    Log.d(TAG, "::::::::::::::::::::DATA:::::::::::::::::::\n" + "<<<<<<" + tempChild.get(childPosition) + ">>>>>>\n");

    TextView text = null;
    if (convertView == null) {
        convertView = minflater.inflate(R.layout.childrow, null);
    }
    text = (TextView) convertView.findViewById(R.id.textView1);
    text.setText(tempChild.get(childPosition));
    convertView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            StringBuilder trainsUrl = new StringBuilder(baseUrlForTrains);
            String fullTrainsURL = "";

                if(groupItem.get(groupPosition).equals("BAKERLOO LINE")){
                String stationCode = tempChild.get(childPosition).substring(tempChild.get(childPosition).length()-4, tempChild.get(childPosition).length()-1);
                trainsUrl.append("B/").append(stationCode);
                fullTrainsURL = trainsUrl.toString();

                Intent i = new Intent(AdapterForStations.this, TestMain.class);
                Bundle b = new Bundle();
                b.putString("url", fullTrainsURL);
                i.putExtras(b);
                startActivity(i);


            } else if(groupItem.get(groupPosition).equals("CENTRAL LINE")){
                String stationCode = tempChild.get(childPosition).substring(tempChild.get(childPosition).length()-4, tempChild.get(childPosition).length()-1);
                trainsUrl.append("C/").append(stationCode);
                fullTrainsURL = trainsUrl.toString();


            } else if(groupItem.get(groupPosition).equals("CIRCLE LINE")){
                String stationCode = tempChild.get(childPosition).substring(tempChild.get(childPosition).length()-4, tempChild.get(childPosition).length()-1);
                trainsUrl.append("H/").append(stationCode);
                fullTrainsURL = trainsUrl.toString();


            }

            Toast.makeText(activity, fullTrainsURL,
                    Toast.LENGTH_SHORT).show();
            }

    });
    return convertView;
}

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

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

@Override
public int getGroupCount() {
    return groupItem.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 View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = minflater.inflate(R.layout.grouprow, null);
    }
    ((CheckedTextView) convertView).setText(groupItem.get(groupPosition));
    ((CheckedTextView) convertView).setChecked(isExpanded);
    return convertView;
}

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

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

最佳答案

startActivity()是 Context 类的成员,尝试这样的事情:

Context context = v.getContext();
Intent i = new Intent(context, TestMain.class);
i.putExtra("url", fullTrainsURL);
context.startActivity(i);

另外你不需要创建第二个Bundle,直接将String添加到Intent内置的Bundle中即可。

关于android - 将变量值从 BaseExpandableListAdapter 类转移到新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14095751/

相关文章:

java - 使用 Android 通过蓝牙发送数据

android - 为什么 findViewById() 抛出空异常?

android - checkout 源代码后如何切换到特定版本的 Android?

android - SpannableStringBuilder 性能改进?

android visibility - ImageView 的预留空间

android - 我的 kivy 程序总是在 Android 上旋转屏幕

android - 禁用 Lint 警告 "The view name suggests this is a number but it does not include a numeric inputType"

android - 带 Horizo​​ntalScrollView 的 DrawerLayout 进入内容菜单

java - Android Wear水平拖动下一个 View

android - Jetpack Compose 格式日期字符串