我有以下代码用于扩展到 BaseExpandableListAdapter 的 ExtendableListAdapter 类。我正在尝试将字符串变量“FullTrainsURL”的值转移到一个新的活动中。正如您在代码中看到的,每个子列表项的 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...
但是我想将变量传输到新活动的类扩展到“活动”以外的“BaseExpandableListAdapter”。我认为这是我不能使用 intent.putExtras(); 的原因。方法,我得到了错误。
您能否指导我如何使用 OnClick() 方法将“fullTrainsURL”变量传输到新活动。
:::::我的实际文件代码:::::
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;
}
}