Muhammad Haris
Muhammad Haris

Reputation: 75

How to set Java code in Fragment Activity

this is the code of cafe management which i add in one of fragment in Tablayout i face 3 errors

1) error in line (TextView textView = (TextView) view.findViewById(R.id.qtea); in onclicklistener 2) just view error in show price methods 3)return view

    public class TabFragment1 extends Fragment {


    int counttea = 0;
    int countsamosa = 0;
    and so on

    int teaprice = 0;
    int samosaprice = 0;
    int macroniprice = 0;
    and so on

    int totalprice = 0;
    int sum = teaprice + samosaprice + macroniprice + biryaniprice + pulawoprice + rotiprice + parathaprice + chickenbiryaniprice + chickenqormaprice + lobyaprice + namkeenprice + sandwichprice + chanaprice + shawarmaprice;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab_fragment_1, container, false);


        //For tea
        Button btnTea = (Button) view.findViewById(R.id.btntea);
        btnTea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                counttea = counttea + 1;
                teaprice = counttea * 20;
                TextView textView = (TextView) view.findViewById(R.id.qtea);
                textView.setText("" + counttea);
                showpricetea(teaprice);
                showtotalPrice(sum);

            }
        });

        //For samosa
        Button btnsam = (Button) view.findViewById(R.id.btnsamosa);
        btnsam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                countsamosa = countsamosa + 1;
                samosaprice = countsamosa * 10;
                TextView textView = (TextView) view.findViewById(R.id.qsamosa);
                textView.setText("" + countsamosa);
                showpricesamosa(samosaprice);
                showtotalPrice(sum);

            }
        });
        //For macroni
        Button btnmacroni = (Button) view.findViewById(R.id.btnmacroni);
        btnmacroni.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                countmacroni = countmacroni + 1;
                macroniprice = countmacroni * 50;
                TextView textView = (TextView) view.findViewById(R.id.qmacroni);
                textView.setText("" + countmacroni);
                showpricemacroni(macroniprice);
                showtotalPrice(sum);

            }
        });

     and so on
    }


    public void showpricesamosa(int price) {
        TextView textView1 = (TextView) view.findViewById(R.id.psamosa);
        textView1.setText("" + price);
    }

    public void showpricetea(int price) {
        TextView textView1 = (TextView) view.findViewById(R.id.ptea);
        textView1.setText("" + price);
    }

    public void showpriceroti(int price) {
        TextView textView1 = (TextView) view.findViewById(R.id.proti);
        textView1.setText("" + price);
    }

and so on
    public void showtotalPrice(int price) {
        TextView textView2 = (TextView) view.findViewById(R.id.showtotalprize);
        int total = teaprice + samosaprice + macroniprice + biryaniprice + pulawoprice + rotiprice + parathaprice + chickenbiryaniprice + chickenqormaprice + lobyaprice + namkeenprice + sandwichprice + chanaprice + shawarmaprice;
        textView2.setText("" + total);
    }

    public void clickreset(View view) {

        counttea = 0;
        countsamosa = 0;
        countmacroni = 0;
        countbiryani = 0;
        and so on
        teaprice = 0;
        samosaprice = 0;
        macroniprice = 0;
        and so on
        //for tea
        TextView tea1 = (TextView) view.findViewById(R.id.qtea);
        tea1.setText("00");

        TextView tea2 = (TextView) view.findViewById(R.id.ptea);
        tea2.setText("00");

        //for samosa
        TextView sam1 = (TextView) view.findViewById(R.id.qsamosa);
        sam1.setText("00");

        TextView sam2 = (TextView) view.findViewById(R.id.psamosa);
        sam2.setText("00");

        //for macroni
        TextView mac1 = (TextView) view.findViewById(R.id.qmacroni);
        mac1.setText("00");

      and so on
    }
        return view;


    }

Upvotes: 2

Views: 188

Answers (2)

Uddhav P. Gautam
Uddhav P. Gautam

Reputation: 7636

//For macroni
        Button btnmacroni = (Button) view.findViewById(R.id.btnmacroni);
        btnmacroni.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                countmacroni = countmacroni + 1;
                macroniprice = countmacroni * 50;
                TextView textView = (TextView) view.findViewById(R.id.qmacroni);
                textView.setText("" + countmacroni);
                showpricemacroni(macroniprice);
                showtotalPrice(sum);

            }
        });

     return view; 

    } //before the end of onCreateView body, you should return view.

//here you should write return view

Upvotes: 1

Bhuvanesh BS
Bhuvanesh BS

Reputation: 13617

Don't initialize views inside onClick. Initialize the views in on onCreateView and use it on public void clickreset(View view) method.

Move the below textViews as a global variable.

TextView tea1; //global variable
TextView tea2;
TextView sam1;
TextView sam2;
TextView mac1;

Assign it in onCreatView()

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab_fragment_1, container, false);
tea1 = (TextView) view.findViewById(R.id.qtea);
tea2 = (TextView) view.findViewById(R.id.ptea);
sam1 = (TextView) view.findViewById(R.id.qsamosa);
sam2 = (TextView) view.findViewById(R.id.psamosa);
mac1 = (TextView) view.findViewById(R.id.qmacroni);
}

Hope it helps:)

Upvotes: 2

Related Questions