我如何在android中设置微调器内的警告对话框?

时间:2016-12-27 07:09:59

标签: android listview android-alertdialog

我正在制作listview,如果您点击任何项目,则应显示alertbox。在alertBox中有微调器,按钮edittext等。我想要在alertbox内部制作微调器,但是我无法为这种情况编写正确的代码。我listitem也来自json.Now请帮我把微调器放在警告对话框中?

这是我的SubMenu活动:

    public class SubMenu extends AppCompatActivity {

    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String RANK = "id";
    static String COUNTRY = "name";

    static String FLAG = "image";
    Integer i = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_sub_menu);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        String SelectedId = getIntent().getStringExtra("id");


        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        // Get the view from listview_main.xml

        // Execute DownloadJSON AsyncTask
        new DownloadJSON().execute();
    }

    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> implements AdapterView.OnItemClickListener {

        // @Override
        //  protected void onPreExecute() {
        //  super.onPreExecute();
        // Create a progressdialog
        //   mProgressDialog = new ProgressDialog(SubMenu.this);
        // Set progressdialog title
        //   mProgressDialog.setTitle("Categories of Main categories.....");
        // Set progressdialog message
        //  mProgressDialog.setMessage("Loading...");
        //  mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        //  mProgressDialog.show();
        // }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonarray = JsonFunctions
                    .getJSONfromURL("http://cloud.granddubai.com/broccoli/menu_typeitem.php?id=" + getIntent().getStringExtra("id"));
            try {
                // Locate the array name in JSON
//                    jsonarray = jsonobject.getJSONArray("main_menu_items");


                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();

                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    // map.put("id", jsonobject.getString("id"));
                    map.put("name", jsonobject.getString("name"));

                    map.put("image", jsonobject.getString("image"));
                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.list1);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(SubMenu.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            listview.setOnItemClickListener(this);
            // Close the progressdialog
            // mProgressDialog.dismiss();
        }


        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long rowId) {

             final  Spinner crust;
            final AlertDialog customDialog = new AlertDialog.Builder(SubMenu.this).create();
            final LayoutInflater inflater = getLayoutInflater();
            final View dialogView = inflater.inflate(R.layout.popup
                    , null);
            customDialog.setView(dialogView);
            // set the custom customDialogimation components - text, image and button
            final TextView tvDistance = (TextView) dialogView.findViewById(R.id.h2);
               crust = (Spinner) dialogView.findViewById(R.id.crst);
            ArrayAdapter adapter = ArrayAdapter.createFromResource(SubMenu.this, R.array.Crust_array, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


            crust.setAdapter(adapter);
         //  crust.setOnItemSelectedListener(this);
// public void addListenerOnSpinnerItemSelection() {





        final Button ok = (Button) dialogView.findViewById(R.id.ok);
            ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    customDialog.dismiss();


                }
            });


            final Button cncl = (Button) dialogView.findViewById(R.id.canc);
            cncl.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    customDialog.dismiss();


                }
            });


            Button _decrease = (Button) dialogView.findViewById(R.id.incr);
            Button _increase = (Button) dialogView.findViewById(R.id.decr);
            final TextView _value = (TextView) dialogView.findViewById(R.id.value);
            i = Integer.parseInt(_value.getText().toString());


            _decrease.setOnClickListener(new View.OnClickListener() {


                public void onClick(View v) {
                    String _stringVal;
                    Log.d("src", "Decreasing value...");
                    if (i > 0) {
                        i = i - 1;
                        _stringVal = String.valueOf(i);
                        _value.setText(_stringVal);
                    } else {
                        Log.d("src", "Value can't be less than 0");
                    }

                }
            });

            _increase.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    String _stringVal;

                    Log.d("src", "Increasing value...");
                    i = i + 1;
                    _stringVal = String.valueOf(i);
                    _value.setText(_stringVal);
                }
            });
            customDialog.show();
        }


    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical">
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_marginTop="10dp"

            android:gravity="center"
            android:orientation="vertical"
            android:paddingBottom="-50dp" >

            <!---add your views here-->
        </LinearLayout>

        <ImageView
            android:id="@+id/imageView_close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|end"
            android:clickable="true"
            android:background="@drawable/crss" />
    </FrameLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dp">

            <ImageView
                android:id="@+id/desimage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/logo" />

            <TextView
                android:id="@+id/h1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="Chicken Pizza Small"
                android:textSize="21sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/h2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="Chicken Pizza Small Combo"
                android:textColor="#8c8181"
                android:textSize="16sp" />


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="10dp"
                android:orientation="horizontal"
                android:weightSum="10">

                <Button
                    android:id="@+id/quant"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginRight="10dp"
                    android:layout_weight="3"
                    android:background="@drawable/mybutton"
                    android:text="Quantity"
                    android:textAllCaps="false"
                    android:textColor="#ffffff" />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="7"
                    android:background="@drawable/stroke_button"
                    android:orientation="horizontal"
                    android:weightSum="3">

                    <Button
                        android:id="@+id/incr"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:background="@android:color/transparent"
                        android:text="-"
                        android:textColor="#FFA726"
                        android:textSize="25dp" />

                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:background="#c0c0c0" />

                    <TextView
                        android:id="@+id/value"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:inputType="number"
                        android:text="1" />

                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:background="#c0c0c0" />

                    <Button
                        android:id="@+id/decr"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:background="@android:color/transparent"
                        android:text="+"
                        android:textColor="#FFA726"
                        android:textSize="25dp" />

                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp">

                <CheckBox
                    android:id="@+id/sl"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:buttonTint="#c0c0c0"
                    android:text="SL"
                    android:textColor="#8c8181"
                    tools:targetApi="lollipop" />

                <CheckBox
                    android:id="@+id/s"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:buttonTint="#c0c0c0"
                    android:text="S"
                    android:textColor="#8c8181"
                    tools:targetApi="lollipop" />

                <CheckBox
                    android:id="@+id/m"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:buttonTint="#c0c0c0"
                    android:text="M"
                    android:textColor="#8c8181"
                    tools:targetApi="lollipop" />

                <CheckBox
                    android:id="@+id/L"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:buttonTint="#c0c0c0"
                    android:text="l"
                    android:textColor="#8c8181"
                    tools:targetApi="lollipop" />

                <CheckBox
                    android:id="@+id/f"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:buttonTint="#c0c0c0"
                    android:text="F"
                    android:textColor="#8c8181"
                    tools:targetApi="lollipop" />

            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:background="#c0c0c0" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="35dp"
                    android:layout_marginLeft="7dp"
                    android:layout_marginRight="15dp"
                    android:layout_marginTop="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/stroke_button">

                    <Spinner
                        android:id="@+id/crst"
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@android:color/transparent"
                        android:textAppearance="?android:attr/textAppearanceSmall" />

                    <ImageView
                        android:id="@+id/img_1"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center_vertical"
                        android:layout_margin="5dp"

                        android:src="@drawable/spinpres" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="35dp"
                    android:layout_marginLeft="7dp"
                    android:layout_marginRight="15dp"
                    android:layout_marginTop="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/stroke_button">

                    <Spinner
                        android:id="@+id/adson"
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@android:color/transparent"
                        android:textAppearance="?android:attr/textAppearanceSmall" />

                    <ImageView
                        android:id="@+id/img_2"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center_vertical"
                        android:layout_margin="5dp"

                        android:src="@drawable/spinpres" />
                </LinearLayout>
            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="SPECIAL INSTRUCTIONS"
                android:textSize="16sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/stroke_button"
                android:padding="@dimen/fab_margin1">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@android:color/transparent"
                    android:gravity="top"
                    android:inputType="textCapSentences|textMultiLine"
                    android:lines="5"
                    android:maxLines="5"
                    android:padding="2dp"
                    android:textColor="#c0c0c0" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"

                android:layout_marginTop="5dp">

                <Button
                    android:id="@+id/canc"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    android:layout_weight="1"
                    android:background="@drawable/mybutton2"
                    android:text="Cancel"
                    android:textColor="#ffffff" />

                <Button
                    android:id="@+id/ok"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="1"
                    android:background="@drawable/mybutton"
                    android:text="Ok"
                    android:textColor="#ffffff" />


            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

这是我的string.xml

<string-array name="AddsOn_array">
        <item>AddsOn</item>
        <item>Extra Meat</item>
        <item>Extra Vegetable</item>

    </string-array>
    <string-array name="Crust_array">
        <item>Crust</item>
        <item>Mushrooms</item>
        <item>Parsely</item>
        <item>Peppers</item>
        <item>Broccoli</item>
        <item>Tomatoes</item>
        <item>Olive</item>
        <item>Onion</item>

    </string-array>

2 个答案:

答案 0 :(得分:1)

用以下代码替换您的代码:

public class SubMenu extends AppCompatActivity {

    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String RANK = "id";
    static String COUNTRY = "name";

    static String FLAG = "image";
    Integer i = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_sub_menu);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        String SelectedId = getIntent().getStringExtra("id");


        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        // Get the view from listview_main.xml

        // Execute DownloadJSON AsyncTask
        new DownloadJSON().execute();
    }

    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> implements AdapterView.OnItemClickListener {

        // @Override
        //  protected void onPreExecute() {
        //  super.onPreExecute();
        // Create a progressdialog
        //   mProgressDialog = new ProgressDialog(SubMenu.this);
        // Set progressdialog title
        //   mProgressDialog.setTitle("Categories of Main categories.....");
        // Set progressdialog message
        //  mProgressDialog.setMessage("Loading...");
        //  mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        //  mProgressDialog.show();
        // }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonarray = JsonFunctions
                    .getJSONfromURL("http://cloud.granddubai.com/broccoli/menu_typeitem.php?id=" + getIntent().getStringExtra("id"));
            try {
                // Locate the array name in JSON
//                    jsonarray = jsonobject.getJSONArray("main_menu_items");


                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();

                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    // map.put("id", jsonobject.getString("id"));
                    map.put("name", jsonobject.getString("name"));

                    map.put("image", jsonobject.getString("image"));
                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.list1);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(SubMenu.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            listview.setOnItemClickListener(this);
            // Close the progressdialog
            // mProgressDialog.dismiss();
        }


        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long rowId) {


            final AlertDialog customDialog = new AlertDialog.Builder(SubMenu.this).create();
            final LayoutInflater inflater = getLayoutInflater();
            final View dialogView = inflater.inflate(R.layout.popup
                    , null);
            customDialog.setView(dialogView);
            // set the custom customDialogimation components - text, image and button
            final TextView tvDistance = (TextView) dialogView.findViewById(R.id.h2);

            //  crust.setOnItemSelectedListener(this);
// public void addListenerOnSpinnerItemSelection() {


            final Button ok = (Button) dialogView.findViewById(R.id.ok);
            ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    customDialog.dismiss();


                }
            });


            final Button cncl = (Button) dialogView.findViewById(R.id.canc);
            cncl.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    customDialog.dismiss();


                }
            });


            Button _decrease = (Button) dialogView.findViewById(R.id.incr);
            Button _increase = (Button) dialogView.findViewById(R.id.decr);
            Spinner spinner1 = (Spinner) dialogView.findViewById(R.id.crust);
            Spinner spinner2 = (Spinner) dialogView.findViewById(R.id.adson);
            ArrayList<String> alspinner1 = new ArrayList<>();
            ArrayList<String> alspinner2 = new ArrayList<>();


            final String[] _spvalue1 = getResources().getStringArray(R.array.Crust_array);
            final String[] _spvalue2 = getResources().getStringArray(R.array.AddsOn_array);
            for (int i = 0; i < _spvalue1.length; i++) {
                alspinner1.add(_spvalue1[i]);
            }
            for (int i = 0; i < _spvalue2.length; i++) {
                alspinner2.add(_spvalue2[i]);
            }
            ArrayAdapter<String> adapter1 = new ArrayAdapter<>(dialogView.getContext(), android.R.layout.simple_spinner_item, alspinner1);
            ArrayAdapter<String> adapter2 = new ArrayAdapter<>(dialogView.getContext(), android.R.layout.simple_spinner_item, alspinner2);

            adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner1.setAdapter(adapter1);
            spinner2.setAdapter(adapter2);

            final TextView _value = (TextView) dialogView.findViewById(R.id.value);
            i = Integer.parseInt(_value.getText().toString());


            _decrease.setOnClickListener(new View.OnClickListener() {


                public void onClick(View v) {
                    String _stringVal;
                    Log.d("src", "Decreasing value...");
                    if (i > 0) {
                        i = i - 1;
                        _stringVal = String.valueOf(i);
                        _value.setText(_stringVal);
                    } else {
                        Log.d("src", "Value can't be less than 0");
                    }

                }
            });

            _increase.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    String _stringVal;

                    Log.d("src", "Increasing value...");
                    i = i + 1;
                    _stringVal = String.valueOf(i);
                    _value.setText(_stringVal);
                }
            });
            customDialog.show();
        }


    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

答案 1 :(得分:0)

使用spinnerbutton edittext等创建布局,并在对话框视图中将此布局设置为: -

Dialog dialog = new Dialog(AddCardScreen.this);
                            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                            dialog.setCancelable(false);
                            dialog.setContentView(R.layout.alert_dialog);

                            Button dialogButton = (Button) dialog.findViewById(R.id.btn_ok);
                            TextView tv_status = (TextView) dialog.findViewById(R.id.tv_status);
                            dialog.show();