0

I have a listview in a class that should pass data to another class however it seems I am doing it wrong. Class A contains the listview and through an intent it sends the data back. Class B must receive the data and pass to a field. For that I use a bundle which contain the string and must pass it. However it seems I am doing something wrong. Any hints?

Class A.

public static final String Item = "shop";

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String item3 = (String)arrayAdapter.getItem(position).toString();
            Intent intent = new Intent(getActivity().getApplicationContext(),ClassB.class);
            intent.putExtra(Item,item3);
            startActivity(intent);
            //Toast.makeText(getActivity().getApplicationContext(), "msg msg", Toast.LENGTH_LONG).show();
        }
    });

Class B.

Bundle argsss = new Bundle();
    argsss = getActivity().getIntent().getExtras();
    if(argsss != null){
        String shop = argsss.getString(ClassA.Item);
        testButton.setText(shop);
    }

Stacktrace :

 Process: nl.boydroid.loyalty4g.app, PID: 27526
android.content.ActivityNotFoundException: Unable to find explicit activity class {nl.boydroid.loyalty4g.app/nl.boydroid.loyalty4g.app.redeempoints.RedeemItemFragment}; have you declared this activity in your AndroidManifest.xml?
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1636)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1430)
        at android.app.Activity.startActivityForResult(Activity.java:3532)
        at android.app.Activity.startActivityForResult(Activity.java:3493)
        at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:849)
        at android.support.v4.app.Fragment.startActivity(Fragment.java:880)
        at nl.boydroid.loyalty4g.app.redeempoints.SearchableShopList$1.onItemClick(SearchableShopList.java:90)
        at android.widget.AdapterView.performItemClick(AdapterView.java:308)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1524)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3531)
        at android.widget.AbsListView$3.run(AbsListView.java:4898)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5586)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at dalvik.system.NativeStart.main(Native Method)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Zakdroid
  • 102
  • 7

4 Answers4

3

This should work:

String shop = getActivity().getIntent().getStringExtra(ClassA.Item);

Problem with your code it that you're binding String into Intent object directly and not into Bundle. This is reason why your code is not working. Your Bundle is empty.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • I get your logic but this didnt work for me, it says there is an error at startActivity(intent), there is still something wrong with my structure i guess. – Zakdroid Dec 10 '14 at 09:43
  • Btw both classes are Fragment it doesn't extend Activity. Your help would be welcome – Zakdroid Dec 10 '14 at 09:46
  • So this is very important to say. You should say it immediately. If your classes are Fragments so why you are using starActivity()? I don't understand what you're trying to do. – Simon Dorociak Dec 10 '14 at 09:48
  • Apologies. Well the data must be sent when you click on an item in the listview. From what i saw on the internet the best way to do this is through intents. if you have a solution i am eager to hear. – Zakdroid Dec 10 '14 at 09:49
  • @Zakdroid sorry man but i have no time to give you new solution. You need to read something about Fragments. So i apologize to you. – Simon Dorociak Dec 10 '14 at 10:13
  • No need to apologize, gonna search it out. If i come to a solution will ad it here. – Zakdroid Dec 10 '14 at 10:16
0

you have sent data through the intent not bundle so get the data in other activity as intent

String shop = getActivity().getIntent().getStringExtra(ClassA.Item);
Pramod Yadav
  • 2,316
  • 2
  • 23
  • 36
0

note: Im not that knowledgeable in android so i dont know about Bundle class nor Intent class just want to share some thoughts.

Try this.

 String shop = argsss.getString("shop");

the value of the Item which is your key (shop) because of this.

  public static final String Item = "shop";

look at this ,

   //you are putting or passing the value of item3 to Item which is your key named "shop"
   intent.putExtra(Item,item3);

so you can only get the value using the key you set.

Update

try this

    // first create a bundle for you to be able to use it later.
    Bundle sampleBundle = new Bundle();

    // then set the values you want. with the Item key.
    sampleBundle.putString(Item, item3);

    // then create the intent
    Intent intent = new Intent(getActivity().getApplicationContext(),ClassB.class);

    // then put your bundle to intent.
    intent.putExtras(sampleBundle);
    startActivity(intent);

to get this try this.

    Bundle bundle = this.getIntent().getExtras(); 

    if(bundle !=null){
            //Obtain Bundle 
        String strdata = bundle.getString("shop"); 
            //do the process
    }

note again: i havent compile it i dont have ide for android its just a hint.

or

if you want to use only intent to pass some value

use your code above then on the other class you can get it by :

Intent intent = getIntent();

//this is how you retrieve the data 
String shop = intent.getStringExtra("shop");

Additional ref.

Intent

Fragment to Fragment Try this dude. :)

Fragment to Fragment . Look at the last answer it may help you finding some ways on how fragments work.

Another possible solution Passing data fragment to fragment

Additional info.

I think you cant pass data from fragment to another fragment directly.

Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

Question.

//note: not sure about the flow of the program. just want to ask. 

     ClassA extends Activity                             ClassB extends Activity 
             ||                                                    ||
 ClassA call the FragmentClassA?                   FragmentClassB startActivity(ClassB)?
             ||                                                    ||
       FragmentClassA       you want to pass data --> ?       FragmentClassB

   after that, you want to start the ClassB Activity class inside FragmentClassB?
Community
  • 1
  • 1
Secondo
  • 451
  • 4
  • 9
  • Hmmm i understand your logic, i had the same thoughts. But the questio now is : How to solve this? – Zakdroid Dec 10 '14 at 09:48
  • @zakdroid try to look on my updates .. well im not sure it will work. just want to share my thoughts. – Secondo Dec 10 '14 at 09:58
  • Just tried it, it still gives the same error at startActivity(intent), it says unable to find explicit activity class did you declare in manifest? your thoughts are welcome, programming is made to fun :) – Zakdroid Dec 10 '14 at 10:09
  • post the full stacktrace :) – Secondo Dec 10 '14 at 10:10
  • have you look on my additional ref. ? – Secondo Dec 10 '14 at 10:11
  • you said theyre not extending the Activity class hmm dont have any idea how to fix fragment class. XD – Secondo Dec 10 '14 at 10:14
  • Just added the stacktrace, tried the additional ref aswell, still the same result.. Well i'll try to search it out. – Zakdroid Dec 10 '14 at 10:15
  • `android.content.ActivityNotFoundException: Unable to find explicit activity class {nl.boydroid.loyalty4g.app/nl.boydroid.loyalty4g.app.redeempoints.RedeemItemFragment}; have you declared this activity in your AndroidManifest.xml?` based on this i think there are some changes needed on your androidmanifest.xml. try to look on it first. – Secondo Dec 10 '14 at 10:16
  • Yeah but it's not an activity so declaring it in the manifest won't help – Zakdroid Dec 10 '14 at 10:23
  • wait gonna get back to this :) were having a practice for christmas party performance. – Secondo Dec 10 '14 at 10:28
0

Bundle add the object as serializable and send it to another fragment.

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
Ramesh sambu
  • 3,577
  • 2
  • 24
  • 39