基本上,我正在尝试在点击expandableListView
上的孩子时打开一项活动,但无法完成。尝试了很多解决方案(主要是改变意图的参数)无济于事。
这些是我的课程:
main_Magic
package com.arrowny.exalted;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class main_Magic extends AppCompatActivity {
TextView tv1, tv2, tv3;
Typeface tf1, tf2;
private ExpandableListView expandableListView;
private List<String> headersList;
private int lastPosition = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_magic);
tf1 = Typeface.createFromAsset(getAssets(), "fonts/Chapter.ttf");
tf2 = Typeface.createFromAsset(getAssets(), "fonts/Titles.ttf");
tv1 = (TextView) findViewById(R.id.chapter);
tv2 = (TextView) findViewById(R.id.title);
tv3 = (TextView) findViewById(R.id.footer);
tv1.setTypeface(tf1);
tv2.setTypeface(tf1);
tv3.setTypeface(tf2);
headersList = new ArrayList<>();
String[] headers = getResources().getStringArray(R.array.magic);
List<String> parentHeaders = new ArrayList<>(Arrays.asList(headers));
headersList.addAll(parentHeaders);
HashMap<String, List<String>> allChildItems = returnGroupedChildItems();
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
final ExpandableListViewAdapter expandableListViewAdapter = new ExpandableListViewAdapter(getApplicationContext(), headersList, allChildItems);
expandableListView.setAdapter(expandableListViewAdapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
if (lastPosition != -1 && groupPosition != lastPosition) {
expandableListView.collapseGroup(lastPosition);
}
lastPosition = groupPosition;
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if (groupPosition == 0 && childPosition == 0) {
Intent solar = new Intent(v.getContext(), charm_Solar.class); //This line is where I want to open the new Activity. Currently it stops the application while running it.
startActivity(solar);
}
return false;
}
});
}
private HashMap<String, List<String>> returnGroupedChildItems() {
HashMap<String, List<String>> childContent = new HashMap<>();
int size = headersList.size();
for (int i = 1; i <= size; i++) {
List<String> childList = new ArrayList<>();
if (i == 1) {
String[] child = getResources().getStringArray(R.array.exalted);
List<String> childHeaders = new ArrayList<>(Arrays.asList(child));
childList.addAll(childHeaders);
} else if (i == 2) {
String[] child = getResources().getStringArray(R.array.martialArts);
List<String> childHeaders = new ArrayList<>(Arrays.asList(child));
childList.addAll(childHeaders);
} else if (i == 3) {
String[] child = getResources().getStringArray(R.array.knacks);
List<String> childHeaders = new ArrayList<>(Arrays.asList(child));
childList.addAll(childHeaders);
} else if (i == 4) {
String[] child = getResources().getStringArray(R.array.astrology);
List<String> childHeaders = new ArrayList<>(Arrays.asList(child));
childList.addAll(childHeaders);
} else if (i == 5) {
String[] child = getResources().getStringArray(R.array.sorcery);
List<String> childHeaders = new ArrayList<>(Arrays.asList(child));
childList.addAll(childHeaders);
} else if (i == 6) {
String[] child = getResources().getStringArray(R.array.necromancy);
List<String> childHeaders = new ArrayList<>(Arrays.asList(child));
childList.addAll(childHeaders);
} else if (i == 7) {
String[] child = getResources().getStringArray(R.array.protocols);
List<String> childHeaders = new ArrayList<>(Arrays.asList(child));
childList.addAll(childHeaders);
} else if (i == 8) {
String[] child = getResources().getStringArray(R.array.arcanoi);
List<String> childHeaders = new ArrayList<>(Arrays.asList(child));
childList.addAll(childHeaders);
} else if (i == 9) {
String[] child = getResources().getStringArray(R.array.thaumaturgy);
List<String> childHeaders = new ArrayList<>(Arrays.asList(child));
childList.addAll(childHeaders);
} else if (i == 10) {
String[] child = getResources().getStringArray(R.array.patterns);
List<String> childHeaders = new ArrayList<>(Arrays.asList(child));
childList.addAll(childHeaders);
} else if (i == 11) {
String[] child = getResources().getStringArray(R.array.paths);
List<String> childHeaders = new ArrayList<>(Arrays.asList(child));
childList.addAll(childHeaders);
}
childContent.put(headersList.get(i - 1), childList);
}
return childContent;
}
}
这是我的适配器:
ExpandableListViewAdapter
package com.arrowny.exalted;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> parentDataSource;
private HashMap<String, List<String>> childDataSource;
public ExpandableListViewAdapter(Context context, List<String> childParent, HashMap<String, List<String>> child) {
this.context = context;
this.parentDataSource = childParent;
this.childDataSource = child;
}
@Override
public int getGroupCount() {
return this.parentDataSource.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return this.childDataSource.get(this.parentDataSource.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return parentDataSource.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this.childDataSource.get(parentDataSource.get(groupPosition)).get(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;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.parent_layout, parent, false);
}
String parentHeader = (String) getGroup(groupPosition);
TextView parentItem = (TextView) view.findViewById(R.id.parent_layout);
parentItem.setText(parentHeader);
Typeface parentFont = Typeface.createFromAsset(context.getAssets(), "fonts/Titles.ttf");
parentItem.setTypeface(parentFont, Typeface.BOLD);
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.child_layout, parent, false);
}
String childName = (String) getChild(groupPosition, childPosition);
TextView childItem = (TextView) view.findViewById(R.id.child_layout);
childItem.setText(childName);
Typeface childFont = Typeface.createFromAsset(context.getAssets(), "fonts/Titles.ttf");
childItem.setTypeface(childFont);
return view;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
希望任何人都可以帮助我。