将数据从一个活动转移到另一个活动

时间:2013-06-13 11:56:55

标签: android

我有两个活动是朋友和详细信息,朋友活动是列表视图,其中列表数据是从我已创建的数据库填充的,当单击列表项时,应启动详细信息活动以及列表项数据进入详细活动并将其放入详细信息类

中的编辑文本框
package com.rich.myfinal;

public class FriendsActivity extends Activity {

     private CustomCursorAdapter customAdapter;
     private PersonDataHelper databaseHelper;
     private ListView listView;

     private static final String TAG = FriendsActivity.class.getSimpleName();
     /**
     * Called when the activity is first created.
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friends);

        databaseHelper = new PersonDataHelper(this);

        listView = (ListView) findViewById(R.id.list_data);
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d(TAG, "clicked on item: " + position);
                Intent i = new Intent(getApplicationContext(), DetailsActivity.class);
                startActivity(i);
            }
         });

         // Database query can be a time consuming task ..
         // so its safe to call database query in another thread
         // Handler, will handle this stuff for you <img src="http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif?m=1129645325g" alt=":)" class="wp-smiley">

         new Handler().post(new Runnable() {
             @Override
             public void run() {
                 customAdapter = new CustomCursorAdapter(FriendsActivity.this, databaseHelper.getAllData());
                 listView.setAdapter(customAdapter);
             }
         });
     }
}

详细活动在

之下
package com.rich.myfinal;

public class DetailsActivity extends Activity {
    EditText details;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        details = (EditText) findViewById (R.id.details);
    }
}

4 个答案:

答案 0 :(得分:1)

你可以用两种方式做到这一点,一种是使用Bundle,另一种是使用Intent。

使用套装

发送:

Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("Key", "Value");
passIntent.putExtras(bundle);
startActivity(passIntent);

收到:

Bundle bundle = getIntent().getExtras();
String recText = bundle.getString("Key");

使用直通意图:

发送:

Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class);
passIntent.putExtras("Key", "Value");
startActivity(passIntent);

收到:

String recText = getIntent().getExtras().getString("Key"); 

对于您的代码,在您的FirstActivity中

listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Log.d(TAG, "clicked on item: " + position);
                    Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class);
                    passIntent.putExtras("Key", position);
                    startActivity(passIntent);
                }
            });



In SecondActivity,


details.setText(getIntent().getExtras().getString("Key"));

答案 1 :(得分:0)

将您想要的值从FriendsActivity传递到DetailsActivity活动Intent,如下所示。通过调用startActivity()

启动其他活动时,从FriendsActivity传递值
intent.putExtra("pos", v.getId()) ;
intent.putExtra("TranObject",(Serializable) data.get(v.getId()).getTranse()) ;

然后在DetailsActivity

中接收onCreate()中的值,如下所示
    try {
        pos = getIntent().getExtras().getInt("pos");
        blog =(clsTranscation) getIntent().getExtras().getSerializable("TranObject");

    } catch (Exception e) {
        e.printStackTrace();
    }

答案 2 :(得分:0)

像那样使用

 @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Log.d(TAG, "clicked on item: " + position);
                      String value = listView .getItemAtPosition(position).toString();
                        Intent i = new Intent(getApplicationContext(), DetailsActivity.class);
                       i.putExtra("data", value);
                        startActivity(i);
                    }
                });

详细使用活动

Bundle bundle = getIntent().getExtras();
String value= bundle.getString("data");
details = (EditText) findViewById (R.id.details);
details.setText(value)

答案 3 :(得分:0)

只需致电String value=YourList.get(position); 并使用intent.putExtra("Key","Value");

在Intent中传递此内容

使用Bundle bundle=getIntent.getExtra();

获取此值
String value=bundle.getExtra("Key");