Search code examples
javaandroidandroid-fragmentsfragment

i have a fragment contain List View, on a item click how to send position (int) to a new activity


i am creating simple counting app i create a main activity contain tab layout of two tab then each tab contain fragment on fragment i have a list view when and onitem clicked i like to stat a new activity

            Intent intent = new Intent(getActivity(), ButtonCountActivity.class);
            startActivity(intent);

i like to get the listView.setOnItemClickListener int i on ButtonCountActivity how ?

on newActivity i am counting and saving counted data to SQLdatabase i need this (int i) to select clicked item to update

on fragment

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {



            Intent intent = new Intent(getActivity(), ButtonCountActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt("Value",i);
            startActivity(intent);

        }
    });

on Newactivity

        Intent intent= getIntent();
    Bundle bundle = new Bundle();
    bundle = intent.getBundleExtra("value");
    int value = bundle.getInt("value");
    Log.d("T","haha got u " + value  );

i am getting a null value i tried to fix my on i need help very very noob here


Solution

  • You put int in "Value" and try to get it from "value" of course you will get null, both should be "Value" or "value"

    change this

     Intent intent = new Intent(getActivity(), ButtonCountActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("Value",i);
                startActivity(intent);
    

    to this

      Intent intent = new Intent(getActivity(), ButtonCountActivity.class);
                 
                    intent .putExtra("Value",i);
                    startActivity(intent);
    

    change this ..

    Intent intent= getIntent();
        Bundle bundle = new Bundle();
        bundle = intent.getBundleExtra("value");
        int value = bundle.getInt("value");
        Log.d("T","haha got u " + value  );
    

    to this

    int value;
     Bundle extras = getIntent().getExtras();
            if (extras != null) {
                       value = extras.getInt("Value");
    
                   }