机器人节约动态视图值 [英] android saving dynamic view values

查看:132
本文介绍了机器人节约动态视图值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从标题可能是有点不清楚,我想问这样让我解释什么。

From the title it might be a little unclear what I want to ask so allow me to explain.

在我的项目,我需要在一个while循环的动态视图。像这样;

In my project, I need to make a dynamic view in a while loop. Like so;

private void loadCheckPointQuantity() {
    final LinearLayout rgLayoutContainer = (LinearLayout)findViewById(R.id.quantityAnswers);

    _answerCursor.moveToFirst();
    while(_answerCursor.isAfterLast() == false) {

        LinearLayout rgContainer = new LinearLayout(this);
        rgContainer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        rgContainer.setOrientation(LinearLayout.HORIZONTAL);

        final ImageView checked = new ImageView(this);
        checked.setMinimumHeight(30);
        checked.setMinimumWidth(30);
        if(_answerCursor.getInt(CPA_IS_CHECKED_COL) == 1) {
            checked.setBackgroundResource(R.drawable.btn_check_buttonless_on);
        } else {
            checked.setBackgroundResource(R.drawable.ic_delete);
        }
        checked.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        TextView txtAmount = new TextView(this);
        txtAmount.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        txtAmount.setText((_answerCursor.getPosition() + 1) + "/" + _answerCursor.getCount());
        txtAmount.setTextSize(22);

        RadioGroup rgAnswer = new RadioGroup(this);
        rgAnswer.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        rgAnswer.setOrientation(RadioGroup.HORIZONTAL);
        rgAnswer.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId) {
                case 0:
                    checked.setBackgroundResource(R.drawable.ic_delete);
                    break;
                case 1:
                    checked.setBackgroundResource(R.drawable.btn_check_buttonless_on);
                    break;
                }
            }
        });

        RadioButton rbYes = new RadioButton(this);
        rbYes.setText(R.string.yes);
        rbYes.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        rbYes.setId(1);

        RadioButton rbNo = new RadioButton(this);
        rbNo.setText(R.string.no);
        rbNo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        rbNo.setId(0);

        ImageButton camButton = new ImageButton(this);
        camButton.setImageResource(R.drawable.ic_menu_camera);
        camButton.setLayoutParams(new LayoutParams(70, 70));

        final EditText txtNote = new EditText(this);
        txtNote.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        txtNote.setEnabled(false);
        txtNote.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if(!hasFocus) {
                    txtNote.setSingleLine(true);
                } else {
                    txtNote.setSingleLine(false);
                }
            }
        });

        rgAnswer.addView(rbYes);
        rgAnswer.addView(rbNo);
        rgContainer.addView(checked);
        rgContainer.addView(txtAmount);
        rgContainer.addView(rgAnswer);
        rgContainer.addView(camButton);
        rgContainer.addView(txtNote);
        rgLayoutContainer.addView(rgContainer);

        if(_answerCursor.getInt(CPA_ANSWER_COL) == 1) {
            rbYes.setChecked(true);
        } else if(_answerCursor.getInt(CPA_ANSWER_COL) == 0) {
            rbNo.setChecked(true);
        }   

        if(_answerCursor.getInt(CPA_ANSWER_COL) == 0 || _answerCursor.getInt(CPA_ANSWER_COL) == 1) {
            txtNote.setEnabled(true);
        }
        _answerCursor.moveToNext();
    }
    _answerCursor.moveToFirst();
}

什么情况是,有一个在databasethat一个字段​​确定循环多少次必须划线。将一定量。因此,如果数量为5的部件应使5倍。

What happens is, There is a field in the databasethat determines how many times the loop should be crossed. A quantity. So if quantity is 5. The components should be made 5 times.

问题是,

由于我做的一切,在一个方法,我无法访问个人的看法。
例如,这是什么需要在现实中发生,以及

Because I do all that in a method, I can't access the individual views. For example, and this is what needs to happen in reality as well;

我点击一个按钮。然后,我需要保存在数据库中的所有结果。
即:对的EditText的和器isChecked的RadioGroup中的结果。

I click a button. Then I need to save all results in the database. Ie for the EditText's and isChecked's radiogroup results.

问题是,我不知道我怎么可以访问每一个人的看法。
我想到的几件事情要解决这个问题。但他们都最终建立在我头上的问号

The problem is, I don't know how I can access each individual view. I have thought of several things to solve this. But they all end up creating question marks above my head

我试过了,做一个类来重新present一个答案(器isChecked,请注意,等等(有getter和setter)
创建一个数组列表,把他们都在一个ArrayList

I tried, making a class to represent an answer(isChecked, note, etc(with getters and setters) create an array list, put them all in an arraylist.

这应该工作。在把一个ArrayList的答案。但是,这让我的下一个问题。我的文字变化?我该如何处理呢?

this should work. Putting the answers in an arraylist. But that leaves me with the next problem. What i a text changes? How can I handle this?

如果我使用的观测,应该我做的答案类可观察到的,我呼吁每一个成员setChanges(),并实现在包含loadCheckPointQuantity类观察到的?

if I use observables, should I make the answer class observable, should I call setChanges() for every member, and implement observable in the class containing the loadCheckPointQuantity?

我有观测准确0,4经验。

I have exactly 0,4 experience with observables.

我怎么会去保存的最新数据时pressing下一个按钮?

How would I go about saving the latest data when pressing the next button?

我绝对无能...

因为它可能是模糊的:

我尽量做到:从所有视图到数据库中保存的最新数据
如何:帮助需要
时:按钮点击

What I try to achieve: Save the latest data from all views to the database how: help needed when: on button click

您走上理解在/阅读这篇文章/看的时间是非常AP preciated。答案甚至更高。

The time you took to understand/look at/read this post is highly appreciated. Answers even more highly.

如果你需要更多的code或解释。让我知道,我会尽我所能。

If you need more code or explanation. Let me know and I will do my best.

推荐答案

我解决了我的问题。

我分配给事件监听器需要在数据库中eventuall结束了所有的东西。
在焦点变化,我将数据保存到数据库中。因此,我不抱任何列出任何控股的答案。我随时更新数据库。不幸的是,它是不好的性能。幸运的是,我们能负担得起这样的表现。

I assigned eventlisteners to all things that need to eventuall end up in a database. On focus change, I save the data to the database. So I don't hold any lists whatsoever holding answers. I update the database on the fly. Unfortunately, it is bad for performance. Fortunately, we can afford this performance.

我试图运行这个code告诉while循环来执行它的3000倍。除了服用一段时间(大约平均3-4seconds)视图生成,所有运行良好。因此,我不需要跟踪所生成的对象的方法之外。安卓/ Java没有为我。

I tried to run this code telling the while loop to execute it 3000 times. Apart from the view generation taking some time(about 3-4seconds on average), all ran fine. So I don't need to keep track of the generated objects outside of the method. android/java does that for me.

这篇关于机器人节约动态视图值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆