0

Hey i have a listfragment with this adapter :

    public class F1_fr extends ListFragment {
   View rootview;



    @Override

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootview=inflater.inflate(R.layout.f1_lay,container,false);
        rootview.findViewById(R.id.semi_transparent).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(getActivity(),MainActivity2.class);
                startActivityForResult(intent, 2);

            }
        });


        return rootview;

    }


    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        // check if the request code is same as what is passed  here it is 2
        if(requestCode==2)
        {
            String message=data.getStringExtra("MESSAGE");
            aa.add(message);
        }
    }





    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final String[] items = getResources().getStringArray(R.array.heroes);
        ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, items);

        setListAdapter(aa);




    }

}

and the activity where i get the result from an edit text :

public class ajout extends ActionBarActivity {

EditText editText1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ajout);
    editText1=(EditText)findViewById(R.id.editText1);

}

public void btn (View view)


{
    String message=editText1.getText().toString();
    Intent intent=new Intent();
    intent.putExtra("MESSAGE",message);
    setResult(2,intent);
    finish();

}

It solved but now i have this error : Failure delivering result ResultInfo{who=null, request=131074, result=2, data=Intent { (has extras) }}

SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
BtAndro
  • 71
  • 1
  • 2
  • 8

1 Answers1

0

Variable aa is not available in current scope. This should be solution for your problem:

public class F1_fr extends ListFragment {
  View rootview;
  ArrayAdapter<String> aa;


  public void onActivityResult(int requestCode, int resultCode, Intent data)
  {
    if(requestCode == 2)
    {
        String message=data.getStringExtra("MESSAGE");
        aa.add(message);
    } 
    else 
    {
        super.onActivityResult(requestCode, resultCode, data); 
    }
  }

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final String[] items = getResources().getStringArray(R.array.heroes);
    aa = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, items);

    setListAdapter(aa);
  }
}
skywall
  • 3,956
  • 1
  • 34
  • 52
  • thank you but i have this error now : Failure delivering result ResultInfo{who=null, request=131074, result=2, data=Intent { (has extras) }} – BtAndro Dec 28 '14 at 10:20
  • Can you be more specific? Where is exception thrown? Can you post logcat? In my opinion, your ` Intent data` is null, but it's just guess. – skywall Dec 28 '14 at 10:29
  • here is the logcat :12-28 11:30:40.635 10002-10002/com.example.gweltaz.coffy3 E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131074, result=2, data=Intent { (has extras) }} to activity {com.example.gweltaz.coffy3/com.example.gweltaz.coffy3.Nav}: java.lang.UnsupportedOperationException It crashes when i press the button Nav is the activity where is all the fragment of the navigation drawer – BtAndro Dec 28 '14 at 10:33
  • http://stackoverflow.com/questions/3200551/unable-to-modify-arrayadapter-in-listview-unsupportedoperationexception – skywall Dec 28 '14 at 11:18
  • I'am still getting exactly the same error after doing the same thing – BtAndro Dec 28 '14 at 12:17