0

我正在尝试从 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);

    }

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

        }
    });

}

}
4

1 回答 1

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

于 2013-08-09T14:57:36.283 回答