将数据从意图传递到片段到活动

时间:2016-12-05 10:02:25

标签: java android android-fragments android-intent

在我的适配器中,已经有了第二个活动的意图,但它无法将值传递给第二个活动。我该如何摆脱它? 谢谢!! 如何将intent活动传递给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;
    }

第二项活动

@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);

2 个答案:

答案 0 :(得分:2)

不是从意图中获取stringExtra,而是从意图中获取额外的内容,然后从中获取字符串。

所以而不是

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

使用:

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

另请注意,您没有设置"price""qty"

你也开始两次活动:

getActivity().getApplicationContext().startActivity(intent);
startActivity(intent);
此处不需要

getActivity().getApplicationContext().startActivity(intent);startActivity(intent)应该这样做。

答案 1 :(得分: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);
相关问题