问题描述
我发现了很多关于这个问题的问题,但我不能修复它。我有一整天我没能解决消费。我重新检查我的为
循环还
如果我有亲值7,如果我在第4父单击它的膨胀和我再次对2号母单击其消费和工作的罚款。但如果我点击第五或第六或第七父我得到这个错误..我不知道我错了,在我的code ..
LogCat中:
11月7日至12日:56:24.504:E / AndroidRuntime(3548):致命异常:主要
11月7日至12日:56:24.504:E / AndroidRuntime(3548):java.lang.IndexOutOfBoundsException:无效指数1,大小为0
11月7日至12日:56:24.504:E / AndroidRuntime(3548):在java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
11月7日至12日:56:24.504:E / AndroidRuntime(3548):在java.util.ArrayList.add(ArrayList.java:143)
11月7日至12日:56:24.504:E / AndroidRuntime(3548):在android.widget.ExpandableListConnector.expandGroup(ExpandableListConnector.java:685)
11月7日至12日:56:24.504:E / AndroidRuntime(3548):在android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:562)
11月7日至12日:56:24.504:E / AndroidRuntime(3548):在android.widget.ExpandableListView.performItemClick(ExpandableListView.java:522)
11月7日至12日:56:24.504:E / AndroidRuntime(3548):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:2812)
11月7日至12日:56:24.504:E / AndroidRuntime(3548):在android.widget.AbsListView $ 1.run(AbsListView.java:3571)
11月7日至12日:56:24.504:E / AndroidRuntime(3548):在android.os.Handler.handleCallback(Handler.java:725)
11月7日至12日:56:24.504:E / AndroidRuntime(3548):在android.os.Handler.dispatchMessage(Handler.java:92)
11月7日至12日:56:24.504:E / AndroidRuntime(3548):在android.os.Looper.loop(Looper.java:153)
11月7日至12日:56:24.504:E / AndroidRuntime(3548):在android.app.ActivityThread.main(ActivityThread.java:5297)
11月7日至12日:56:24.504:E / AndroidRuntime(3548):在java.lang.reflect.Method.invokeNative(本机方法)
11月7日至12日:56:24.504:E / AndroidRuntime(3548):在java.lang.reflect.Method.invoke(Method.java:511)
11月7日至12日:56:24.504:E / AndroidRuntime(3548):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833)
11月7日至12日:56:24.504:E / AndroidRuntime(3548):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11月7日至12日:56:24.504:E / AndroidRuntime(3548):在dalvik.system.NativeStart.main(本机方法)
的Java
mAdapter =新MyExpandableListAdapter(MainActivity.this,listchapt,
空值); elvForDialog.setIndicatorBounds(宽 - 20,宽度); // 130
elvForDialog.setAdapter(mAdapter); elvForDialog.setOnGroupClickListener(新OnGroupClickListener(){ 公共布尔onGroupClick(ExpandableListView为arg0,ARG1查看,
INT ARG2,长ARG3)抛出的RuntimeException {
字符串POS =(字符串)mAdapter.getGroup(ARG2);
的System.out.println(位置+ POS)
清单<串GT;孩子=新的ArrayList<串GT;();
children.clear();
listNames.clear();
的for(int i = 0; I< listchapt.size();我++){ dbHelper.openDataBase();
孩子= dbHelper.GetSubchapt(POS)
dbHelper.close();
listNames.add(儿童); //线143号
} 的System.out.println(位置+ POS)
显示newDisplay = getWindowManager()getDefaultDisplay()。
INT宽度= newDisplay.getWidth();
mAdapter =新MyExpandableListAdapter(MainActivity.this,
listchapt,listNames);
elvForDialog.setIndicatorBounds(宽 - 20,宽度); // 130
elvForDialog.setAdapter(mAdapter);
的System.out.println(explist+ listSubchapt); 尝试{ Log.v(LH,ssetOnGroupClickListener);
Log.v(LH,+ viewListLastSelected.toString());
Log.v(LH
+((TextView的)viewListLastSelected).getText()); }赶上(例外五){
Log.v(LH,ERROR @的onCreate:+ e.toString());
}
返回false;
}
}); elvForDialog.setOnChildClickListener(新OnChildClickListener(){ 公共布尔onChildClick(ExpandableListView为arg0,ARG1查看,
INT ARG2,诠释ARG3,长ARG4)抛出的RuntimeException { currentChapt = listchapt.get(ARG2)的ToString();
currentSubchapt = listNames.get(ARG2)获得(参数3)的ToString();
的System.out.println(是它显示子+ currentChapt
+ currentSubchapt);
// createExpandableListViewDialog(1); 在意向=新意图(MainActivity.this,WebContent.class);
in.putExtra(chapterid,currentChapt);
in.putExtra(subchaptid,currentSubchapt);
startActivity(在); elvForDialog.clearChildFocus(viewListLastSelected); 尝试{ viewListLastSelected.setBackgroundDrawable(NULL);
((的TextView)viewListLastSelected).setTextColor(Color.GRAY);
}赶上(例外五){
}
((的TextView)ARG1).setTextColor(Color.WHITE);
viewListLastSelected = ARG1; 返回false;
}
}); elvForDialog.setOnGroupExpandListener(新OnGroupExpandListener(){ 公共无效onGroupExpand(INT groupPosition){
INT LEN = mAdapter.getGroupCount(); 的for(int i = 0; I< LEN,我++){
如果(我!= groupPosition){
elvForDialog.collapseGroup(ⅰ);
}
}
} });
MyAdapter类
公共类MyExpandableListAdapter扩展BaseExpandableListAdapter {
活动活动;
清单<串GT; listGroup =新的ArrayList<串GT;();
清单<名单,LT;弦乐>> listChild =新的ArrayList<名单,LT;弦乐>>(); 私人INT _posGroup = 0;
私人INT _posChild = 0; 公共MyExpandableListAdapter(活动一,表<弦乐基团,名单,LT;名单<串GT;>儿童){
超();
活性=一个;
listGroup =组;
listChild =儿童;
}
公共对象getChild(INT groupPosition,诠释childPosition){
返回listChild.get(groupPosition)获得(childPosition);
}
众长getChildId(INT groupPosition,诠释childPosition){
//Log.v(\"LH,childID的:+ childPosition);
_posGroup = groupPosition;
_posChild = childPosition;
返回childPosition;
}
公众诠释getChildrenCount(INT groupPosition){
返回listChild.get(groupPosition).size(); //行没有51
}
公共查看getChildView(INT groupPosition,诠释childPosition,
布尔isLastChild,查看convertView,父母的ViewGroup){
串串= listChild.get(groupPosition)获得(childPosition);
查看查看= getGenericChildView(字符串); TextView的文本=(TextView的)视图。
如果(this._posGroup == groupPosition&放大器;&安培;
this._posChild == childPosition)
{
text.setTextColor(Color.rgb(85,85,85));
text.setTypeface(NULL,Typeface.BOLD);
}
view.setBackgroundResource(R.drawable.menu_submenubg); 返回视图。
}
//组方法存根 公共对象getGroup(INT groupPosition){
返回listGroup.get(groupPosition);
} 公众诠释getGroupCount(){
返回listGroup.size();
}
众长getGroupId(INT groupPosition){
//Log.v(\"LH,组ID:+ groupPosition);
返回groupPosition;
}
公共查看getGroupView(INT groupPosition,布尔isExpanded,
查看convertView,父母的ViewGroup){
串串= listGroup.get(groupPosition);
查看结果= getGenericGroupView(字符串);
result.setBackgroundResource(R.drawable.menu_hoverbg);
返回结果;
} 公众的TextView getGenericChildView(String s)将{
AbsListView.LayoutParams LP =新AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,0);
lp.height = 73; // 73,50
TextView的文本=新的TextView(活动);
text.setLayoutParams(LP);
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
text.setPadding(50,0,0,0);
text.setTextColor(Color.rgb(85,85,85));
text.setTypeface(NULL,Typeface.BOLD);
text.setTextSize(15);
text.setText(多个);
返回文本;
}
公众的TextView getGenericGroupView(String s)将{
AbsListView.LayoutParams LP =新AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,0);
lp.height = 60;
TextView的文本=新的TextView(活动);
text.setLayoutParams(LP);
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
text.setPadding(20,0,0,0);
text.setTextSize(20);
text.setTextColor(Color.WHITE);
text.setText(多个);
返回文本;
}
公共布尔hasStableIds(){
返回false;
}
公共布尔isChildSelectable(INT groupPosition,诠释childPosition){
返回true;
}
感谢提前..
修改:
DBHelper
公共类DBHelper扩展SQLiteOpenHelper {
公众诠释GetCursor;
私人语境myContext;
公共字符串DB_PATH =数据/数据/ com.redcaso.mia /数据库/; //路径 公共静态字符串DB_NAME =Test.sqlite; //你的数据库名称
静态字符串ASSETS_DB_FOLDER =DB;
私人SQLiteDatabase分贝; 公共DBHelper(上下文的背景下){
超级(上下文,DB_NAME,空,2);
如果(DB = NULL&放大器;!&安培; db.isOpen())
关(); this.myContext =背景;
// DB_NAME = DB_NAME; 尝试{
的CreateDatabase();
的openDatabase();
}赶上(IOException异常五){
//的System.out.println(异常创作的数据库:+
// e.getMessage());
e.printStackTrace();
} } 公共无效的CreateDatabase()抛出IOException
布尔dbExist = checkDataBase(); 如果(dbExist){
//的System.out.println(数据库中不存在);
}其他{
this.getReadableDatabase(); 尝试{
copyDatabase();
}赶上(IOException异常五){
抛出新的错误(错误复制数据库);
}
}
} 私人无效copyDatabase()抛出IOException
输入的InputStream = myContext.getAssets()开(DB_NAME)。
字符串outputFileName = DB_PATH + DB_NAME;
OutputStream的输出=新的FileOutputStream(outputFileName); 字节[]缓冲区=新的字节[1024];
INT长;
而((长度= input.read(缓冲液))大于0){
output.write(缓冲液,0,长度);
} //关闭流
output.flush();
output.close();
input.close();
//的System.out.println(DB_NAME +数据库复制!);
} @覆盖
公共无效的onCreate(SQLiteDatabase DB){ } @覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){ } 公共无效的openDatabase()抛出的SQLException {
//打开数据库
字符串mypath中= DB_PATH + DB_NAME;
DB = SQLiteDatabase.openDatabase(mypath中,空,
SQLiteDatabase.OPEN_READWRITE);
} 公共布尔isOpen会(){
如果(DB!= NULL)
返回db.isOpen();
返回false;
} @覆盖
公共同步无效的close(){
如果(DB!= NULL)
db.close();
super.close();
} 私人布尔checkDataBase(){
SQLiteDatabase CHECKDB = NULL;
尝试{
字符串mypath中= DB_PATH + DB_NAME;
CHECKDB = SQLiteDatabase.openDatabase(mypath中,空,
SQLiteDatabase.OPEN_READWRITE); }赶上(例外五){
//数据库开不存在。
} 如果(CHECKDB!= NULL){
checkDB.close();
} 返回CHECKDB!= NULL?真假;
} 公共光标execCursorQuery(SQL字符串){
光标光标= NULL;
尝试{
光标= db.rawQuery(SQL,NULL);
GetCursor = cursor.getCount();
Log.i(内部execCursorQuery尝试,SQL);
}赶上(例外五){
Log.i(内部execCursorQuery异常,e.getMessage());
}
返回游标;
} 公共无效execNonQuery(SQL字符串){
尝试{
db.execSQL(SQL);
// Log.d(SQL,SQL);
}赶上(例外五){
// Log.e(ERR,e.getMessage());
} {最后
// closeDb();
}
} 公众的ArrayList<串GT; GetchapterID(){
SQL字符串=;
//System.out.println(\"Inside数据库);
SQL =选择不同的(ChapterId)的内容; ArrayList的<串GT;名单=新的ArrayList<串GT;();
光标光标= db.rawQuery(SQL,NULL); 尝试{
如果(指针!= NULL){
如果(cursor.moveToFirst()){
做{
list.add(cursor.getString(0));
}
而(cursor.moveToNext());
}
如果(光标=空&放大器;!&放大器;!cursor.isClosed()){
cursor.close();
}
}
其他
Toast.makeText(myContext,在DB没有价值,Toast.LENGTH_SHORT).show();
}
赶上(例外五){
的System.out.println(GetGradeNames():+ E);
}
返回列表;
}
公众的ArrayList<串GT; GetSubchapt(字符串类型){
SQL字符串=;
SQL =选择的内容不同(SubchapterId),其中ChapterId ='+类型+'; ArrayList的<串GT;名单=新的ArrayList<串GT;();
光标光标= db.rawQuery(SQL,NULL); 尝试{
如果(指针!= NULL){
如果(cursor.moveToFirst()){
做{
list.add(cursor.getString(0));
}
而(cursor.moveToNext());
}
如果(光标=空&放大器;!&放大器;!cursor.isClosed()){
cursor.close();
}
}
其他
Toast.makeText(myContext,没有财产的DB,Toast.LENGTH_SHORT).show();
}
赶上(例外五){
的System.out.println(GetLevelNames():+ E);
}
返回列表;
}
公众的ArrayList<串GT;的GetName(章字符串,字符串subchapt){
SQL字符串=;
SQL ='+ subchapt +和ChapterId ='+章+',从那里SubchapterId =内容选择不同的(文件名)'; ArrayList的<串GT;名单=新的ArrayList<串GT;();
光标光标= db.rawQuery(SQL,NULL);
//list.add(\"All主题 - 整个+级);
尝试{
如果(指针!= NULL){
如果(cursor.moveToFirst()){
做{
list.add(cursor.getString(0));
}
而(cursor.moveToNext());
}
如果(光标=空&放大器;!&放大器;!cursor.isClosed()){
cursor.close();
}
}
其他
Toast.makeText(myContext,在DB没有地址,Toast.LENGTH_SHORT).show();
}
赶上(例外五){
//System.out.println(GetBasicTopicNames():+ E);
}
返回列表;
}
您错了存储你的孩子,你不必每次添加新的儿童时重新创建您的适配器。
下面是一些修正,让您的code功能:
更改适配器的构造函数,所以它只能接收活动和组:
公共MyExpandableListAdapter(活动一,表<弦乐基团){
this.activity =一个;
this.listGroup =组;
}
更改您的孩子列表的类型,因此您可以通过groupPosition访问它们,就像这样:
SparseArray<名单,LT;弦乐>> listChild =新SparseArray<名单,LT;弦乐>>();
现在创建它增加了儿童的适配器内部的新方法:
公共addChildren(INT groupPosition,列表与LT;弦乐>儿童){
this.listChild.put(groupPosition,子女);
}
现在该适配器的照顾,解决您的OnGroupClick事件。不是每次都设置新的适配器,添加或删除的儿童(根据是否已存在的儿童):
expList.setOnGroupClickListener(新ExpandableListView.OnGroupClickListener(){
@覆盖
公共布尔onGroupClick(ExpandableListView父视图V,INT groupPosition,长ID){
如果(mAdapter.getChildrenCount == 0){
dbHelper.openDataBase();
孩子= dbHelper.GetSubchapt(POS)
dbHelper.close();
mAdapter.addChildren(groupPosition,子女);
}
其他
mAdapter.removeChildren(groupPosition);
返回false;
}
});
群组相关事件现在已修正。剩下的就是调整getChildrenCount方法,这样就不会抛出NPE:
公众诠释getChildrenCount(INT groupPosition){
如果(listChild.get(groupPosition)== NULL){
Log.e(mAdapter.getChildrenCount,试图访问组的+
groupPosition +尚未被添加儿童!);
返回0;
}
其他
返回listChild.get(groupPosition).size();
}
发表任何可能出现进一步的错误,因为这code未与您的设置进行测试。
I found a lot of question about this problem, but I can't fix it. I have spend a whole day i couldn't solve. I rechecked my for
loop also
If i have parent value 7 if i click on 4th parent its expands and again i click on 2nd parent its expends and working fine. but if i click 5th or 6th or 7th parent i am getting this error.. i don't know where i am wrong in my code..
LogCat :
07-12 11:56:24.504: E/AndroidRuntime(3548): FATAL EXCEPTION: main
07-12 11:56:24.504: E/AndroidRuntime(3548): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
07-12 11:56:24.504: E/AndroidRuntime(3548): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
07-12 11:56:24.504: E/AndroidRuntime(3548): at java.util.ArrayList.add(ArrayList.java:143)
07-12 11:56:24.504: E/AndroidRuntime(3548): at android.widget.ExpandableListConnector.expandGroup(ExpandableListConnector.java:685)
07-12 11:56:24.504: E/AndroidRuntime(3548): at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:562)
07-12 11:56:24.504: E/AndroidRuntime(3548): at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:522)
07-12 11:56:24.504: E/AndroidRuntime(3548): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2812)
07-12 11:56:24.504: E/AndroidRuntime(3548): at android.widget.AbsListView$1.run(AbsListView.java:3571)
07-12 11:56:24.504: E/AndroidRuntime(3548): at android.os.Handler.handleCallback(Handler.java:725)
07-12 11:56:24.504: E/AndroidRuntime(3548): at android.os.Handler.dispatchMessage(Handler.java:92)
07-12 11:56:24.504: E/AndroidRuntime(3548): at android.os.Looper.loop(Looper.java:153)
07-12 11:56:24.504: E/AndroidRuntime(3548): at android.app.ActivityThread.main(ActivityThread.java:5297)
07-12 11:56:24.504: E/AndroidRuntime(3548): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 11:56:24.504: E/AndroidRuntime(3548): at java.lang.reflect.Method.invoke(Method.java:511)
07-12 11:56:24.504: E/AndroidRuntime(3548): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-12 11:56:24.504: E/AndroidRuntime(3548): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-12 11:56:24.504: E/AndroidRuntime(3548): at dalvik.system.NativeStart.main(Native Method)
Java
mAdapter = new MyExpandableListAdapter(MainActivity.this, listchapt,
null);
elvForDialog.setIndicatorBounds(width - 20, width);// 130
elvForDialog.setAdapter(mAdapter);
elvForDialog.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView arg0, View arg1,
int arg2, long arg3) throws RuntimeException {
String pos = (String)mAdapter.getGroup(arg2);
System.out.println("position" + pos);
List<String> children = new ArrayList<String>();
children.clear();
listNames.clear();
for (int i = 0; i < listchapt.size(); i++) {
dbHelper.openDataBase();
children = dbHelper.GetSubchapt(pos);
dbHelper.close();
listNames.add(children); // line no.143
}
System.out.println("position" + pos);
Display newDisplay = getWindowManager().getDefaultDisplay();
int width = newDisplay.getWidth();
mAdapter = new MyExpandableListAdapter(MainActivity.this,
listchapt, listNames);
elvForDialog.setIndicatorBounds(width - 20, width);// 130
elvForDialog.setAdapter(mAdapter);
System.out.println("explist" + listSubchapt);
try {
Log.v("LH", "ssetOnGroupClickListener");
Log.v("LH", "" + viewListLastSelected.toString());
Log.v("LH",
"" + ((TextView) viewListLastSelected).getText());
} catch (Exception e) {
Log.v("LH", "ERROR@onCreate: " + e.toString());
}
return false;
}
});
elvForDialog.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView arg0, View arg1,
int arg2, int arg3, long arg4) throws RuntimeException {
currentChapt = listchapt.get(arg2).toString();
currentSubchapt = listNames.get(arg2).get(arg3).toString();
System.out.println("Yes it shows child " + currentChapt
+ currentSubchapt);
// createExpandableListViewDialog(1);
Intent in = new Intent(MainActivity.this, WebContent.class);
in.putExtra("chapterid", currentChapt);
in.putExtra("subchaptid", currentSubchapt);
startActivity(in);
elvForDialog.clearChildFocus(viewListLastSelected);
try {
viewListLastSelected.setBackgroundDrawable(null);
((TextView) viewListLastSelected).setTextColor(Color.GRAY);
} catch (Exception e) {
}
((TextView) arg1).setTextColor(Color.WHITE);
viewListLastSelected = arg1;
return false;
}
});
elvForDialog.setOnGroupExpandListener(new OnGroupExpandListener() {
public void onGroupExpand(int groupPosition) {
int len = mAdapter.getGroupCount();
for (int i = 0; i < len; i++) {
if (i != groupPosition) {
elvForDialog.collapseGroup(i);
}
}
}
});
MyAdapter class
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
Activity activity;
List<String> listGroup = new ArrayList<String>();
List<List<String>> listChild = new ArrayList<List<String>>();
private int _posGroup = 0;
private int _posChild = 0;
public MyExpandableListAdapter(Activity a, List<String> group, List<List<String>> children){
super();
activity = a;
listGroup = group;
listChild = children;
}
public Object getChild(int groupPosition, int childPosition) {
return listChild.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
//Log.v("LH", "ChildID: " + childPosition);
_posGroup = groupPosition;
_posChild = childPosition;
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return listChild.get(groupPosition).size(); //line no 51
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String string = listChild.get(groupPosition).get(childPosition);
View view = getGenericChildView(string);
TextView text = (TextView) view;
if(this._posGroup==groupPosition &&
this._posChild==childPosition)
{
text.setTextColor(Color.rgb(85, 85, 85));
text.setTypeface(null, Typeface.BOLD);
}
view.setBackgroundResource(R.drawable.menu_submenubg);
return view;
}
//group method stub
public Object getGroup(int groupPosition) {
return listGroup.get(groupPosition);
}
public int getGroupCount() {
return listGroup.size();
}
public long getGroupId(int groupPosition) {
//Log.v("LH", "GroupID: " + groupPosition);
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String string = listGroup.get(groupPosition);
View result=getGenericGroupView(string);
result.setBackgroundResource(R.drawable.menu_hoverbg);
return result;
}
public TextView getGenericChildView(String s) {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 0);
lp.height=73; //73,50
TextView text = new TextView(activity);
text.setLayoutParams(lp);
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
text.setPadding(50, 0, 0, 0);
text.setTextColor(Color.rgb(85, 85, 85));
text.setTypeface(null, Typeface.BOLD);
text.setTextSize(15);
text.setText(s);
return text;
}
public TextView getGenericGroupView(String s) {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 0);
lp.height=60;
TextView text = new TextView(activity);
text.setLayoutParams(lp);
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
text.setPadding(20, 0, 0, 0);
text.setTextSize(20);
text.setTextColor(Color.WHITE);
text.setText(s);
return text;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
Thank in advance..
Edit :DBHelper
public class DBHelper extends SQLiteOpenHelper {
public int GetCursor;
private Context myContext;
public String DB_PATH = "data/data/com.redcaso.mia/databases/"; // path
public static String DB_NAME = "Test.sqlite";// your database name
static String ASSETS_DB_FOLDER = "db";
private SQLiteDatabase db;
public DBHelper(Context context) {
super(context, DB_NAME, null, 2);
if (db != null && db.isOpen())
close();
this.myContext = context;
//DB_NAME = db_name;
try {
createDataBase();
openDataBase();
} catch (IOException e) {
// System.out.println("Exception in creation of database : "+
// e.getMessage());
e.printStackTrace();
}
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
// System.out.println("Database Exist");
} else {
this.getReadableDatabase();
try {
copyDatabase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private void copyDatabase() throws IOException {
InputStream input = myContext.getAssets().open(DB_NAME);
String outputFileName = DB_PATH + DB_NAME;
OutputStream output = new FileOutputStream(outputFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
// Close the streams
output.flush();
output.close();
input.close();
// System.out.println(DB_NAME + "Database Copied !");
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
public boolean isOpen() {
if (db != null)
return db.isOpen();
return false;
}
@Override
public synchronized void close() {
if (db != null)
db.close();
super.close();
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (Exception e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
public Cursor execCursorQuery(String sql) {
Cursor cursor = null;
try {
cursor = db.rawQuery(sql, null);
GetCursor = cursor.getCount();
Log.i("Inside execCursorQuery try", sql);
} catch (Exception e) {
Log.i("Inside execCursorQuery exception", e.getMessage());
}
return cursor;
}
public void execNonQuery(String sql) {
try {
db.execSQL(sql);
// Log.d("SQL", sql);
} catch (Exception e) {
// Log.e("Err", e.getMessage());
} finally {
// closeDb();
}
}
public ArrayList<String> GetchapterID(){
String sql="";
//System.out.println("Inside Database");
sql="select distinct(ChapterId) from contents";
ArrayList<String> list = new ArrayList<String>();
Cursor cursor=db.rawQuery(sql, null);
try{
if(cursor != null){
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
else
Toast.makeText(myContext, "No values in DB", Toast.LENGTH_SHORT).show();
}
catch(Exception e){
System.out.println("GetGradeNames(): " +e);
}
return list;
}
public ArrayList<String> GetSubchapt(String type){
String sql="";
sql="select distinct(SubchapterId) from contents where ChapterId='"+type+"'";
ArrayList<String> list = new ArrayList<String>();
Cursor cursor=db.rawQuery(sql, null);
try{
if(cursor != null){
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
else
Toast.makeText(myContext, "No Property in DB", Toast.LENGTH_SHORT).show();
}
catch(Exception e){
System.out.println("GetLevelNames(): " +e);
}
return list;
}
public ArrayList<String> GetName(String chapter,String subchapt){
String sql="";
sql="select distinct(filename) from contents where SubchapterId='"+subchapt+"' and ChapterId='"+chapter+"'";
ArrayList<String> list = new ArrayList<String>();
Cursor cursor=db.rawQuery(sql, null);
//list.add("All Topics - Entire "+grade);
try{
if(cursor != null){
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
else
Toast.makeText(myContext, "No Address in DB", Toast.LENGTH_SHORT).show();
}
catch(Exception e){
//System.out.println("GetBasicTopicNames(): " +e);
}
return list;
}
You are storing your children wrong and you don't have to re-create your adapter every time you add new children.
Here are some fixes to get your code functional:
Change your adapters constructor to, so it receives only activity and groups:
public MyExpandableListAdapter(Activity a, List<String> group){
this.activity = a;
this.listGroup = group;
}
Change the type of your children list, so you can access them by groupPosition, like this:
SparseArray<List<String>> listChild = new SparseArray<List<String>>();
Now create a new method inside the adapter which adds children:
public addChildren(int groupPosition, List<String> children) {
this.listChild.put(groupPosition, children);
}
Now that the adapter is taken care of, fix your OnGroupClick event. Instead of setting a new adapter each time, add or remove the children (based on whether the children already exist):
expList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if (mAdapter.getChildrenCount == 0) {
dbHelper.openDataBase();
children = dbHelper.GetSubchapt(pos);
dbHelper.close();
mAdapter.addChildren(groupPosition, children);
}
else
mAdapter.removeChildren(groupPosition);
return false;
}
});
GroupClick event has been fixed now. What's left is to adjust your getChildrenCount method so it wouldn't throw a NPE:
public int getChildrenCount(int groupPosition) {
if (listChild.get(groupPosition) == null) {
Log.e("mAdapter.getChildrenCount", "Tried to access group's "+
groupPosition+" children which have not yet been added!");
return 0;
}
else
return listChild.get(groupPosition).size();
}
Post any further errors that might occur, as this code is not tested with your setup.
这篇关于IndexOutOfBoundException在ExpandableListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!