0

I have this in the main activity :

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String forecast = mForecastAdapter.getItem(position);
                Intent intent = new Intent(getActivity(),DetailActivity.class)
                        .putExtra(Intent.EXTRA_TEXT,forecast);
                startActivity(intent);
            }
        });

the click is working because I tested it with TOAST earlier. I'm trying to pass that forecast string in the DetailActivity fragment, in a text view which initially shows "Hello world!". This is the code that I'm using :

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Intent intent = getActivity().getIntent();
        View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
        if(intent != null && intent.hasExtra(Intent.EXTRA_TEXT)){
           String forecastStr = intent.getStringExtra(Intent.EXTRA_TEXT);
            ((TextView) rootView.findViewById(R.id.detail_text)).setText(forecastStr);
        }


        return rootView;
    }

The problem is that the if is never executed because I tested it with Log.v(TAG,"TestIfStatement");

What am I doing wrong ?

Bogdan Daniel
  • 2,689
  • 11
  • 43
  • 76
  • the problem is that `intent != null` doesn't return true – Bogdan Daniel May 04 '15 at 23:37
  • See this question: http://stackoverflow.com/q/15392261/2543138. In general you don't pass Intents to Fragments, you use Bundles or a callback interface. – brwngrldev May 04 '15 at 23:48
  • I will try that too. I was following steps in the udacity tutorials and this is how they did it, and it was working for them .. I have no idea why my intent doesn't get the value. – Bogdan Daniel May 04 '15 at 23:54

1 Answers1

0

You can try this snippet:

TextView myTextView = (TextView) rootView.findViewById(R.id.detail_text);
myTextView.setText(mForcastStr.toCharArray(), 0, mForcastStr.length());

In works in my application.

Prudhvi
  • 2,276
  • 7
  • 34
  • 54