Search code examples
androidsqlitebuttonnotifydatasetchanged

Disable button in a Listview's row while getting response of a save?


Issue: I have a Listview in Mainactivity. Each row of listview has two buttons say SET and RUN. Pressing SET will take you to SET activity and if the user clicks save button in SET Activity, I need to disable the SET button in the corresponding row position of the listview in mainactivity.

So Far Done: For that I have a refresh function on a onclicklistener to requery the list with updated values. How to call that refresh function without keypress in the Mainactivity or is there any other way?

Activity MAIN :

 viewHolder.ButtonSET.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    String title = v.getTag().toString();

                    if (title.equals("SET")) {

                        if (Integer.parseInt((String) viewHolder.TDNQTY.getText()) > 0) {

                            if(scanoverornot(pos)<=0) {

                                Intent s = new Intent(DN.this, SETActivity.class);
                                s.putExtra("position", pos);
                                s.putExtra("mode", "SET");

                                try{

                                    startActivityForResult(s, saverequestcode);
                                    // getContext().startActivity(s);

                                 }
                                catch(Exception e){
                                    Toast.makeText(getContext(),""+e,Toast.LENGTH_LONG).show();
                                }
                           }

                        }
                    }
                }

            });
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (data == null)
            return;

        switch (requestCode) {

            case saverequestcode:

                if (resultCode == RESULT_OK) {

                    String SItem= data.getStringExtra("SItem");
                    int SPos= data.getIntExtra("SPos", 0);
                    saved = 700;
                    Toast.makeText(getApplicationContext(), ""+ SItem+ SPos, Toast.LENGTH_LONG).show();
                    //btnvalidate.performClick();
                }
       }
                   
}

Activity SET :

  Intent sav = new Intent();
  sav.putExtra("SItem", String.valueOf(itemno));
  sav.putExtra("SPos", String.valueOf(pos));
  setResult(RESULT_OK, sav);
  finish();


Solution

  • Issue Lines:

    1. Have to add super.onActivityResult(requestCode, resultCode, data);

    2. Removed switch case and used if condition for requestCode check

    Solution:

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
         
            if (data == null)
                return;
    
            if (requestCode == saverequestcode) {
    
                if (resultCode == Activity.RESULT_OK) {
    
                    String SItem = data.getStringExtra("SItem");
                    String SPos = data.getStringExtra("SPos");
                    
                    Toast.makeText(getApplicationContext(), "Item :" + SItem + "Position :" + SPos, Toast.LENGTH_LONG).show();
                             
                }
                if (resultCode == Activity.RESULT_CANCELED) {
                    //Any methods
                }
            }
            else if (requestCode == importrequestcode){
    
    
            }
    }
    
    

    Activity SET :

      Intent sav = new Intent();
      sav.putExtra("SItem", String.valueOf(itemno));
      sav.putExtra("SPos", String.valueOf(pos));
      setResult(Activity.RESULT_OK,sav);
      finish();