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...
しかし、変数を新しいアクティビティに転送したいクラスは、「Activity」以外の「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;
}
}