samedi 17 septembre 2016

how to make number picker work in fragment with on clicklistener

I have a tab layout, number picker in the fragment doesn't seem to be working, the values remain 0 always. Basically I am making random number app.

there are three number pickers in my fragment, one gives minimum number, one gives maximum number and one gives quantity of random numbers.

after setting these values in number picker user hits a button to get random numbers.

for me button is not doing anything when the values are set.

Please suggest me how to do this.

My fragment activity

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;


public class TabFragment2 extends Fragment {

    //declare Spinner object
    Spinner generatorType;
    Button getRandomNumberBtn;
    int minimumNumber;
    int maximumNumber;
    int quantity1;
    TextView randNumbText;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.tab_fragment_2, container, false);

        getRandomNumberBtn = (Button) view.findViewById(R.id.get_number);


        NumberPicker miniText;
        final TextView minimumTextView = (TextView) view.findViewById(R.id.textView3);

        miniText = (NumberPicker) view.findViewById(R.id.start_number);
        miniText.setMinValue(0);
        miniText.setMaxValue(10000);
        miniText.setWrapSelectorWheel(true);

        miniText.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal1, int newVal1) {
                //Display the newly selected number from picker
                minimumTextView.setText("Min: " + newVal1);
            }
        });

        minimumNumber = miniText.getValue();


        NumberPicker maxiText;
        final TextView maximumTextView = (TextView) view.findViewById(R.id.textView2);


        maxiText = (NumberPicker) view.findViewById(R.id.end_number);
        maxiText.setMinValue(0);
        maxiText.setMaxValue(10000);
        maxiText.setWrapSelectorWheel(true);

        maxiText.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal2, int newVal2) {
                //Display the newly selected number from picker
                maximumTextView.setText("Max: " + newVal2);
            }
        });

        maximumNumber = maxiText.getValue();










        NumberPicker quantity;
        final TextView quantityTextView = (TextView) view.findViewById(R.id.quantity_text);


        quantity = (NumberPicker) view.findViewById(R.id.quantity_picker);
        quantity.setMinValue(0);
        quantity.setMaxValue(100);
        quantity.setWrapSelectorWheel(true);

        quantity.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal3, int newVal3) {
                //Display the newly selected number from picker
                quantityTextView.setText("Quantity: " + newVal3);
            }
        });

        quantity1 = quantity.getValue();


        randNumbText = (TextView) view.findViewById(R.id.random_number_display);



        getRandomNumberBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int b = maximumNumber + 1;
                int a = minimumNumber;
                int c = quantity1;
                String multipleRandoms = "";

                ArrayList<Integer> list = new ArrayList<Integer>();
                ArrayList<Integer> list1 = new ArrayList<Integer>();
                for (int i = a; i < b; i++) {
                    list.add(new Integer(i));
                }

                int k = list.size();
                Collections.shuffle(list);


                if (k>c) {
                    for (int i = 1; i <= c; i++) {

                        list1.add(new Integer(list.get(i)));
                        multipleRandoms = list1.toString();


                    }
                }else if (k==c) {
                    multipleRandoms = list.toString();
                } else {
                    Toast toast = Toast.makeText(getActivity(),"Quantity can not be greater than numbers in range", Toast.LENGTH_LONG);
                    toast.show();
                }


                randNumbText.setText(multipleRandoms);

            }
        });










        return view;

    }

My fragment layout

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:id="@+id/tab_fragment_2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    tools:context="com.example.tabstrial2.TabFragment2">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="@dimen/activity_vertical_margin">


        <LinearLayout
            android:id="@+id/linear_layout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/generator_type_spinner"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/linear_layout2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="10px"
                    android:text="Min" />

                <NumberPicker
                    android:id="@+id/start_number"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginBottom="20px"
                    android:layout_marginTop="20px" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="2px"
                android:layout_height="match_parent"
                android:background="@android:color/black">

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/quantity_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="10px"
                    android:text="Quantity" />

                <NumberPicker
                    android:id="@+id/quantity_picker"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginBottom="20px"
                    android:layout_marginTop="20px" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="2px"
                android:layout_height="match_parent"
                android:background="@android:color/black">

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="10px"
                    android:text="Max" />

                <NumberPicker
                    android:id="@+id/end_number"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/textView2"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginBottom="20px"
                    android:layout_marginTop="20px" />

            </LinearLayout>

        </LinearLayout>

        <TextView
            android:id="@+id/random_number_display"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/linear_layout1"
            android:layout_centerHorizontal="true"
            android:layout_margin="10px"
            android:textColor="@android:color/black"
            android:textSize="40dp" />


        <Button
            android:id="@+id/get_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/random_number_display"
            android:layout_centerHorizontal="true"
            android:background="@android:color/holo_orange_light"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:onClick="getRandomNumber"
            android:paddingLeft="20px"
            android:paddingRight="20px"
            android:text="Get Random Number" />

    </RelativeLayout>

</ScrollView>




Aucun commentaire:

Enregistrer un commentaire