且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何填充这是在android的按钮,点击动态生成一个微调

更新时间:2023-09-29 20:56:58

编辑:
你不需要你的额外活动。你只需要一个活动,这将显示您的布局,并更新在你按一下按钮。

You don't need your extra activity. You simply need one activity, that will display your layout, and update it on your button click.

public class TestActivity extends Activity {

    Button btnDisplay;
    ImageButton btnAdd;
    LinearLayout container;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        container = findViewById(R.id.linearLayoutForm);
        btnAdd = (ImageButton) findViewById(R.id.btnAdd);
        btnDisplay = (Button) findViewById(R.id.btnDisplay);

        btnAdd.setOnClickListener(addListener);
        //TODO: btnDisplay
    }

    /*
     * We define our OnClickListener that will act when we click on the btn.
     */
    View.OnClickListener addListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final LinearLayout newView = (LinearLayout) getLayoutInflater().inflate(R.layout.rowdetail, null);

            newView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
            btnRemove.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    container.removeView(newView);
                }
            });

            container.addView(newView);

            //Now we load your data into your spinner
            Spinner s = newView.findViewById(R.id.spinner1);
            try {
                loadSpinnerData(s);
            } catch (IOException e) {
                //TODO: catch exception
                e.printStackTrace();
            }

        }
    };

    /*
     * This function is supposed to load the data into the given spinner.
     * It would be better to load the data an other way, i.e.: using ASyncTask
     */
    private void loadSpinnerData(Spinner s) throws IOException {

        // database handler
        DBHelper db = new DBHelper(getApplicationContext());

        // Spinner Drop down elements
        List<String> products = db.getAllProducts();

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, products);

        // Drop down layout style - list view with radio button
        dataAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        s.setAdapter(dataAdapter);

    }
}

我没有测试此code,所以你可能因此它符合你的需求来调整它,但我认为主要的想法是存在的。
您的活动的onCreate膨胀布局。在那里你设定按钮,保存你的容器。

I haven't tested this code, so you might have to tweak it so it matches your needs, but I think the main idea is there. Your activity's onCreate inflates your layout. In there you set your button, and save information about your "container".

在点击您的添加按钮,您只需抬高新的布局,并使用设置微调数据的 loadSpinnerData(S); ,它从数据库加载数据到你的微调。

On click on your add button, your simply inflate your new layout, and set your spinner data using your loadSpinnerData(s);, which loads the data from the database into your spinner.

请注意,这是没有得到从数据库中的信息的一个好方法。这样做可以阻止UI线程为获取大量的信息可能会非常耗时。这是更好地使用加载器,或AsyncTask的这样做。我也可以把你 Vogella教程,解释得非常好(而且很容易明白)如何有效地管理数据库中。

Note that it is not a good way of getting the information from the database. Doing so can block the UI thread as retrieving lots of information can be time consuming. It's better to use a loader, or an asynctask to do so. I can redirect you to Vogella tutorials that explains very well (and is very easy to understand) how to manage a database efficiently.