gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:20 24 4
gpt4 key购买 nike

我有以下扩展到 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com