如何填充这是在android的按钮,点击动态生成一个微调 [英] How to populate a spinner which is dynamically generated on click of button in android

查看:326
本文介绍了如何填充这是在android的按钮,点击动态生成一个微调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何在Android的添加/删除布局动态按钮的点击发现的的这个

我用codeS上面提到的教程和设计产生按钮的每次点击一个布局的页面。此布局包含微调的EditText &安培; 按钮

I used the codes from above mentioned tutorial and designed a page which generates a layout on every click of button. This layout contains a Spinner, EditText & Button.

我的code成功地增加了/删除按钮单击布局。

所以我的第一个问题是所有生成的微调都是空的,即它们不与填充数据从数据库之后,他们的创作和第二个问题是,我怎么能获取所选/进入,从动态创建的微调数据/作为的EditText我不知道他们的ID。

DBHelper 类正常工作与我的其他应用程序,所以我没有在这里张贴。

The DBHelper class works fine with my other apps so I haven't posted it here.

这是 MyLayoutOperation 类: -

This is the MyLayoutOperation class:-

    public class MyLayoutOperation extends Activity {
    static Spinner s;

public static void display(final Activity activity, Button btn)
    {
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                LinearLayout scrollViewlinerLayout = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);

                java.util.ArrayList<String> msg = new ArrayList<String>();

                for (int i = 0; i < scrollViewlinerLayout.getChildCount(); i++)
                {
                    LinearLayout innerLayout = (LinearLayout) scrollViewlinerLayout.getChildAt(i);
                    s = (Spinner) innerLayout.findViewById(R.id.spinner1);
                    EditText edit = (EditText) innerLayout.findViewById(R.id.editDescricao);

                    //msg.add(products.getSelectedItem().toString());
                    msg.add(edit.getText().toString());

                }

                Toast t = Toast.makeText(activity.getApplicationContext(), msg.toString(), Toast.LENGTH_SHORT);
                t.show();
            }
        });
    }
    public static void add(final Activity activity, ImageButton btn)
    {
        final LinearLayout linearLayoutForm = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);;

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final LinearLayout newView = (LinearLayout)activity.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) {
                        linearLayoutForm.removeView(newView);
                    }
                });

                linearLayoutForm.addView(newView);
            }
        });
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rowdetail);

         s = (Spinner) findViewById(R.id.spinner1);
         // Loading spinner data from database
            try {
                loadSpinnerData();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

    private void loadSpinnerData() 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);

    }
}

LogCat中: -

LogCat:-

07-22 08:23:27.090: E/AndroidRuntime(1834): FATAL EXCEPTION: main
07-22 08:23:27.090: E/AndroidRuntime(1834): Process: com.example.teste1, PID: 1834
07-22 08:23:27.090: E/AndroidRuntime(1834): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.teste1/com.example.teste1.MainActivity}: java.lang.NullPointerException
07-22 08:23:27.090: E/AndroidRuntime(1834): Caused by: java.lang.NullPointerException
07-22 08:23:27.090: E/AndroidRuntime(1834):     at com.example.teste1.MyLayoutOperation.add(MyLayoutOperation.java:73)
07-22 08:23:27.090: E/AndroidRuntime(1834):     at com.example.teste1.MainActivity.onCreate(MainActivity.java:22)
07-22 08:23:27.090: E/AndroidRuntime(1834):     at android.app.Activity.performCreate(Activity.java:5231)

线73 MyLayoutOperation : - btn.setOnClickListener(新View.OnClickListener(){

Line 73 of MyLayoutOperation :- btn.setOnClickListener(new View.OnClickListener() {

线22 MainActivity : - MyLayoutOperation.add(这一点,btnAdd);

Line 22 of MainActivity :- MyLayoutOperation.add(this, btnAdd);

MainActivity.java : -

MainActivity.java :-

 public class MainActivity extends Activity {

    Button btnDisplay;
    ImageButton btnAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnAdd = (ImageButton) findViewById(R.id.btnAdd);
        btnDisplay = (Button) findViewById(R.id.btnDisplay);

        MyLayoutOperation.add(this, btnAdd);
        MyLayoutOperation.display(this, btnDisplay);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

rowdetail.xml : -

rowdetail.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowdetail"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="94dp"
        android:layout_height="45dp" />

    <EditText
        android:id="@+id/editDescricao"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.01"
        android:ems="10"
        android:inputType="text" />

    <ImageButton
        android:id="@+id/btnRemove"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/btnRemove"
        android:src="@android:drawable/ic_delete" />

</LinearLayout>

activity_main.xml中: -

activity_main.xml :-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layoutTeste"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="95dp"
            android:layout_height="fill_parent"
            android:layout_alignBottom="@+id/btnAdd"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@+id/btnAdd"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/titleTecnologies"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ImageButton
            android:id="@+id/btnAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:contentDescription="@string/btnAdd"
            android:src="@android:drawable/ic_input_add" />

    </RelativeLayout>

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="275dp" >

        <LinearLayout
            android:id="@+id/linearLayoutForm"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnDisplay" />

</LinearLayout>

请帮忙。

推荐答案

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

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.

这篇关于如何填充这是在android的按钮,点击动态生成一个微调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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