TabHost onTabChangeListener - 设置listview适配器

时间:2016-07-27 21:48:05

标签: android listview android-arrayadapter android-tabhost custom-adapter

我正在尝试更改我的应用中的一些部分。我无法弄清楚如何设置我的应用程序,因此每个选项卡都会更改listview中的数据。任何帮助赞赏。 我的标签设置类:

public class WorkoutDaysActivity extends BaseActivity {

    ListView mListView = new ListView(this);
    ArrayList < CustomObject > w29w1m;
    CustomListViewAdapter mCustomListViewAdapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.workout_days);
        mToolBar = activateToolbar();
        setUpNavigationDrawer();
        w29w1m.add(new CustomObject("Squat", "65%", "6", "150", false));

        final ArrayList < CustomObject > w29w1w = new ArrayList < CustomObject > ();
        w29w1w.add(new CustomObject("Dead", "65%", "6", "150", false));

        final ArrayList < CustomObject > w29w1f = new ArrayList < CustomObject > ();
        w29w1f.add(new CustomObject("Bench", "65%", "6", "150", false));

        TabHost tabHost = (TabHost) findViewById(R.id.TabHost1);
        tabHost.setup();
        tabHost.addTab(tabHost.newTabSpec("1").setIndicator("Monday").setContent(objects));
        tabHost.addTab(tabHost.newTabSpec("1").setIndicator("Wednesday").setContent(objects));
        tabHost.addTab(tabHost.newTabSpec("1").setIndicator("Friday").setContent(objects));


    }


    private TabHost.OnTabChangeListener mOnTabChangeListener = new TabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String tabId) {
            if (tabId.equalsIgnoreCase("1"))
                mListView.setAdapter(mCustomListViewAdapter, w29w1m);
        }
    };

    private final TabHost.TabContentFactory objects = new TabHost.TabContentFactory() {
        @Override
        public View createTabContent(String tag) {
            return mListView;
        }
    };
}

customObject类:

public class CustomObject implements Serializable {

    private String exercise;
    private String percent;
    private String reps;
    private String weight;
    private boolean check1;

    public CustomObject(String exercise, String percent, String reps, String weight, boolean 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 class CustomListViewAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    public ArrayList < CustomObject > objects;
    boolean[] checkBoxState;
    private CustomObject[] dataToBePopulated;

    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;
        checkBoxState = new boolean[objects.size()];
    }

    public int getCount() {
        return dataToBePopulated != null ? dataToBePopulated.length : 0;
    }

    public CustomObject getItem(int position) {
        return objects.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final 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(checkBoxState[position]);

        holder.check1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    checkBoxState[position] = true;
                } else {
                    checkBoxState[position] = false;
                }
            }
        });

        return convertView;
    }
}

问题是我不知道如何设置我的适配器来显示我制作的自定义数组列表(例如w29w1m)。任何帮助非常感谢!谢谢!

1 个答案:

答案 0 :(得分:0)

如果您只想更改适配器内的数据,请尝试更改您在适配器上设置的列表。然后在适配器上调用notifyDataSetChanged()。例如,如果您已实例化LVAdapter lvAdapter = new LVAdapter(context, list);(或您在适配器中使用的任何参数)的适配器,只需将列表更改为您希望ListView包含的内容,然后调用lvAdapter.notifyDataSetChanged();

但是如果您还要更改适配器,请在每次更改选项卡时尝试将另一个适配器设置为listview。

另外,我建议使每个tabSpec ID都是唯一的。