Search code examples
androidandroid-listviewsettext

Android SetText not functioning


I'm trying to set the text of an EditText from a ListView. Once the user clicks the clickable EditText, it will then open up the ListActivity(That holds the ListView). When the user selects something from the ListView, it will then go back to the activity that holds the clickable EditText and change the text within the EditText(Hopefully that makes sense).

Problem I am having is when I click a product from the list, it then returns me to the EditText activity like it should, but it doesn't change the text of the EditText. I have been trying to get this to work for 2-3 days and still can't seem to find why this isn't working.

Activity that contains the EditText

EditText CPU = (EditText) findViewById(R.id.autoCompleteTextView4);
    CPU.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent CPUList = new Intent(getApplicationContext(),
                    CPUList.class);
            startActivityForResult(CPUList, 1);

        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == 1) {

        Intent i = getIntent();
        String product = i.getStringExtra("product");
        EditText CPU = ((EditText) findViewById(R.id.autoCompleteTextView4));
        CPU.setText (product);

    }

Activity contains ListView

        ListView listViewCPU = (ListView) findViewById(R.id.listViewCPU);
    listViewCPU.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    String CPUList[] = {

    "Products go here", "Products go here", "Products go here", "Products go here"  };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, CPUList);

    listViewCPU.setAdapter(adapter);

    listViewCPU.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> listview, View myView,
                int pos, long mylng) {

            String CPU = (String) listview.getAdapter().getItem(pos);
            Intent i = new Intent();
             i.putExtra("product",CPU);
             setResult(RESULT_OK,i);     
             finish();

        }
    });

}

}

Solution

  • protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
    super.onActivityResult(requestCode, resultCode, data);
    
    if (resultCode == 1) { //HERE PROBLEM
    
        Intent i = getIntent();
        String product = i.getStringExtra("product");
        EditText CPU = ((EditText) findViewById(R.id.autoCompleteTextView4));
        CPU.setText (product);
    
    }
    

    resultCode==RESULT_OK and requestCode==1