skip to Main Content

I want to return the data from second activity to first activity using intent when click the OnAddClicked button in second activity, but what I done was not working. The data that I return to first activity is null and I have no idea about this. Can anyone help me to solve this problem?

First Activity:

public class CartView extends AppCompatActivity {

    ActivityCartViewBinding binding;
    public CartView.MyClickHandler handler;
    GridView view_listView;
    String Default_curr;
    String itemgroup;
    String itemtype;
    String uprice;
    String quantity;
    ACDatabase db;
    List<AC_Class.Item> s_item = new ArrayList<>();
    AC_Class.InvoiceDetails invoiceDetails;
    AC_Class.Item item;
    String substring = "";
    EditText searchEditText;
    Button All;
    Button Type;
    Button Group;
    ArrayAdapter<String> adapter;
    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_cart_view);

        // Action Bar
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("Catalog");
        actionBar.setDisplayHomeAsUpEnabled(true);
        searchEditText = (EditText) findViewById(R.id.searchField);
        UIUtil.hideKeyboard(this);
        UIUtil.showKeyboard(this, searchEditText);

        db = new ACDatabase(this);
        invoiceDetails = new AC_Class.InvoiceDetails();
        item = new AC_Class.Item();
        handler = new MyClickHandler(this);
        binding.setHandler(handler);
        All = (Button) findViewById(R.id.button1);
        Type = (Button) findViewById(R.id.button2);
        Group = (Button) findViewById(R.id.button3);
        view_listView = (GridView) findViewById(R.id.GridView);

        All.setVisibility(View.GONE);
        Type.setVisibility(View.GONE);
        Group.setVisibility(View.GONE);

        getData("");

        searchEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {}

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

            @Override
            public void afterTextChanged(Editable s) {

                getData(s.toString().trim());
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.ctlg_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        /*if (id == android.R.id.home)
        {
            onBackPressed();
            return true;
        }*/
        if (id == R.id.cart)
        {
            Intent new_intent = new Intent(CartView.this, CartList.class);
            new_intent.putExtra("invoiceDetail", invoiceDetails);
            startActivityForResult(new_intent,3);

        }
      
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData("");
    }

    public void getData(String substring) {
        Cursor data = db.getItemLike(substring, 0);
        if (data.getCount() > 0){
            s_item.clear();
            while (data.moveToNext()) {
                try {
                    AC_Class.Item item = new AC_Class.Item(data.getString(0), data.getString(1), data.getString(2), data.getString(3), data.getString(4), data.getString(5), data.getString(6), data.getString(7), data.getString(8), data.getFloat(9), data.getFloat(10), data.getFloat(11), data.getFloat(12), data.getFloat(13), data.getFloat(14), data.getString(15), data.getString(16), data.getFloat(17), data.getString(18),data.getFloat(19),data.getFloat(20));

                    s_item.add(item);
                } catch (Exception e) { Log.i("custDebug", "error reading image: "+e.getMessage()); }
            }
            CartViewListAdapter arrayAdapter = new CartViewListAdapter(this, s_item);
            view_listView.setAdapter(arrayAdapter);
            Cursor dcurren = db.getReg("6");
            if(dcurren.moveToFirst()){
                Default_curr = dcurren.getString(0);
            }
            view_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String itemcode = ((AC_Class.Item)parent.getItemAtPosition(position)).getItemCode();
                    String uom = ((AC_Class.Item)parent.getItemAtPosition(position)).getUOM();
                    AC_Class.Item sa =((AC_Class.Item) parent.getItemAtPosition(position));
                    //Intent item_intent = new Intent();
                    Intent item_intent = new Intent(CartView.this, ItemDetail.class);
                    item_intent.putExtra("ItemKey",itemcode);
                    item_intent.putExtra("ItemUOMKey",uom);
                    item_intent.putExtra("Items",sa);
                    //setResult(4, item_intent);
                    startActivity(item_intent);
                    //finish();
                }
            });
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {

            case 1:
                if (resultCode == 1) {
                    itemtype = data.getStringExtra("TypeKey");

                    if (itemtype != null) {
                        getTypedata();
                    }
                }
                break;

            case 2:
                if (resultCode == 1) {
                    itemgroup = data.getStringExtra("GroupKey");
                    if(itemgroup != null){
                        getGroupdata();
                    }

                }
                break;

            case 3:
                if (resultCode == 1) {
                    AC_Class.Item i = data.getParcelableExtra("Item");
                    uprice = data.getStringExtra("Price");
                    quantity = data.getStringExtra("Quantity");

                    if (i != null) {
                        invoiceDetails.setItemCode(i.getItemCode());
                        invoiceDetails.setItemDescription(i.getDescription());
                        invoiceDetails.setUPrice(Double.valueOf(i.getPrice()));
                        invoiceDetails.setUOM(i.getUOM());
                        //invoiceDetails.setUPrice(Double.valueOf(uprice));
                        invoiceDetails.setQuantity(Double.valueOf(quantity));
                    }
                }
                break;
        }
    }
}

SecondActivity:

public class ItemDetail extends AppCompatActivity {
    ActivityItemDetailBinding binding;
    AC_Class.Item items;
    AC_Class.ItemUOM itemuom;
    AC_Class.Cart cart;
    AC_Class.InvoiceDetails invoiceDetails;
    ACDatabase db;
    String Item;
    String ItemUOM;
    String Default_curr;
    Intent pintent;
    EditText etQty;
    String default_loc;
    List<AC_Class.Item> c_item = new ArrayList<>();
    MyClickHandler handler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_item_detail);

        itemuom = new AC_Class.ItemUOM();
        items = new AC_Class.Item();
        invoiceDetails = new AC_Class.InvoiceDetails();
        cart = new AC_Class.Cart();
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        db = new ACDatabase(this);
        pintent = getIntent();
        //substring = pintent.getStringExtra("substring");
        Item = pintent.getStringExtra("ItemKey");
        ItemUOM = pintent.getStringExtra("ItemUOMKey");

        handler = new MyClickHandler(this);
        binding.setHandler(handler);

        Cursor loc = db.getReg("7");
        if(loc.moveToFirst()){
            default_loc = loc.getString(0);
        }
     
        items = pintent.getParcelableExtra("Items");
        binding.setItem(items);
        
        binding.setInvoicedetail(invoiceDetails);

     


        binding.quantity.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                etQty = (EditText)findViewById(R.id.quantity);

                String qty = etQty.getText().toString();

                if(qty.length() > 0)
                {
                    invoiceDetails.setQuantity(Double.valueOf(qty));
                    //Calculation();
                }
            }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home: {
                onBackPressed();
                return true;
            }
        }
        return super.onOptionsItemSelected(item);
    }

    public class MyClickHandler {
        Context context;

        public MyClickHandler(Context context) {
            this.context = context;
        }
 

        public void OnAddClicked(View view) {
            //Intent new_intent = new Intent(context, CartList.class);
            Intent new_intent = new Intent(context,CartView.class);
            new_intent.putExtra("Item", items);
            new_intent.putExtra("Quantity",invoiceDetails.getQuantity());
            new_intent.putExtra("Price",invoiceDetails.getUPrice());
            setResult(1, new_intent);
            finish();

        }
    }
}

2

Answers


  1. OnAddClicked your are finishing the activity when it goes to next activity previous activity goes to end….so remove the finish();

    Login or Signup to reply.
  2. First thing first, to get result from other activity, you need to Use startActivityForResult method instead of startActivity.

     Intent item_intent = new Intent(CartView.this, ItemDetail.class);
                        item_intent.putExtra("ItemKey",itemcode);
                        item_intent.putExtra("ItemUOMKey",uom);
                        item_intent.putExtra("Items",sa);
                       
                        //startActivity(item_intent);
                        startActivityForResult(item_intent,REQUST_CODE);
    

    and for setting result

    Intent new_intent = new Intent();
                new_intent.putExtra("Item", items);
                new_intent.putExtra("Quantity",invoiceDetails.getQuantity());
                new_intent.putExtra("Price",invoiceDetails.getUPrice());
                setResult(Activity.RESULT_OK, new_intent);
                finish();
    

    and to receive result use below code.

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    if(REQUST_CODE==requestCode && resultCode==Activity.RESULT_OK){
    // do logical stuff.
    }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search