创建所有ListView项目

时间:2017-07-03 23:49:05

标签: java android listview arraylist

我有一个 ListView适配器,其中getView方法在 ArrayList 中添加和删除内容。因此,如果我不向下滚动但从适配器中取出ArrayList,则ArrayList未完成。我该如何解决这个问题?

我想知道我是否可以添加像onCreate()之类的东西,它在被查看之前被触发,尽管我无法通过反复试验来实现这一点。

虽然我会编辑此问题并添加代码,但我认为您不需要此问题的代码。 谢谢你的帮助:)

public class ContributionAdapter extends BaseAdapter {
    ArrayList<ArrayList<Course>> subjects = new ArrayList<>();
    ArrayList<boolean[]> checked = new ArrayList<>();
    Context context;
    String username;
    FragmentManager manager;
    ListView lv;
    String parentDir;
    Toolbox tool;

    private static LayoutInflater inflater=null;
    public ContributionAdapter(Context c, MenuActivity menuActivity, ArrayList<ArrayList<Course>> subjects, FragmentManager manager, ListView lv) {
        this.subjects = subjects;

        this.manager = manager;
        this.lv = lv;

        context = c;
        tool = new Toolbox(menuActivity);
        this.parentDir = parentDir;
        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    public class Holder
    {
        ArrayList<Course> subject;
        boolean[] checks;
        ImageView coursecolor;
        CheckBox q1c, q2c, q3c, q4c;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final Holder holder=new Holder();
        View rowView;
        rowView = inflater.inflate(R.layout.fragment_finished_two_listview, null);

        holder.subject = new ArrayList<>();
        holder.subject = Collection.getSubjects().get(position);
        holder.coursecolor =(ImageView) rowView.findViewById(R.id.coursecolor);

        holder.q1c = rowView.findViewById(R.id.q1c);
        holder.q2c = rowView.findViewById(R.id.q2c);
        holder.q3c = rowView.findViewById(R.id.q3c);
        holder.q4c = rowView.findViewById(R.id.q4c);

        holder.checks = new boolean[]{false, false, false, false};

        try {
            checked.remove(position);
        }
        catch (IndexOutOfBoundsException e) {

        }

        ArrayList<CheckBox> boxes = new ArrayList<>();
        boxes.add(holder.q1c); boxes.add(holder.q2c); boxes.add(holder.q3c); boxes.add(holder.q4c);



        for(int i = 0; i < boxes.size(); i++) {
            final int u = i;
            boxes.get(i).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                    holder.checks[u] = isChecked;
                }
            });
        }
        checked.add(position, holder.checks);
        return rowView;
    }



    public ArrayList<boolean[]> getChecked() {
        return checked;
    }

}

获取ArrayList:

ArrayList<boolean[]> checked = ((ContributionAdapter)lv.getAdapter()).getChecked();

0 个答案:

没有答案