开发者

Need to show the result of the selected item in my ListView to my button

开发者 https://www.devze.com 2023-02-16 08:35 出处:网络
I Have a problem: i have a button that when i click is ca开发者_StackOverflow中文版lling another screen and in this screen i have ListView

I Have a problem:

i have a button that when i click is ca开发者_StackOverflow中文版lling another screen and in this screen i have ListView i went to insert in my button the result of the selected item of my ListView

someone have an idea how to do that?

Thanks for your help!!!


You should override method onActivityResult (int requestCode, int resultCode, Intent data) in your first activity and start the second screen via startActivityForResult (Intent intent, int requestCode). When you select the item in list view pass the result via setResult (int resultCode, Intent data) (put data into intent - putExtra method). You can get result from intent, which passed into onActivityResult in first activity UPDATE manifest <application> section

<application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".test1"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="test1.pack.test2">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT"></category>
                <action android:name="test1.act2.START"></action>
            </intent-filter>
        </activity>
    </application>

Screen layout

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button android:text="Button" 
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btn"
android:onClick="onBtnClick"></Button>
</LinearLayout>

1st activity code

public class test1 extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void onBtnClick(View view){
        Intent i = new Intent("test1.act2.START");
        i.putExtra("btnText", "Hello, from activity 1");
        startActivityForResult(i, 0);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data.hasExtra("response")){
            Button b = (Button)findViewById(R.id.btn);
            CharSequence seq = data.getCharSequenceExtra("response");
            b.setText(seq);
        }
    }
}

2nd activity code

public class test2 extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b = (Button)findViewById(R.id.btn);
        b.setText(getIntent().getStringExtra("btnText"));
    }
    public void onBtnClick(View view){
        Intent i = new Intent();
        i.putExtra("response", "Response from second");
        setResult(Activity.RESULT_OK, i);
        finish();
    }
}


You should use , StartActivityForResult() to call another activity and get the result from second Activity to firstActivity.
Check this link
http://developer.android.com/guide/appendix/faq/commontasks.html

search for "Returning a Result from a Screen" in that page. That should help you.


If the other activity is just for the listing of options I suggest you use a spinner in your first activity itself. : http://developer.android.com/reference/android/widget/Spinner.html

0

精彩评论

暂无评论...
验证码 换一张
取 消