繁体   English   中英

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

[英]getting data from one activity to another

我有两个活动,即朋友和详细信息,朋友活动是一个列表视图,其中列表数据是从我已经创建的数据库中填充的,单击列表项时,应启动details活动并将列表项数据携带到详细信息活动,并放入详细信息类的“编辑”文本框中

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);
    }
}

您可以通过两种方式执行此操作,一种是使用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"));

使用如下所示的IntentDetailsActivity值从FriendsActivityDetailsActivity活动。 当您通过调用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();
    }

这样使用

 @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)

只需调用String value=YourList.get(position); 并使用intent.putExtra("Key","Value");将其传递给Intent intent.putExtra("Key","Value");

并使用Bundle bundle=getIntent.getExtra();获得此值Bundle bundle=getIntent.getExtra();

String value=bundle.getExtra("Key");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM