4

I'm creating two button with textview ,where if i click plus button the textview should increase and if i decrease vice versa. Now i made one activity where on clicking listview item one dialogbox will appear and it has some checkbox ,button,spinner etc are there. I tried adding code into alert box and my buttons. But it's not working fine, if i click plus button its giving the last limit which is 45 it becomes 46 and if i click the decrease button it just reduces once..

Here is my SubMenu Activity:

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";

    @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.......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);

            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);




  _decrease.setOnClickListener(new View.OnClickListener() {

            int currentID = (int) System.currentTimeMillis();
            int lastID;

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                removeLastTextView();
                lastID--;
            }
        });

        _increase.setOnClickListener(new View.OnClickListener() {

            int currentID = (int) System.currentTimeMillis();
            int lastID;

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                addNewTextView();
                lastID = currentID;
                currentID++;
            }
        });
    }
}



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

here is the image where i want to make spinner like this: i want want to have those image on spinner enter image description here

z.al
  • 279
  • 1
  • 6
  • 21

3 Answers3

5

Replace for Code with this:

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.......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);

            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);
        }
    }
}
Sneha Sarkar
  • 731
  • 6
  • 19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131510/discussion-between-sneha-sarkar-and-z-al). – Sneha Sarkar Dec 26 '16 at 12:50
0

Create an array (not necessarily limited) of IDs of so that you can later find the textViews.

You can do as following

final int currentID  = (int)System.currentTimeMillis();
final int lastID ;

plusButton.setOnClickListener(new OnClickListener(){
@Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            addNewTextView() ;
            lastID  = currentID ;
            currentID++ ; 
        }
}

minusButton.setOnClickListener(new OnClickListener(){
@Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            removeLastTextView() ;
            lastID-- ; 
        }
}

You can take help on how to create new TextView for addNewTextView() from here In the solution you can modify the line that sets the Id by

textView.setId(currentID);

And to remove Views for removeLastTextView() you can take help from here.

Community
  • 1
  • 1
Tahmid Rahman
  • 748
  • 1
  • 8
  • 20
0

Kotlin user Increase or decrease textview number through button in android xml code

<LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:gravity="center"
                    android:layout_gravity="start|center_vertical"
                    android:padding="@dimen/dim_5_dp">
                    <ImageView
                        android:id="@+id/img_minus_vote"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_circle_minus"
                        android:padding="@dimen/dim_5_dp"
                        android:fitsSystemWindows="true"/>
                    <TextView
                        android:id="@+id/txt_num_vote"
                        android:layout_width="60dp"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:textColor="@color/colorSurveyTextBlue"
                        android:fontFamily="@font/font_medium"
                        android:textSize="@dimen/dim_14_sp"
                        android:text="0"
                        android:maxLength="2"
                        android:background="@drawable/solid_white_rectangle_survey"
                        android:padding="@dimen/dim_5_dp"/>
                    <ImageView
                        android:id="@+id/img_plus_vote"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_circle_plus"
                        android:padding="@dimen/dim_5_dp"
                        android:fitsSystemWindows="true"/>

                </LinearLayout>

kotlin file

private var vote: String? = null
private var o = 0

Method call in Initial method and initial method call onViewCreate()

private fun setOnClick(){

    binding.imgPlusVote.setOnClickListener {
        Log.d("TAG", "Increasing value...")
        o += 1
        vote = o.toString()
        binding.txtNumVote.text = vote
        Log.i("TAG", "vote add: $vote")
    }

    binding.imgMinusVote.setOnClickListener {
        Log.d("TAG", "Decreasing value...")
        if (o > 0) {
            o -= 1
            vote = o.toString()
            binding.txtNumVote.text = vote
            Log.i("TAG", "vote minus: $vote")
        } else {
            Log.d("TAG", "Value can't be less than 0")
        }
    }

}