Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将EditText值提取到对话框中,还需要添加用户输入的值_Android_Listview_Android Edittext_Android Alertdialog_Addition - Fatal编程技术网

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

Android 将EditText值提取到对话框中,还需要添加用户输入的值,android,listview,android-edittext,android-alertdialog,addition,Android,Listview,Android Edittext,Android Alertdialog,Addition,嗨,我试着在点击“添加”按钮时打开对话框 在此之前,我想给你一个小的解释,我需要实现什么。这里我有listView项目。该列表包括项目名称和编辑文本。用户可以在此编辑文本中输入值。假设用户在编辑文本中输入三个项目值(例如:Tomoto 30、Soup 40、Biriyani 50)。此外,还有一个“添加”按钮,当单击时,该按钮会在同一屏幕上显示一个带有对话框的弹出窗口。对话框内部必须显示项目名称和用户输入的值,上述示例中也需要这些合计值(Tomoto 30、Soup 40、Biriyani 50

嗨,我试着在点击“添加”按钮时打开对话框

在此之前,我想给你一个小的解释,我需要实现什么。这里我有listView项目。该列表包括项目名称和编辑文本。用户可以在此编辑文本中输入值。假设用户在编辑文本中输入三个项目值(例如:Tomoto 30、Soup 40、Biriyani 50)。此外,还有一个“添加”按钮,当单击时,该按钮会在同一屏幕上显示一个带有对话框的弹出窗口。对话框内部必须显示项目名称和用户输入的值,上述示例中也需要这些合计值(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`
===========
my_row.xml
===========
activity_main.xml
公共类MainActivity扩展了AppCompatActivity{
私有MyCustomAdapter dataAdapter=null;
公共按钮;
公共编辑文本;
文本视图文本视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayListView();
按钮=(按钮)findViewById(R.id.btnAdd);
EditText=(EditText)findViewById(R.id.quantity);
textView=(textView)findViewById(R.id.description);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
AlertDialog.Builder alertdialogbuilder=新建AlertDialog.Builder(MainActivity.this);
alertdialogbuilder.setTitle(“选定项”);
}
});
}
私有void displayListView(){
//阵列产品列表
ArrayList productList=新的ArrayList();
产品=新产品(R.drawable.ic_汤、“Tomoto”、“desc”);
productList.add(产品);
产品=新产品(R.drawable.ic_汤、“汤”、“描述”);
productList.add(产品);
产品=新产品(R.drawable.ic_biriyani,“biriyani”,“desc”);
productList.add(产品);
产品=新产品(R.drawable.ic_汤、“辣椒”、“描述”);
productList.add(产品);
产品=新产品(R.drawable.ic_汤、“粉末”、“描述”);
productList.add(产品);
产品=新产品(R.drawable.ic_汤、“苹果”、“描述”);
productList.add(产品);
产品=新产品(R.drawable.ic_汤、“橙色”、“描述”);
productList.add(产品);
产品=新产品(R.drawable.ic_汤、“香蕉”、“描述”);
productList.add(产品);
产品=新产品(R.drawable.ic_汤、“芒果”、“描述”);
productList.add(产品);
//从字符串数组创建ArrayAdaptar
dataAdapter=新的MyCustomAdapter(此,R.layout.my_行,productList);
ListView ListView=(ListView)findViewById(R.id.listView1);
//将适配器分配给ListView
setAdapter(dataAdapter);
}
私有类MyCustomAdapter扩展了ArrayAdapter{
私有ArrayList产品列表;
公共MyCustomAdapter(上下文,int textViewResourceId,
ArrayList产品列表){
超级(上下文、textViewResourceId、productList);
this.productList=新的ArrayList();
this.productList.addAll(productList);
}
@凌驾
公共视图getView(内部位置、视图视图、视图组父视图){
产品=产品列表。获取(位置);
如果(视图==null){
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
视图=vi.充气(R.layout.my_row,空);
EditText数量=(EditText)view.findViewById(R.id.quantity);
//将TextWatcher侦听器附加到EditText
addTextChangedListener(新的MyTextWatcher(视图));
}
EditText数量=(EditText)view.findViewById(R.id.quantity);
数量.设置标签(产品);
if(product.getQuantity()!=0){
quantity.setText(String.valueOf(product.getQuantity());
}否则{
数量。setText(“”);
}
ImageView itemimage=(ImageView)view.findViewById(R.id.ImageView);
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());
返回视图;
}
}
私有类MyTextWatcher实现TextWatcher{
私人视野;
私有MyTextWatcher(查看){
this.view=视图;
}
更改前文本之前的公共void(字符序列s、int start、int count、int after){
//无所事事
}
public void onTextChanged(字符序列、int start、int before、int count){
//无所事事
}
公共无效后文本已更改(可编辑){
字符串qtyString=s.toString().trim();
int QUOTE=qtyString.equals(“”?0:Integer.valueOf(qtyString);
编辑文本
    @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();
    button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //note: import android.support.v4.app.FragmentManager;!!!
              FragmentManager fm = getSupportFragmentManager();
              AmountDialog alert = new AmountDialog ();
              alert.show(fm, "AmountDialog");
                    }
                });