0

我正在为 android 平板电脑开发,我有一个选项卡,它是一个片段,它有许多 ViewGroups (LinearLayouts),其中包含许多彩色圆圈视图——它们有一些与之关联的数据对象。(见屏幕截图)

消息将从网络传入并更新与每个圆形视图关联的数据对象的数量。当他们进来时,我想将它们添加到 SQLite DB;并且当这种变化自动更新侦听 ViewGroups。然后它将更新与每个 CircleView 关联的数据对象的数量和内容。

我一直在研究内容提供者、适配器等。但这些似乎是针对占据整个屏幕的列表视图,例如在手机上。问题是如何根据数据库更改在我的片段中自动有效地更新这些视图?我将使用什么机制以及如何使用它们。我已经陷入了许多死胡同。(基于我有限的android dev exp)

任何见解都会很棒!谢谢

我设置视图的选项卡片段:

    public class TheoryFragment extends Fragment {


    private ViewGroup theoriesView;
    private HashMap<String, TheoryPlanetView> theoryViewsToAnchors = new HashMap<String, TheoryPlanetView>();
    private GestureDetector gestureDetector;    
    private SimpleCursorAdapter dataAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        theoriesView = (ViewGroup) inflater.inflate(R.layout.theories_activity_vertical,
                container, false);

        setupListeners();

        return theoriesView;
    }

    /**
     * Setups the listeners 
     */
    private void setupListeners() {
        //add listeners to all the draggable planetViews
        // get all the colors and the
        int childCount = theoriesView.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View possibleView = theoriesView.getChildAt(i);
            if( possibleView instanceof TheoryPlanetView ) {
                TheoryPlanetView tv = (TheoryPlanetView) possibleView;
                tv.setOnDragListener(new TargetViewDragListener());

                theoryViewsToAnchors.put(tv.getAnchor(), tv);
            }
        }

        ViewGroup planetColorsView = (FrameLayout)             theoriesView.findViewById(R.id.colors_include);
        // get all the colors and the
        childCount = planetColorsView.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View color = planetColorsView.getChildAt(i);
            color.setOnTouchListener(new CircleViewDefaultTouchListener());
        }
    }

}

截屏

4

1 回答 1

0

在理解问题时遇到了一些麻烦,但我会使用异步任务 http://developer.android.com/reference/android/os/AsyncTask.html

基本上你想在后台线程上做网络或数据库的东西。任务完成后,android会自动运行一个方法

onPreexecute() -> 启动进度条等

doInBackground() -> 网络/数据库

onPostExecute() -> 更新你的 UI(当 doInBackground 完成时调用)

如果这还不够,您还可以查看观察者模式

于 2013-08-13T00:54:17.920 回答