0

我正在编写我的第一个 android 应用程序作为大学课程。到目前为止,我的大部分工作都在工作,但我遇到了一个问题。

该应用程序是结构化的,因此我有一个 Activity,它设置了一个 ViewPager,其中包含 3 个片段。每个 Fragment 都是 ListFragment 的扩展,列表中填充了从数据库中提取的数据。适配器显示每个 ListItem 的自定义视图,其中包括一个复选框。

我想要发生的是当复选框是票时,所有三个片段都被更新。也就是说,为每个片段重新查询数据库并更新列表视图。

家庭活动。这将设置 ViewPager。它添加片段并设置片段适配器。公共类 HomeActivity 扩展 SherlockFragmentActivity {

    private FragmentAdapter mPagerAdapter;
    private ViewPager mViewPager;

    /**
    * Set up the view. Add the Fragments to the viewPager, set up tabs. 
    */
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        getSupportActionBar();

        ...

        //Set up Fragments. 
        List<MemoFragment> fragments = new Vector<MemoFragment>();
        fragments.add(new IncompleteMemoFragment());
        fragments.add(new CompleteMemoFragment());
        fragments.add(new UrgentMemoFragment());

        mPagerAdapter = new FragmentAdapter(getSupportFragmentManager(), fragments);

        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setOnPageChangeListener(new TabListener(mTabHost,mViewPager));
        mViewPager.setOffscreenPageLimit(3);
    }
    ...
}

片段适配器

public class FragmentAdapter extends FragmentPagerAdapter {

    private List<MemoFragment> mFragments;

    public FragmentAdapter(FragmentManager fm, List<MemoFragment> fragments) {
        super(fm);
        this.mFragments = fragments;
    }

    @Override
    public MemoFragment getItem(int position) {
        return mFragments.get(position);
    }

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

}

MemoFragment 类、IncompleteMemoFragment、CompleteMemoFragment、UrgentMemoFragment 都扩展了它,并且 99% 相同,它们只是从数据库中提取一组不同的数据。

public class MemoFragment extends SherlockListFragment {

    protected DatabaseSource datasource;

    /**
     * Creates the View. 
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_memo_list, container, false);

        datasource = new DatabaseSource(container.getContext());
        datasource.open();

        List<Memo> values = getMemos();

        datasource.close();

        MemoListViewAdaptor adapter = new MemoListViewAdaptor(container.getContext(), values);

        setListAdapter(adapter);

        return rootView;
    }

    /**
    * Gets the memos to show.
    * @return the memos to show.
    */
    public List<Memo> getMemos(){
        return datasource.getAllMemos();
    }

}

这是 MemoListViewAdapter。在这里,您将看到我也在尝试寻找更改的复选框。

public class MemoListViewAdaptor extends ArrayAdapter<Memo> {
    private final Context mContext;
    private final List<Memo> mMemos;

    /**
     * Save the context and the Memos
     * @param context the context to save
     * @param memos the memos to save.
     */
    public MemoListViewAdaptor(Context context, List<Memo> memos) {
        super(context, R.layout.memo_list_row, memos);
        this.mContext = context;
        this.mMemos = memos;
    }

    /**
     * Get the view. Sets up the list to use the custom view. 
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.memo_list_row, parent, false);

        ...
        CheckBox done = (CheckBox) rowView.findViewById(R.id.done);

        Memo memo = mMemos.get(position);


        done.setOnClickListener(new ClickListener(memo));
        if(memo.getCompleteness() == Memo.COMPLETE){
            done.setChecked(true);
        }

        ...

        return rowView;
    }

}

而且,最后这里是点击列表器。

public class ClickListener implements OnClickListener {
    private Memo memo;

    ClickListener(Memo memo){
        this.memo = memo;
    }
    @Override
    public void onClick(View v) {
        if(v.getClass() == CheckBox.class){
            Log.v("com.milner.memolist", "These people are crazy. This task is done");
            if(((CheckBox)v).isChecked()){
                memo.setCompleteness(1);
            }else{
                memo.setCompleteness(0);
            }
            DatabaseSource datasource = new DatabaseSource(v.getContext());
            datasource.open();
            datasource.updateMemo(memo);
            datasource.close();

        }else{
        ...
        }
    }  
}

所以你有它。这就是我到目前为止所拥有的!如您所见,我可能以一种非常复杂的方式完成了此操作……我对android没有太多经验。我想我真正想做的是在勾选复选框时重新加载所有片段。

是否有捷径可寻?有没有一种复杂的方法可以做到这一点?我只是无法理解这最后一点!

希望你能帮忙!:)

谢谢

汤姆·M

4

1 回答 1

4

是否有捷径可寻?

添加:

@Override
public int getItemPosition(Object object) {     
    return POSITION_NONE;
}

到你的FragmentAdapter,当你需要更新片段调用notifyDataSetChanged()FragmentAdapter。这将触发片段的重建。

有没有一种复杂的方法可以做到这一点?

立即更新可见片段,以便用户看到更改并在用户尝试查看它们之后或之后更新下一个片段(开始滑动时)。要检查这个解决方案,谷歌有点关于获取/更新Fragment/从ViewPager.

If you just have three fragments in the ViewPager set the off screen page limit to 2 as you'll only have a maximum of two fragments on either side of any fragment from the ViewPager. Also keeping references to the fragment like you do with that Vector(use an ArrayList) for (I'm assuming) accessing them later will fail after a screen rotation as those will not be the fragments the ViewPager will use.

于 2013-03-09T13:52:31.250 回答