带有textview和复选框的Listview,自定义适配器

时间:2016-07-24 20:05:50

标签: android listview custom-adapter android-checkbox

我有一个可扩展的列表视图,当单击时,切换到带有3个选项卡的tablayout,每个选项卡包含一个列表视图。根据在可扩展列表视图中单击的项目,3个选项卡将填充不同的数据。

我已经把它全部设置好所以我认为它可能(可能)有效..但我在列表视图中添加行时遇到了麻烦。当我添加此行时,我不知道要为复选框添加什么(我认为false会将其设置为未选中):

mCustomListViewAdapter.addAdapterItem(new CustomObject("Squats", "60%", "6", "150", false);

我也开始怀疑这是最好的方法。我并不担心它是不是最好的方法,因为它只是我和一些朋友的应用程序,但我担心这样做是迟滞/崩溃这样做

这是我的可扩展列表视图代码,假设将其发送到下一个活动并使用自定义适配器添加所需的对象。

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

这是我的惯例:

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

最后是我的customobject类:

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

感谢您的帮助!请通过添加带复选框的行告诉我如何修复错误。也欢迎其他方式的意见。你认为这会让这个应用程序变得迟钝吗?每个子位置将有3个列表视图,每个列表视图将使适配器添加大约20-25行。根据我的理解,只有点击的子位置将保存信息,而不是其他的(不确定)。每行中的一个值最终将由用户提供的变量替换。

再次感谢!

0 个答案:

没有答案