Top 6 or 7 item of groupview of expandable listview do not refresh - expandablelistview

I want to achieve a function which when I click the groupview item of expandable listview the item will appear a new imageview. If I click the No.9 or No.10 of the listview, evertthing goes fine while if I click the top 6 or 7(depand on the mobilephone) item of the listview, there would be nothing happen.So I wonder how to fix this problem.
The adapter of the expandable listview is below:
class RouteLineAdapter extends BaseExpandableListAdapter {
boolean isMt = false;
List<HashMap<String, String>> busRouteListInner = new ArrayList<HashMap<String, String>>();
RouteLineAdapter(boolean isMt) {
this.isMt = isMt;
if (isMt) busRouteListInner = busRouteListMt;
else busRouteListInner = busRouteListMo;
}
#Override
public Object getGroup(int groupPosition) {
if (groupPosition < busRouteListInner.size())
return busRouteListInner.get(groupPosition).get("station_name");
else return "";
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = convertView;
final Animation refreshAnimation = AnimationUtils.loadAnimation(RealTime.this, R.anim.refresh_progress);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.member_listview, null);
}
TextView busNo = (TextView) view.findViewById(R.id.busNumMember);
TextView stationName = (TextView) view
.findViewById(R.id.stationNameMember);
final ImageView refreshImageView = (ImageView) view.findViewById(R.id.refresh_businfo);
final ImageView collectImageView = (ImageView) view.findViewById(R.id.collect_businfo);
String tempSearchStationId = searchStationId;
if (!isMt && (groupCountMt > 0) && ((BasicUtils.convertStringToInt(searchStationId) - 1) > groupCountMt)) {
tempSearchStationId = BasicUtils.convertStringToInt(searchStationId) - groupCountMt + "";
}
if (refreshState == 1 && (BasicUtils.convertStringToInt(tempSearchStationId) - 1 == groupPosition)) {
refreshImageView.startAnimation(refreshAnimation);
} else if (refreshState == 0) {
refreshImageView.clearAnimation();
}
if (refreshState == 2 && (BasicUtils.convertStringToInt(tempSearchStationId) - 1 == groupPosition)) {
//it execute here,but not show imageview at the top item
Logs.d("is execuated");
refreshImageView.setVisibility(View.VISIBLE); refreshImageView.startAnimation(refreshAnimation);
refreshState = 3;
}
return view;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
try {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.member_childitem, null);
}
globalView = convertView;
if (groupPosition != searchLocation) {
Logs.d(busDataList.get(0).get("station_count_remain") + " remain");
getBusInfoDetail(convertView);
searchLocation = groupPosition;
}
} catch (Exception e) {
e.printStackTrace();
Logs.e(e.getMessage(), "");
}
return convertView;
}
}
And in the handler ,the program do
routeLineAdapterMt.notifyDataSetChanged();
routeLineAdapterMt.notifyDataSetInvalidated();
to let the listview refresh

Related

Item List not Clickable

Item list instantiates but the items but click event (toast) doesnt occur when clicked.
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawerLayout;
private NavigationView navigationView;
Toolbar toolbar;
FragmentTransaction fragmentTransaction;
FragmentManager fragmentManager;
Fragment fragment;
FrameLayout frameLayout;
ListView listView;
String[] workoutsRoutines = {"Upper Body", "Lower body"};
int[] workoutsBackgrounds = {R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground};
int count;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
navigationView.bringToFront();
navigationView.setNavigationItemSelectedListener(this);
toolbar = findViewById(R.id.toolbar);
toolbar.setTitle(null);
toolbar.setNavigationIcon(R.drawable.ic_android_black_24dp);
item list click event
listView = findViewById(R.id.listview);
CustomAdapter customAdapter = new CustomAdapter();
listView.setAdapter(customAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
Toast.makeText(MainActivity.this, "succ", Toast.LENGTH_SHORT).show();
}
});
toolbar.setNavigationOnClickListener(v -> {
drawerLayout.openDrawer(GravityCompat.START);
Toast.makeText(MainActivity.this, "succ", Toast.LENGTH_SHORT).show();
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
}
if (fragment != null) {
count = fragmentManager.getBackStackEntryCount();
for (int i = 0; i < count; i++) {
fragmentManager.popBackStack();
drawerLayout.closeDrawer(GravityCompat.START);
}
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction().remove(fragment);
fragmentTransaction.commit();
}
toolbar.setNavigationIcon(R.drawable.ic_android_black_24dp);
});
}
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else super.onBackPressed();
count = fragmentManager.getBackStackEntryCount();
for (int i = 0; i < count; i++) {
fragmentManager.popBackStack();
}
toolbar.setNavigationIcon(R.drawable.ic_android_black_24dp);
}
private class CustomAdapter extends BaseAdapter {
#Override
public int getCount() {
return workoutsBackgrounds.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View convertView, ViewGroup parent) {
View view1 = getLayoutInflater().inflate(R.layout.rowdata, null);
TextView name = view1.findViewById((R.id.workouts));
ImageView image = view1.findViewById((R.id.background));
name.setText(workoutsRoutines[i]);
image.setImageResource(workoutsBackgrounds[i]);
return view1;
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_profile:
fragment = new profileFragment();
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction().replace(R.id.frameLayout, fragment).addToBackStack(null);
fragmentTransaction.commit();
toolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_back_ios_24);
break;
case R.id.nav_custom_workouts:
fragment = new customWorkoutFragment();
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction().replace(R.id.frameLayout, new customWorkoutFragment()).addToBackStack(null);
fragmentTransaction.commit();
toolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_back_ios_24);
break;
}
item.setChecked(true);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
needed to bring itemlist view to front

Java NullPointerException while invoking interface method java.lang.Object[] java.util.Collection.toArray()

This is my source code:
public class ListorderActivity extends AppCompatActivity {
TextView lblTotal;
ListView listView;
MealClass mealDetails;
MealListDataAdapter mealAdapter;
ArrayList<OrderClassDetail> orderlist;
float totalPrice;
private class MealListDataAdapter extends ArrayAdapter<OrderClassDetail> {
int layoutResID;
ArrayList<OrderClassDetail> mealList = new ArrayList<>();
private class ViewHolder {
Button btndelete;
ImageView imgmeal;
TextView lblName;
TextView lblPrice;
TextView lblQuantity;
TextView lblSaltSpicy;
TextView lblSoup;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
String soupStr;
View view = convertView;
if (convertView == null) {
view = ((LayoutInflater) ListorderActivity.this.getSystemService("layout_inflater")).inflate(this.layoutResID, null);
holder = new ViewHolder();
holder.lblName = (TextView) view.findViewById(R.id.txtOrderMealName);
holder.lblPrice = (TextView) view.findViewById(R.id.txtOrderMealPrice);
holder.lblSoup = (TextView) view.findViewById(R.id.txtOrderMealSoup);
holder.lblSaltSpicy = (TextView) view.findViewById(R.id.txtSaltSpicy);
holder.lblQuantity = (TextView) view.findViewById(R.id.txtOrderMealQuantity);
holder.imgmeal = (ImageView) view.findViewById(R.id.mealOrder);
holder.btndelete = (Button) view.findViewById(R.id.delete);
view.setTag(holder);
holder.btndelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mealAdapter.remove((OrderClassDetail) ((Button) v).getTag());
mealAdapter.notifyDataSetChanged();
}
});
} else {
holder = (ViewHolder) view.getTag();
}
try {
OrderClassDetail mealObj = (OrderClassDetail) this.mealList.get(position);
int mealOrderNo = mealObj.getMealNo();
String str = BuildConfig.FLAVOR;
if (mealObj.isHaveSoup()) {
soupStr = "with Soup";
} else {
soupStr = "no Soup";
}
holder.lblName.setText(ListorderActivity.this.mealDetails.getMealName(mealOrderNo));
TextView textView = holder.lblPrice;
StringBuilder sb = new StringBuilder();
sb.append("Price/meal: ");
sb.append(ListorderActivity.this.mealDetails.getMealPrice(mealOrderNo));
textView.setText(sb.toString());
TextView textView2 = holder.lblSoup;
StringBuilder sb2 = new StringBuilder();
sb2.append("Adds On: ");
sb2.append(soupStr);
textView2.setText(sb2.toString());
TextView textView3 = holder.lblSaltSpicy;
StringBuilder sb3 = new StringBuilder();
sb3.append("Salt: ");
sb3.append(mealObj.getSaltPercent());
sb3.append("% : Spice: ");
sb3.append(mealObj.getSpicyPercent());
sb3.append("%");
textView3.setText(sb3.toString());
TextView textView4 = holder.lblQuantity;
StringBuilder sb4 = new StringBuilder();
sb4.append("No. of Order(s): ");
sb4.append(mealObj.getOrderQuantity());
textView4.setText(sb4.toString());
holder.imgmeal.setBackgroundResource(ListorderActivity.this.mealDetails.getMealImage(mealOrderNo));
holder.btndelete.setTag(mealObj);
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
public MealListDataAdapter(Context context, int resourceLayoutID,ArrayList<OrderClassDetail> listObj) {
super(context, resourceLayoutID, listObj);
layoutResID = resourceLayoutID;
mealList.addAll(listObj);
}
public void addAll(ArrayList<OrderClassDetail> obj) {
mealList.clear();
mealList.addAll(obj);
}
public void remove(OrderClassDetail object) {
super.remove(object);
mealList.remove(object);
ListorderActivity.this.totalPrice = 0.0f;
Iterator it = this.mealList.iterator();
while (it.hasNext()) {
OrderClassDetail orbobj = (OrderClassDetail) it.next();
int mealOrderNo = orbobj.getMealNo();
int quant = orbobj.getOrderQuantity();
ListorderActivity.this.totalPrice += ListorderActivity.this.mealDetails.getMealPrice(mealOrderNo) * ((float) quant);
TextView textView = ListorderActivity.this.lblTotal;
StringBuilder sb = new StringBuilder();
sb.append("Total Amount: Php ");
sb.append(String.format("%.2f", new Object[]{Float.valueOf(ListorderActivity.this.totalPrice)}));
textView.setText(sb.toString());
}
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listorder_activityy);
mealDetails = new MealClass();
orderlist = (ArrayList) getIntent().getSerializableExtra("oderList");
lblTotal = (TextView) findViewById(R.id.txtTotalPrice);
listView = (ListView) findViewById(R.id.listView);
mealAdapter = new ListorderActivity.MealListDataAdapter(getBaseContext(), R.layout.custom_listview_layout,this.orderlist);
listView.setAdapter(this.mealAdapter);
Iterator it = orderlist.iterator();
while (it.hasNext()) {
OrderClassDetail orbobj = (OrderClassDetail) it.next();
totalPrice += mealDetails.getMealPrice(orbobj.getMealNo()) * ((float) orbobj.getOrderQuantity());
TextView textView = lblTotal;
StringBuilder sb = new StringBuilder();
sb.append("Total Amount: Php ");
sb.append(String.format("%,.2f", new Object[]{Float.valueOf(this.totalPrice)}));
textView.setText(sb.toString());
}
}
My stack trace:
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
at java.util.ArrayList.addAll(ArrayList.java:188)
at com.example.myhappymeal.ListorderActivity$MealListDataAdapter.<init>(ListorderActivity.java:120)
at com.example.myhappymeal.ListorderActivity.onCreate(ListorderActivity.java:158)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
at android.app.ActivityThread.access$800(ActivityThread.java:153) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5293) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

How to save the content of custom array class and adapter in list view save into text in android

I need your help I am new in android programming. How can I save all the content in the list view save into text file this is my code of try to save the file but the problem is how can i put the listview array list to get the data i don't know how to put it where to put it please help how to do it to save the content of my listview
Button code:
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
for (int i = 0; i < ChatBubbles.length; i++) {
myOutWriter.append(ChatBubbles[i] +"\n");
}
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
Chatbubble class:
package com.example.ezminute;
public class ChatBubble {
private String content;
private boolean myMessage;
public ChatBubble(String content, boolean myMessage) {
this.content = content;
this.myMessage = myMessage;
}
public String getContent() {
return content;
}
public boolean myMessage() {
return myMessage;
}
}
MessageAdapter:
package com.example.ezminute;
public class MessageAdapter extends ArrayAdapter<ChatBubble> {
private Activity activity;
private List<ChatBubble> messages;
public MessageAdapter(Activity context, int resource, List<ChatBubble> objects) {
super(context, resource, objects);
this.activity = context;
this.messages = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
int layoutResource = 0; // determined by view type
ChatBubble ChatBubble = getItem(position);
int viewType = getItemViewType(position);
if (ChatBubble.myMessage()) {
layoutResource = R.layout.left_chat_bubble;
} else {
layoutResource = R.layout.right_chat_bubble;
}
if (convertView != null) {
holder = (ViewHolder) convertView.getTag();
} else {
convertView = inflater.inflate(layoutResource, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
//set message content
holder.msg.setText(ChatBubble.getContent());
return convertView;
}
#Override
public int getViewTypeCount() {
// return the total number of view types. this value should never change
// at runtime. Value 2 is returned because of left and right views.
return 2;
}
#Override
public int getItemViewType(int position) {
// return a value between 0 and (getViewTypeCount - 1)
return position % 2;
}
private class ViewHolder {
private TextView msg;
public ViewHolder(View v) {
msg = (TextView) v.findViewById(R.id.txt_msg);
}
}
}

messed up dynamic radio buttons in ArrayAdapter

when i run my app, i get much more radiobuttons than i need. It seems the radiobuttons repeat themselves in the same group. I don't really understand what is is going on. Here is my custom ArrayAdapter. I would like to know the problem here
public class QuestionsListAdapter extends ArrayAdapter<QuestionProperties> {
List<QuestionProperties> list;
Context test;
public QuestionsListAdapter(Context context, int resource, List<QuestionProperties> list2) {
super(context,resource,list2);
test = context;
list =list2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
final RadioButton[] rB;
RadioHolder holder = new RadioHolder();
view= convertView;
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
if(view == null)
{
LayoutInflater inflator = ((Activity) test).getLayoutInflater();
view = inflator.inflate(R.layout.question_list_row, null);
holder.questionTV = (TextView) view.findViewById(R.id.qTextView);
holder.radiogroup = (RadioGroup) view.findViewById(R.id.radio_group);
view.setTag(holder);
}
else{
//view = convertView;
holder = (RadioHolder) view.getTag();
}
holder.questionTV.setText(String.valueOf(list.get(position).getQuestionNo())+"."+" " + list.get(position).getQuestion());
rB=new RadioButton[list.get(position).possibleAns.length];
for(int count = 0; count<(list.get(position).possibleAns.length);count++)
{
rB[count]= new RadioButton(test);
rB[count].setId(count);
rB[count].setText(list.get(position).possibleAns[count]);
layoutParams.weight=1.0f;
layoutParams.setMargins(15, 0, 5, 10);
rB[count].setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
String a = String.valueOf(v.getId());
Toast.makeText(QActivity.context, "Radio Button "+ a,Toast.LENGTH_SHORT).show();
}
});
holder.radiogroup.addView(rB[count],layoutParams);
}
return view;
}
static class RadioHolder {
protected TextView questionTV;
protected RadioGroup radiogroup;
}
Finally after some hacks i solved it! i removed all the radio buttons in the else clause.
The solution..
public class QuestionsListAdapter extends ArrayAdapter<QuestionProperties> {
List<QuestionProperties> list;
RadioButton rB;
Context test;
RadioHolder holder;
String chkBtn;
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
public QuestionsListAdapter(Context context, int resource, List<QuestionProperties> list2) {
super(context,resource,list2);
test = context;
list =list2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
holder = new RadioHolder();
view= convertView;
Log.v("ConvertView", String.valueOf(position));
if(view == null)
{
LayoutInflater inflator = ((Activity) test).getLayoutInflater();
view = inflator.inflate(R.layout.question_list_row, parent,false);
holder.questionTV = (TextView) view.findViewById(R.id.qTextView);
holder.radiogroup = (RadioGroup) view.findViewById(R.id.radio_group);
//holder.radiogroup.check(list.get(position).getSelectedAns());
view.setTag(holder);
//((RadioHolder) view.getTag()).radiogroup.setTag(list.get(position));
Log.v("holder setTag", String.valueOf(position));
}
else{
view = convertView;
holder = (RadioHolder)view.getTag();
//((RadioHolder)view.getTag()).radiogroup.getTag();
holder.radiogroup.removeAllViews();
}
holder.questionTV.setText(String.valueOf(list.get(position).getQuestionNo())+"."+" " + list.get(position).getQuestion());
configureRadioButtons(position);
chkBtn = String.valueOf(list.get(position).getSelectedAns());
holder.radiogroup.check(Integer.valueOf(chkBtn));
return view;
}
static class RadioHolder {
protected TextView questionTV;
protected RadioGroup radiogroup;
}
public void configureRadioButtons(int pos){
final int position = pos;
//rB=new RadioButton(test);
for(int count = 0; count<(list.get(position).possibleAns.length);count++)
{
rB= new RadioButton(test);
rB.setId(count);
rB.setText(list.get(position).possibleAns[count]);
layoutParams.weight=1.0f;
layoutParams.setMargins(15, 0, 5, 10);
holder.radiogroup.addView(rB,layoutParams);
rB.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
String a = String.valueOf(v.getId());
list.get(position).setSelectedAns(v.getId());
chkBtn = String.valueOf(list.get(position).getSelectedAns());
Toast.makeText(QActivity.context, "Radio Button "+ a,Toast.LENGTH_SHORT).show();
}
});
rB.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
}
});
holder.radiogroup.clearCheck();
Log.v("rB added to radiogroup", String.valueOf(position));
}
}

controlling buttons in different group of ExpandableListView

I have a program in android and i have a button on the group of expandableListView,I have 4 groups, so i need to control those 4 buttons individually, different activities for each.
Can anyone help me?
Code for MainActivity:
public class MainActivity extends Activity {
final Context context = this;
private static final String[][] data = {{" "},{"a1","a2"},{"s1","s2","s3"},{"t1","t2","t3"}};
private ExpandableListView expandableListView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = (ExpandableListView)findViewById(R.id.lvExp);
expandableListView.setAdapter(new ExpandableListAdapter(context, this, data));
}
Here`s my code for Adapter:
class ExpandableListAdapter extends BaseExpandableListAdapter {
public Context context;
CheckBox checkBox;
private LayoutInflater vi;
private String[][] data;
int _objInt;
public static Boolean checked[] = new Boolean[1];
HashMap<Long,Boolean> checkboxMap = new HashMap<Long,Boolean>();
private static final int GROUP_ITEM_RESOURCE = R.layout.list_group;
private static final int CHILD_ITEM_RESOURCE = R.layout.list_item;
public String []check_string_array;
public ExpandableListAdapter(Context context, Activity activity, String[][] data) {
this.data = data;
this.context = context;
vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
_objInt = data.length;
check_string_array = new String[_objInt];
popolaCheckMap();
}
public void popolaCheckMap(){
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
String buffer = null;
for(int i=0; i<_objInt; i++){
buffer = settings.getString(String.valueOf((int)i),"false");
if(buffer.equals("false"))
checkboxMap.put((long)i, false);
else checkboxMap.put((long)i, true);
}
}
public class CheckListener implements OnCheckedChangeListener{
long pos;
public void setPosition(long p){
pos = p;
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Log.i("checkListenerChanged", String.valueOf(pos)+":"+String.valueOf(isChecked));
checkboxMap.put(pos, isChecked);
if(isChecked == true) check_string_array[(int)pos] = "true";
else check_string_array[(int)pos] = "false";
// save checkbox state of each group
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor preferencesEditor = settings.edit();
preferencesEditor.putString(String.valueOf((int)pos), check_string_array[(int)pos]);
preferencesEditor.commit();
}
}
public String getChild(int groupPosition, int childPosition) {
return data[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return data[groupPosition].length;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v = convertView;
String child = getChild(groupPosition, childPosition);
int id_res = 0;
if(groupPosition == 0){
if(childPosition == 0) id_res = R.drawable.ic_launcher;
}
else if(groupPosition == 1){
}
else if(groupPosition == 2){
}
else if(groupPosition == 3){
}
if (child != null) {
v = vi.inflate(CHILD_ITEM_RESOURCE, null);
ViewHolder holder = new ViewHolder(v);
holder.text.setText(Html.fromHtml(child));
holder.imageview.setImageResource(id_res);
}
return v;
}
public String getGroup(int groupPosition) {
return "group-" + groupPosition;
}
public int getGroupCount() {
return data.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View v = convertView;
String group = null;
int id_res = 0;
long group_id = getGroupId(groupPosition);
if(group_id == 0){
group = "Power Consumption";
}
else if(group_id == 1){
group = "Appliances";
}
else if(group_id == 2){
group = "Scenes";
}
else if(group_id == 3){
group = "Triggers";
}
if (group != null) {
v = vi.inflate(GROUP_ITEM_RESOURCE, null);
ViewHolder holder = new ViewHolder(v);
holder.text.setText(Html.fromHtml(group));
holder.imageview.setImageResource(id_res);
holder.imageview.setFocusable(false);
CheckListener checkL = new CheckListener();
checkL.setPosition(group_id);
}
return v;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
class ViewHolder {
public TextView text;
public ImageView imageview;
public ViewHolder(View v) {
this.text = (TextView)v.findViewById(R.id.text1);
this.imageview = (ImageView)v.findViewById(R.id.image1);
}
}

Resources