将EditText值提取到对话框中,还需要为这些用户输入的值添加

时间:2016-06-09 02:27:33

标签: android listview android-edittext android-alertdialog addition

image1

image2

嗨,我点击了#34; ADD"按钮....

在此之前,我想向您解释一下我需要实现的目标。这里我有listView项目。该列表包括项目名称和editText。用户可以在此editText中输入值。假设用户在编辑文本中输入三个项目值(例如:Tomoto 30,Soup 40,Biriyani 50)。此外,有一个" ADD"单击时在同一屏幕上显示弹出对话框的按钮。在对话框的内部,必须显示项目名称和用户输入的值,在上述示例(Tomoto 30,Soup 40,Biriyani 50..Total 120)的情况下也需要这些值。如果有人可以提供帮助,将很容易转发并实现我的申请....在此先感谢

===========
 my_row.xml
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:orientation="vertical" android:padding="6dip">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="150dp"
            android:layout_height="100dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="16dp"
            android:src="@drawable/ic_soup"/>

        <TextView android:id="@+id/description"
            android:layout_width="100sp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/imageView"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"/>

        <TextView android:id="@+id/itemNumber"
            android:layout_width="100sp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/imageView"
            android:layout_below="@+id/description"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText android:id="@+id/quantity"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/itemNumber"
            android:inputType="number"
            android:layout_marginTop="50dp"
            android:ems="10"
            android:hint="0.00"

            />

    </RelativeLayout>

    ===========
    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.info.detail.textwatcher.MainActivity">

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="1dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="45dp"
            tools:listitem="@layout/my_row"
            />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal"
            android:background="#46BB09">

           <!-- <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="SEND QUERY"
                android:textSize="20dp"
                android:textColor="@android:color/white"
                android:layout_weight="2"
                android:layout_marginLeft="30dp"/>-->
            <Button
                android:id="@+id/btnAdd"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="ADD"
                android:layout_weight="1"
                android:textSize="16dp"
                android:background="@null"
                android:textColor="@android:color/white"/>
        </LinearLayout>

    </RelativeLayout>

    public class MainActivity extends AppCompatActivity {
        private MyCustomAdapter dataAdapter = null;

        public Button button;
        public EditText ediText;
        TextView textView;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            displayListView();
            button = (Button) findViewById(R.id.btnAdd);
            ediText=(EditText) findViewById(R.id.quantity);
            textView = (TextView) findViewById(R.id.description);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    AlertDialog.Builder alertdialogbuilder = new AlertDialog.Builder(MainActivity.this);


                    alertdialogbuilder.setTitle("Selected Items");


                        }
                    });
        }

        private void displayListView() {

            //Array list of products
            ArrayList<Product> productList = new ArrayList<Product>();
            Product product = new Product(R.drawable.ic_soup, "Tomoto", "desc");
            productList.add(product);
            product = new Product(R.drawable.ic_soup, "Soup", "desc");
            productList.add(product);
            product = new Product(R.drawable.ic_biriyani, "Biriyani", "desc");
            productList.add(product);
            product = new Product(R.drawable.ic_soup, "Chilli", "desc");
            productList.add(product);
            product = new Product(R.drawable.ic_soup, "Powder", "desc");
            productList.add(product);
            product = new Product(R.drawable.ic_soup, "Apple", "desc");
            productList.add(product);
            product = new Product(R.drawable.ic_soup, "Orange", "desc");
            productList.add(product);
            product = new Product(R.drawable.ic_soup, "Banana", "desc");
            productList.add(product);
            product = new Product(R.drawable.ic_soup, "Mango", "desc");
            productList.add(product);


            //create an ArrayAdaptar from the String Array
            dataAdapter = new MyCustomAdapter(this, R.layout.my_row, productList);
            ListView listView = (ListView) findViewById(R.id.listView1);
            // Assign adapter to ListView
            listView.setAdapter(dataAdapter);
        }

        private class MyCustomAdapter extends ArrayAdapter<Product> {

            private ArrayList<Product> productList;

            public MyCustomAdapter(Context context, int textViewResourceId,
                                   ArrayList<Product> productList) {
                super(context, textViewResourceId, productList);
                this.productList = new ArrayList<Product>();
                this.productList.addAll(productList);
            }

            @Override
            public View getView(int position, View view, ViewGroup parent) {

                Product product = productList.get(position);

                if (view == null) {
                    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = vi.inflate(R.layout.my_row, null);
                    EditText quantity = (EditText) view.findViewById(R.id.quantity);
                    //attach the TextWatcher listener to the EditText
                    quantity.addTextChangedListener(new MyTextWatcher(view));

                }

                EditText quantity = (EditText) view.findViewById(R.id.quantity);
                quantity.setTag(product);
                if (product.getQuantity() != 0) {
                    quantity.setText(String.valueOf(product.getQuantity()));
                } else {
                    quantity.setText("");
                }
                ImageView itemimage = (ImageView) view.findViewById(R.id.imageView);
                itemimage.setImageResource(product.getItemimage());
                TextView itemNumber = (TextView) view.findViewById(R.id.itemNumber);
                itemNumber.setText(product.getTitle());
                TextView description = (TextView) view.findViewById(R.id.description);
                description.setText(product.getDescription());
                return view;

            }

        }

        private class MyTextWatcher implements TextWatcher {

            private View view;

            private MyTextWatcher(View view) {
                this.view = view;
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //do nothing
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //do nothing
            }

            public void afterTextChanged(Editable s) {
                String qtyString = s.toString().trim();
                int quantity = qtyString.equals("") ? 0 : Integer.valueOf(qtyString);

                EditText qtyView = (EditText) view.findViewById(R.id.quantity);
                Product product = (Product) qtyView.getTag();
               product.setQuantity(quantity);

            }
        }
    }`enter code here`

1 个答案:

答案 0 :(得分:0)

我会帮助您解释一些代码和几行代码,但是您可以继续研究和编写代码。

首先,制作课程<GetCaseDetailsResponse xmlns="http://tempuri.org/" xmlns:xsi="xsi"> <Cases> <Name xsi:type="xsd:string">noob</Name> <Description xsi:type="xsd:string">Student</Description> <Ref xsi:type="xsd:string">1234</Ref> <Status xsi:type="xsd:string">Open</Status> </Cases> </GetCaseDetailsResponse> 以扩展AmountDialog。像这样覆盖DialogFragment方法:

OnCreateDialog

在MainActivity中:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        //for this line you must create the layout for your dialog
        View v = inflater.inflate(R.layout.dialog, null);
        //extract view references like this:
        final TextView amount = (TextView) v.findViewById(R.id.amount);//here will be the id of the total 

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Total price")
                .setView(v)
                .setPositiveButton(R.string.OkButton, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //do something with the amount:
                        String amountValue = amount.getText().toString();
                    }
                })
                .setNegativeButton(R.string.CancelButton, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //nothing here, it will dismiss by default
                    }
                });
        return builder.create();

这些将是基本步骤。不要忘记为对话框创建布局。并搜索如何通过活动操纵从对话框中提取的值。