在ViewTab内部的片段内刷新列表,按下适配器中的按钮

时间:2017-11-14 10:24:47

标签: android listview android-fragments tags adapter

从标题中可以看出,我对如何解决问题感到有些困惑。我有一个活动,使用Tabs在两个包含一个列表eache one的片段之间切换。该列表具有相同的适配器,并且在每个列表中都有一个刷新两个列表的按钮。

这是我的MainActivity.class:

public class MainActivity extends AppCompatActivity {

    private SectionsPagerAdapter mSectionsPagerAdapter;
    public static TreeSet<Task> tasksTODO = new TreeSet<>();
    public static TreeSet<Task> tasksDONE = new TreeSet<>();
    private TabLayout tabs;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewPager = findViewById(R.id.container);
        tabs = findViewById(R.id.tabs);
        tabs.setupWithViewPager(mViewPager);
        tabs.setSelectedTabIndicatorColor(Color.parseColor("#FFFFFF"));
        addTasks();

    }

    @Override
    protected void onResume() {
        super.onResume();
        addTasks();
    }


    private void setupViewPager(ViewPager vp) {
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mSectionsPagerAdapter.addFragment(new ShowTodoTasks(), "todo");
        mSectionsPagerAdapter.addFragment(new ShowDoneTasks(), "done");
        vp.setAdapter(mSectionsPagerAdapter);
    }


    public void addTasks() {
        tasksDONE.add(new Task("Number 1", "pepin", "", "Description 1", 1));
        tasksDONE.add(new Task("Number 2", "pepin", "", "Description 2", 1));
        tasksDONE.add(new Task("Number 3", "pepin", "", "Description 3", 1));
        tasksTODO.add(new Task("Number 4", "pepin", "", "Description 4", 1));
        setupViewPager(mViewPager);

    }
}

这是我的TaskAdapter:

public class TaskAdapter extends BaseAdapter {

    protected Context context;
    protected ArrayList<Task> items;
    private ImageView edit;
    private CheckBox checked;
    Task dir;
    private TextView title, comment, description, date;

    public TaskAdapter(Context context, ArrayList<Task> items) {
        this.context = context;
        this.items = items;
    }


    @Override
    public int getCount() {
        return items.size();
    }

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

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

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {


        LayoutInflater inf = (LayoutInflater) viewGroup.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inf.inflate(R.layout.task_item_template, null);


        edit = (ImageView) view.findViewById(R.id.item_edit);
        checked = (CheckBox) view.findViewById(R.id.item_done);

        final View finalView = view;


        dir = items.get(position);


        int priority = dir.getPriority();
        if (priority == 1) {
            view.setBackgroundResource(R.drawable.major_prior_task_border);
        }
        if (priority == 2) {
            view.setBackgroundResource(R.drawable.medium_prior_task_border);
        }

        if (priority == 3) {
            view.setBackgroundResource(R.drawable.minor_prior_task_border);
        }


        title = view.findViewById(R.id.item_title);
        title.setText(dir.getTitle());

        comment = view.findViewById(R.id.tv_comment);
        comment.setText(dir.getComment());

        description = view.findViewById(R.id.item_description);
        description.setText(dir.getDescription());

        date = view.findViewById(R.id.item_date);
        date.setText(dir.getCreationDate());

        return view;

    }
}

这是我的碎片:

public class ShowDoneTasks extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_show_done_tasks, container, false);
        setup(MainActivity.tasksDONE,v);
        return v;
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    private void setup(TreeSet<Task> tasks, View v) {
        ListView lv = v.findViewById(R.id.lv_tasks_done);
        ArrayList<Task> tareasDone = new ArrayList<>(tasks);
        TaskAdapter adapter = new TaskAdapter(v.getContext(), tareasDone);
        lv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
}

另一个:

public class ShowTodoTasks extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_show_todo_tasks, container, false);
        setup(MainActivity.tasksTODO,v);
        return v;
    }



    private void setup(TreeSet<Task> tasks, View v) {
        ListView lv = v.findViewById(R.id.lv_tasks_todo);
        ArrayList<Task> tareasTodo = new ArrayList<>(tasks);
        TaskAdapter adapter = new TaskAdapter(v.getContext(), tareasTodo);
        adapter.notifyDataSetChanged();
        lv.setAdapter(adapter);
    }

}

我想要的是:当你点击TaskAdapter中的复选框时,我想刷新列表。我尝试在片段中使用adapter.notifyDataSetChanged(),但它似乎不起作用。

有什么想法吗?

0 个答案:

没有答案