Search code examples
androidlistviewandroid-listviewonactivityresult

How to Call onActivityResult in adapter listview?


i'm learning how to change Single item with resultActivity in in my custom Adapter listview. how it's can work ?

this my code when startActivityForResult in custom adapter listview

holder.isi_layout.setOnClickListener(new android.view.View.OnClickListener(){

        public void onClick(View v) 
        {

                            Intent i = null;                                
                            i = new Intent(activity, DetailLaporanActivity.class);                              
                            Bundle b = new Bundle();
                            b.putString("position", Integer.toString(position));

                            i.putExtras(b);
                            activity.startActivityForResult(i, mRequestCode);                                  
                            activity.overridePendingTransition (R.anim.push_left_in, R.anim.push_left_out);


        }
    });                

and this OnResultActivity

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode==mRequestCode) {
            if(resultCode == RESULT_OK){
                String position = data.getStringExtra("position");
                String status_favorite = data.getStringExtra("status_favorite");
                String jumlah_favorite = data.getStringExtra("jumlah_favorite");
                String jumlah_komentar = data.getStringExtra("jumlah_komentar");
            }

            Toast.makeText(getApplicationContext(), "This code Success get Result", Toast.LENGTH_LONG).show();
        }
    }

When i put OnResultActivity in Adapter, code is error, RESULT_OK get notice Cannot be resolved to a variable,

but if i put in MainActivity , this not error but not get value result, i check with Toast.makeText(getApplicationContext(), "This code Success get Result", Toast.LENGTH_LONG).show(); but no toast,....

anybody help me ? how it's work ? sorry, for my english...


Solution

  • You added unresolved RESULT_OK so you should set it in DetailLaporanActivity.class like that.

    public class DetailLaporanActivity extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setResult(RESULT_OK);
    
      }
    }
    

    and than you can use RESULT_OK in your onActivityResult method.