-2

In my Adapter there is already an intent to my second activity but it can not passing the value to second activity. How can i get rid out from that? Thank you!! How to pass intent activity to fragment?

First Fragment

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.detailsitem, null);
        }

        if(position ==0) {
        }else {
            final Product myObj = DetailList.get(position);

            final TextView Detailtext = (TextView) convertView.findViewById(R.id.detailsitem);
            Detailtext.setText("" + myObj.getProductName());

            final TextView Detailsprice= (TextView)convertView.findViewById(R.id.detailsprice);
            Detailsprice.setText("" + myObj.getProductPrice());


            preview = (ImageButton) convertView.findViewById(R.id.preview);
            preview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(getActivity().getApplicationContext(),details_gridview_view.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("title", "1");
                    getActivity().getApplicationContext().startActivity(intent);


                    startActivity(intent);
                }
            });

        }
        return convertView;
    }

Second Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.details_gridview_view);

    String name = getIntent().getStringExtra("title");
    String price =getIntent().getStringExtra("price");
    String qty =getIntent().getStringExtra("qty");

    TextView titleTextView = (TextView) findViewById(R.id.title);
    titleTextView.setText(name);
    TextView priceTextView = (TextView) findViewById(R.id.price);
    titleTextView.setText(price);
    TextView qtyTextView = (TextView) findViewById(R.id.qty);
    titleTextView.setText(qty);
Learning Always
  • 1,563
  • 4
  • 29
  • 49
Wei Ming Tang
  • 105
  • 3
  • 14
  • details_gridview_view your activity name should start with uppurcase – Learning Always Dec 05 '16 at 10:10
  • @janki Android resource files cannot contain uppercases. see http://stackoverflow.com/questions/15320951/why-is-it-not-possible-to-use-uppercase-in-naming-resources-in-android – Raymond Dec 05 '16 at 10:14

2 Answers2

2

Instead of getting the stringExtra from the intent you should get the extra's from the intent and from there get the string.

So instead of

String name = getIntent().getStringExtra("title");
String price = getIntent().getStringExtra("price");
String qty = getIntent().getStringExtra("qty");

Use:

Bundle extras = this.getIntent().getExtras();
String name = extras.getString("title");
String price = extras.getString("price");
String qty = extras.getString("qty");

Also take note that you didn't set "price" and "qty"

Also you are starting the activity twice:

getActivity().getApplicationContext().startActivity(intent);
startActivity(intent);

getActivity().getApplicationContext().startActivity(intent); is not required here. startActivity(intent) should do.

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Raymond
  • 2,276
  • 2
  • 20
  • 34
0
Here's the solution of ur problem..

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.details_gridview_view);

    Bundle extras = this.getIntent().getExtras();
    String name = extras.getString("title");
    String price =extras.getString("price");
    String qty =extras.getString("qty");

    TextView titleTextView = (TextView) findViewById(R.id.title);
    titleTextView.setText(name);
    TextView priceTextView = (TextView) findViewById(R.id.price);
    titleTextView.setText(price);
    TextView qtyTextView = (TextView) findViewById(R.id.qty);
    titleTextView.setText(qty);