1

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!

LBJ33
  • 425
  • 11
  • 29
  • 1. do you have many listview rows? if so, you should use recyclerview, as it will improve the performance. 2. what happens in the current state? does the row not get added? – Aleksandar Stefanović Jul 24 '16 at 20:47
  • Each listview will have around 20 rows.. would that be enough to justify recyclerview? Right now I get a red line where I try to put the strings to be added, the "false" for the checkmark seems to be giving the error – LBJ33 Jul 24 '16 at 21:04
  • what exactly is this error? It must have some kind of error message... – Aleksandar Stefanović Jul 24 '16 at 21:16
  • Yes when I hover over it says: "CustomObject() in CustomObject cannot be applied to: Expected args: actual args exercise:string "Squats" percent:string "60%" reps:string "6" weight: string "150" check1: android.widget.checkbox false (boolean) – LBJ33 Jul 24 '16 at 21:33
  • The check1 part is giving the error, im guesing it's something wrong in my CustomObject class.. if I had to guess it would be this line: "public CustomObject(String exercise, String percent, String reps, String weight, Checkbox check1)............. but I'm not sure what I would change the checkbox to – LBJ33 Jul 24 '16 at 21:36
  • It's because you should probably replace "CheckBox" with boolean (everywhere in your custom object). CheckBox is an View that displays the value of the boolean, custom object should hold the value (boolean) instead of the View object (CheckBox can hold different View values, while boolean is the one that is either true or false) – Aleksandar Stefanović Jul 24 '16 at 21:36
  • That worked for that problem, thank you. Now when I try to run the app, I get this error: Attempt to invoke virtual method 'void com.bestworkouts.sheikoworkout.CustomListViewAdapter.addAdapterItem(com.bestworkouts.sheikoworkout.CustomObject)' on a null object reference – LBJ33 Jul 24 '16 at 21:45
  • and it sends me to this line: if (childPosition == 0) { mCustomListViewAdapter.addAdapterItem(new CustomObject("Squats", "60%", "6", "150", false)); listViewFri.setAdapter(mCustomListViewAdapter); – LBJ33 Jul 24 '16 at 21:45
  • Try to give the initial ArrayList (even empty) before adding additional items. It is probably trying to add an item to the ArrayList which doesn't exist, hence the "null object reference" error. – Aleksandar Stefanović Jul 24 '16 at 21:53

0 Answers0