messed up dynamic radio buttons in ArrayAdapter - android-layout

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));
}
}

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

behavior explanation: Using LiveData to update adapter with DiffUtil

I am updating my recyclerview by using LiveData as below:
viewModel = ViewModelProviders.of(getActivity()).get(MyViewModel.class);
viewModel.getPurchaseList().observe(getViewLifecycleOwner(), new Observer<List<ProductsObject>>() {
#Override
public void onChanged(#Nullable List<ProductsObject> productsObjects) {
adapter.submitList(productsObjects);
//adapter.notifyDataSetChanged();
}
});
And I am using a FloatActionButton to change the value of my MutableLiveData as below:
FloatingActionButton fab = view.findViewById(R.id.cart_fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewModel.setPurchasePrice(0, 101.2);
}
});
All the data gets changed and onChanged is called as expected, but it only updates my recyclerview when I enable the adapter.notifyDataSetChanged();
If I create a new ProductsObject inside the FAB and submit a new list, the recyclerview gets updated without calling adapter.notifyDataSetChanged(); as below:
FloatingActionButton fab = view.findViewById(R.id.cart_fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//viewModel.setPurchaseAmount(0, 101.2);
ProductsObject prod = new ProductsObject("6666", 5, 152.2, "new product");
List<ProductsObject> prodList = new ArrayList<>();
prodList.add(prod);
adapter.submitList(prodList);
}
});
I appreciate if anyone could explain why.
Here is my adapter:
public class CartFragAdapter extends RecyclerView.Adapter<CartFragAdapter.CartFragViewHolder> {
private static final String TAG = "debinf PurchaseAdap";
private static final DiffUtil.ItemCallback<ProductsObject> DIFF_CALLBACK = new DiffUtil.ItemCallback<ProductsObject>() {
#Override
public boolean areItemsTheSame(#NonNull ProductsObject oldProduct, #NonNull ProductsObject newProduct) {
Log.i(TAG, "areItemsTheSame: old is "+oldProduct.getCode()+" ; new is "+newProduct.getCode());
return oldProduct.getCode().equals(newProduct.getCode());
}
#Override
public boolean areContentsTheSame(#NonNull ProductsObject oldProduct, #NonNull ProductsObject newProduct) {
Log.i(TAG, "areContentsTheSame: old is "+oldProduct.getPrice()+" ; new is "+newProduct.getPrice());
return oldProduct.getPrice() == newProduct.getPrice();
}
};
private AsyncListDiffer<ProductsObject> differ = new AsyncListDiffer<ProductsObject>(this, DIFF_CALLBACK);
#NonNull
#Override
public CartFragViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_purchase, parent, false);
return new CartFragViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CartFragViewHolder holder, int position) {
final ProductsObject purchaseList = differ.getCurrentList().get(position);
holder.mCode.setText(purchaseList.getCode());
holder.mPrice.setText(String.valueOf(purchaseList.getPrice()));
holder.mDescription.setText(purchaseList.getDescription());
}
#Override
public int getItemCount() {
Log.i(TAG, "getItemCount");
return differ.getCurrentList().size();
}
public void submitList(List<ProductsObject> products){
Log.i(TAG, "submitList: products.size is "+products.size());
differ.submitList(products);
}
public class CartFragViewHolder extends RecyclerView.ViewHolder {
public TextView mCode, mPrice, mDescription;
public CartFragViewHolder(#NonNull View itemView) {
super(itemView);
mCode = (TextView) itemView.findViewById(R.id.item_productCode);
mPrice = (TextView) itemView.findViewById(R.id.item_productPrice);
mDescription = (TextView) itemView.findViewById(R.id.item_productDescription);
}
}
}
And here is my ViewModel:
public class MyViewModel extends ViewModel {
MutableLiveData<List<ProductsObject>> purchaseList = new MutableLiveData<>();
public LiveData<List<ProductsObject>> getPurchaseList() {
return purchaseList;
}
public void setPurchasePrice(int position, double price) {
List<ProductsObject> itemList = purchaseList.getValue();
if (itemList != null && itemList.get(position) != null) {
Log.i("debinf ViewModel", "setPurchaseAmount: "+itemList.get(position).getPrice());
itemList.get(position).setPrice(price);
purchaseList.postValue(itemList);
}
}
}
AsyncListDiffer saves only the reference of the list. This means that if you submit a modified list instead of submitting a new list, AsncListDiffer won't be able to detect any difference because both the previous list and the new list are referencing the same list with the same items.
To fix this you need to create a new list and new item. Change MyViewModel#setPurchasePrice as below:
public void setPurchasePrice(int position, double price) {
List<ProductsObject> itemList = purchaseList.getValue();
if (itemList != null && itemList.get(position) != null) {
List<ProductsObject> newList = new ArrayList<>();
for (int i = 0; i < itemList.size(); i++) {
ProductsObject prevProd = itemList.get(i);
if (i != position) {
newList.add(prevProd);
} else {
ProductsObject newProd = new ProductsObject(..., price, ...);
newList.add(newProd);
}
}
purchaseList.postValue(newList);
}
}

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);
}
}
}

how to Update ListView after deletion

Here I'm using Custom Adapter by implementing listAdapter to Support buttons in the list items. When i click the Delete button it deletes currect row. But i can't able to refresh this listView.
File Structure is MainActivity -> FragmentClass-> this Custom class for the listAdpater.
public class Category_ListView_Custom_Adapter extends BaseAdapter implements ListAdapter{
private Realm realm;
private ArrayList<String> list = new ArrayList<>();
private Context context;
public Category_ListView_Custom_Adapter(ArrayList<String> list, Context context){
this.list = list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int i) {
return list.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(final int i, View view, ViewGroup viewGroup) {
View view1 = view;
realm = Realm.getDefaultInstance();
if(view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.category_listview_row, null);
}
TextView listItemText = (TextView) view.findViewById(R.id.item);
listItemText.setText(list.get(i));
ImageView edit = (ImageView) view.findViewById(R.id.image);
ImageView delete = (ImageView) view.findViewById(R.id.delete);
edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// list.remove(i);
/// Toast.makeText(view.getContext(), "hey "+list.get(i).toString(), Toast.LENGTH_SHORT).show();
// notifyDataSetChanged();
Intent intent = new Intent(context, AddCategory.class);
intent.putExtra("category","expense");
intent.putExtra("subcategory", list.get(i));
context.startActivity(intent);
}
});
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RealmResults<Category> categorylist = realm.where(Category.class).equalTo("Id",i+1).and().equalTo("Subcategory",list.get(i)).findAll();
for (Category category : categorylist) {
realm.beginTransaction();
categorylist.deleteFirstFromRealm();
realm.commitTransaction();
try {
context.notifyAll();
}catch (Exception e){
Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
}
// Expense.call_resume
}
}
});
return view;
}
}
Please Help me. Thank you.
You are .removing from a wrong view. Try to make root List<> static and do Class.list.get(i).remove or something like that.
Also, try to hide a view inside your get view.

Hashmap order inside the expandable listview varies from device to another

Now i'm making an application that it should display a list of courses in an expandable list view and when the user clicks on a child element it opens a new activity with the course details , everything works pretty fine but , when i tested the application through nexus the order of the courses was the same i put inside the hashmap and in the switch case statement but when i tested it through my galaxy not two here it came the disaster the orders in the list were scrambled so the click event got messed up when supposed to open course details for x it opens for y and so on ,,, what should i do ?
THIS IS THE MAIN
public class Courses extends ActionBarActivity {
HashMap<String,List<String>> Courses_castegory;
List<String> Courses_list;
ExpandableListView expandableListView;
MyCourseAdapter myCourseAdapter;
Intent detaledActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_courses);
expandableListView = (ExpandableListView) findViewById(R.id.simple_expandable_list);
Courses_castegory = CourseProvider.getInfo();
Courses_list=new ArrayList<>(Courses_castegory.keySet());//get all the keys from the hashmap
myCourseAdapter = new MyCourseAdapter(this,Courses_castegory,Courses_list);
expandableListView.setAdapter(myCourseAdapter);
expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getBaseContext(), Courses_list.get(groupPosition) + " is collapsed", Toast.LENGTH_SHORT).show();
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//Toast.makeText(getBaseContext(),
//Courses_castegory.get(Courses_list.get(groupPosition)).get(childPosition)+" from "+Courses_list.get(groupPosition)+" is selected",Toast.LENGTH_SHORT).show();
switch(groupPosition) {
case 0:
switch (childPosition) {
case 0:
detaledActivity = new Intent(Courses.this, DetailedCouse.class);
startActivity(detaledActivity);
break;
}
}
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_courses, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
THIS IS THE COURSE PROVIDER
public class CourseProvider {
public static HashMap<String,List<String>> getInfo(){
HashMap<String,List<String>>CourseDetails = new HashMap<String,List<String>>();
List<String> CourseType1 = new ArrayList<>();
CourseType1.add("English For Work");
List<String> CourseType2= new ArrayList<>();
CourseType2.add("General English For Work");
CourseType2.add("Intensive English For Work");
List<String> CourseType3 = new ArrayList<>();
CourseType3.add("English Academic Year");
CourseType3.add("English Academic Semester");
List<String> CourseType4 = new ArrayList<>();
CourseType4.add("IELTS Exam Preparation");
CourseType4.add("TOEFL Exam Preparation");
CourseType4.add("GMAT Exam Preparation");
CourseType4.add("GRE Exam Preparation");
List<String> CourseType5 = new ArrayList<>();
CourseType5.add("Young Learners");
List<String> CourseType6 = new ArrayList<>();
CourseType6.add("General English Language");
CourseType6.add("Intensive English Language");
CourseType6.add("30+");
CourseDetails.put("Learn English in Your Teacher's Home",CourseType1);
CourseDetails.put("English For Work",CourseType2);
CourseDetails.put("Academic Semester / Year",CourseType3);
CourseDetails.put("Exam Preparation Courses",CourseType4);
CourseDetails.put("Young Learners",CourseType5);
CourseDetails.put("Flexible Courses",CourseType6);
return CourseDetails;
}}
AND THIS IS THE COURSE ADAPTER
public class MyCourseAdapter extends BaseExpandableListAdapter {
private Context context;
private HashMap<String,List<String>>Courses_Category;
private List<String> Course_List ;
public MyCourseAdapter(Context context ,HashMap<String,List<String>>Courses_Category,List<String> Course_List) {
this.context=context;
this.Courses_Category=Courses_Category;
this.Course_List=Course_List;
}
#Override
public int getGroupCount() {
return Course_List.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return Courses_Category.get(Course_List.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return Course_List.get(groupPosition);
}
#Override
public Object getChild(int parent, int child) {
return Courses_Category.get(Course_List.get(parent)).get(child);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int parent, int child) {
return child;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String group_title = (String) getGroup(groupPosition);
if (convertView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.parent_courses,parent,false);
}
TextView parent_textview = (TextView) convertView.findViewById(R.id.parent_text_view);
parent_textview.setTypeface(null, Typeface.BOLD);
parent_textview.setText(group_title);
return convertView;
}
#Override
public View getChildView(final int parentPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String child_title = (String) getChild(parentPosition,childPosition);
if (convertView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.child_courses,parent,false);
}
TextView child_textview = (TextView) convertView.findViewById(R.id.child_text_view);
child_textview.setText(child_title);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void onGroupCollapsed(int groupPosition){
super.onGroupCollapsed(groupPosition);
}
public void onGroupExpanded(int groupPosition){
super.onGroupExpanded(groupPosition);
}}
//
Done using LinkedHashMapinstead of HashMap

Resources