android arraylist不会在runnnig app上刷新

时间:2018-03-02 02:14:30

标签: android

我的应用程序由一个包含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();

    }
}

1 个答案:

答案 0 :(得分:-1)

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

这样,只要你想读取First Fragment中的值,你就可以从这个全局arraylist中读取它们,因此你就可以使用你的arraylist的单个实例。