将Listview选择从一个类传递到另一个类并更改按钮文本

时间:2011-12-04 21:12:15

标签: android

我有一个ListView的课程。一旦用户做出选择,我想将该选择传递给上一课的按钮。这就是我到目前为止所做的:

public class SetupNewCourse extends Activity {
    String[] tees;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setup_new_course);

        final Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent i = new Intent(SetupNewCourse.this, selectTee.class);
                startActivity(i);
                // following code is the hang up I know its in the wrong place
                selectTee buttonText = new selectTee();
                buttonText.returnTeeSelection();
                b.setText((CharSequence) buttonText);
            };
        });
    }
}

以下是我的ListView课程

public class selectTee extends ListActivity {

    String[] tees_list;
    String selectedText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tees_list = getResources().getStringArray(R.array.tees_array);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_view,
                tees_list));
        final ListView teelist = getListView();
        teelist.setChoiceMode(1);
        teelist.setTextFilterEnabled(false);
        teelist.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> teeAdapter, View arg1, int selectedInt, long selectedLong) {
                selectedText = (String) (teelist.getItemAtPosition(selectedInt));
                Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(), Toast.LENGTH_SHORT).show();
                System.out.println(selectedText);
                finish();
            }
        });
    }

    public String returnTeeSelection() {
        return selectedText;
    }
}

任何帮助将不胜感激 提前谢谢。

2 个答案:

答案 0 :(得分:0)

使用startActivityForResult()代替startActivity()。 有关在the documentation of the Activity class中使用此方法的说明。

答案 1 :(得分:0)

除了谢尔盖所说的,

您应该在setResult(int result, Intent data);活动中使用selectTee并设置所选列表项,即您希望使用data.putExtra("selectedText", selectedText)传播回来的数据