I have a expandable listview that when clicked, switches to a tablayout with 3 tabs, each tab containing a listview. Depending on what item was clicked in the expandable listview the 3 tabs will populate different data.
I have set it all up so I think it might (MIGHT) work.. but I'm having trouble adding rows to the listview. I don't know what to put for the checkbox when Im adding this line(I thought false would set it to unchecked):
mCustomListViewAdapter.addAdapterItem(new CustomObject("Squats", "60%", "6", "150", false);
I'm also starting to doubt that this is the best method. I'm not worried if it's not the best method as it's just an app for me and a few friends, but I'm worried it will be laggy/crash doing it this way.
Here is my expandable list view code, that is suppose to send it to the next activity and use the custom adapter to add the objects needed.
public class MyWorkout extends BaseActivity {
ExpandableListView expandableListView;
HashMap<String, List<String>> Workouts_details;
List<String> Workout_list;
WorkoutsAdapter mAdapter;
CustomListViewAdapter mCustomListViewAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_workout);
mToolBar = activateToolbar();
setUpNavigationDrawer();
expandableListView = (ExpandableListView) findViewById(R.id.listViewWorkouts);
Workouts_details = DataProvider.getInfo();
Workout_list = new ArrayList<>(Workouts_details.keySet());
mAdapter = new WorkoutsAdapter(this, Workouts_details, Workout_list);
final ListView listViewFri = (ListView) findViewById(R.id.listViewFri);
final ListView listViewMon = (ListView) findViewById(R.id.listViewMon);
final ListView listViewWed = (ListView) findViewById(R.id.listViewWed);
expandableListView.setAdapter(mAdapter);
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if (groupPosition == 0) {
if (childPosition == 0) {
listViewFri.setAdapter(mCustomListViewAdapter);
mCustomListViewAdapter.addAdapterItem(new CustomObject("Squats", "60%", "6", "150", false);
listViewMon.setAdapter(mCustomListViewAdapter);
// TODO: ADD ALL ITEMS FOR TAB
listViewWed.setAdapter(mCustomListViewAdapter);
// TODO: ADD ALL ITEMS FOR TAB
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
startActivity(intent);
}
if (childPosition == 1) {
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
startActivity(intent);
}
if (childPosition == 2) {
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
startActivity(intent);
}
if (childPosition == 3) {
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
startActivity(intent);
} else return false; }
if (groupPosition == 1) {
if (childPosition == 0) {
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
startActivity(intent);
}
if (childPosition == 1) {
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
startActivity(intent);
}
if (childPosition == 2) {
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
startActivity(intent);
}
if (childPosition == 3) {
Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
startActivity(intent);
} else return false; }
}
return false;
}
Here is my customadapter:
public class CustomListViewAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ArrayList<CustomObject> objects;
private class ViewHolder {
TextView txtExercise;
TextView txtPercent;
TextView txtReps;
TextView txtWeight;
CheckBox check1;
}
public void addAdapterItem(CustomObject item) {
objects.add(item);
}
public CustomListViewAdapter(Context context, ArrayList<CustomObject> objects) {
inflater = LayoutInflater.from(context);
this.objects = objects;
}
public int getCount() {
return objects.size();
}
public CustomObject getItem(int position) {
return objects.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.workout_item, null);
holder.txtExercise = (TextView) convertView.findViewById(R.id.txtExercise);
holder.txtPercent = (TextView) convertView.findViewById(R.id.txtPercentage);
holder.txtReps = (TextView) convertView.findViewById(R.id.txtReps);
holder.txtWeight = (TextView) convertView.findViewById(R.id.txtWeight);
holder.check1 = (CheckBox) convertView.findViewById(R.id.check1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtExercise.setText(objects.get(position).getExercise());
holder.txtPercent.setText(objects.get(position).getPercent());
holder.txtReps.setText(objects.get(position).getReps());
holder.txtWeight.setText(objects.get(position).getWeight());
holder.check1.setChecked(false);
return convertView;
}
}
And finally my customobject class:
public class CustomObject {
private String exercise;
private String percent;
private String reps;
private String weight;
private CheckBox check1;
public CustomObject(String exercise, String percent, String reps, String weight, CheckBox check1) {
this.exercise = exercise;
this.percent = percent;
this.reps = reps;
this.weight = weight;
this.check1 = check1;
}
public String getExercise() {
return exercise;
}
public String getPercent() {
return percent;
}
public String getReps() {
return reps;
}
public String getWeight() {
return weight;
}
public CheckBox getCheck1() {
return check1;
}
}
Thanks for the help! Please let me know how to fix the error with adding the row with checkbox. Also open to opinions on other ways to do this. Do you think this will make the app laggy doing it this way? Each child position will have 3 listviews and each listview will have the adapter adding about 20-25 rows. Based on what I understand though, only the childposition that will click will be holding the info, not the other ones(not sure though). One of the values in each row will be replaced by a variable supplied by the user eventually.
Thanks again!