Android SetText无法正常运行

时间:2013-08-09 14:30:49

标签: android android-listview settext

我正在尝试从ListView设置EditText的文本。一旦用户单击可点击的EditText,它将打开ListActivity(它包含ListView)。当用户从ListView中选择某些内容时,它将返回到包含可点击的EditText的活动并更改EditText中的文本(希望这是有意义的)。

我遇到的问题是当我从列表中单击某个产品时,它会将我返回到EditText活动,但它不会更改EditText的文本。我一直试图让这个工作2-3天,但仍然无法找到为什么这不起作用。

包含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);

    }

活动包含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();

        }
    });

}

}

1 个答案:

答案 0 :(得分:0)

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 和requestCode == 1