android arraylist不会在运行应用程序时刷新

问题描述 投票:0回答:1

我的应用程序包含一个包含3个片段的主活动,并且我在网格适配器的第二个片段中有一个布尔类型的arraylist来检查每个位置的复选框状态,因此可以在第一个片段中执行操作。问题是,arreylist永远不会更新,它会保留我的代码所在的相同数据值

public class GridAdapter extends BaseAdapter {

    private ArrayList<GridWord> list;
    private Context context;
    private ArrayList<Boolean> checkBoxState;
    public boolean status;




    GridAdapter (Context context) {

        this.context = context;
        checkBoxState = new ArrayList<Boolean>();

        for (int y = 0; y < 14; y++){
            checkBoxState.add(y, false);
        

        }
    }


    @Override
    public int getCount() {

        return list.size();
    }


    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    class ViewHolder  {

        ImageView logoImage;
        TextView textName;
        CheckBox checkBox;

        ViewHolder (View v){

            logoImage = v.findViewById(R.id.grid_image);
            textName = v.findViewById(R.id.grid_text);
            checkBox = v.findViewById(R.id.grid_checbox);
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View row = convertView;
        ViewHolder holder = null;











        if (row == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.grid_item,parent,false);
            holder = new ViewHolder(row);
            row.setTag(holder);

        }
        else {

            holder = (ViewHolder) row.getTag();

        }

        final GridWord temp = list.get(position);
        holder.logoImage.setImageResource(temp.logo);
        holder.textName.setText((CharSequence) temp.name);



        final ViewHolder finalHolder = holder;

        finalHolder.checkBox.setId(position);






        row.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                status = true;
                if(((finalHolder.checkBox.isChecked()))) {
                    checkCheckBox(position, false);
                    finalHolder.checkBox.setChecked(checkBoxState.get(position));

                }
                else {
                    checkCheckBox(position, true);
                    finalHolder.checkBox.setChecked(checkBoxState.get(position));
                }

            }
        });
         finalHolder.checkBox.setChecked(checkBoxState.get(position));






        return row;
    }


    public ArrayList<Boolean> getCheckBoxState(){
        return checkBoxState;


    }
    public void checkCheckBox(int position, boolean value) {
        if (value)
            checkBoxState.set(position, true);
        else
            checkBoxState.set(position, false);

        notifyDataSetChanged();
    }
 }
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    isVisible = isVisibleToUser;
    if (isVisible && isStarted) {
        checkList.clear();
        checkList = gridAdapter.getCheckBoxState();
        for (int x = 0; x < checkList.size(); x++) {

            if (checkList.get(x)) {
              // some code


            }


        }

        adapter.notifyDataSetChanged();

    }
}
android
1个回答
-1
投票

我建议你让这个arraylist在你的活动中持有这3个片段。尝试从第二个片段更新此Arraylist

这样,每当你想要读取First Fragment中的值时,你就可以从这个全局arraylist中读取它们,因此你就拥有了arraylist的单个实例。

© www.soinside.com 2019 - 2024. All rights reserved.