2

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

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

4 回答 4

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"));
于 2013-06-13T12:02:18.977 回答
0

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

并使用Bundle bundle=getIntent.getExtra();,

String value=bundle.getExtra("Key");
于 2013-06-13T12:16:59.097 回答
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);
                    }
                });

详细使用Activity

Bundle bundle = getIntent().getExtras();
String value= bundle.getString("data");
details = (EditText) findViewById (R.id.details);
details.setText(value)
于 2013-06-13T12:04:16.040 回答
0

将您想要的值传递FriendsActivityDetailsActivity活动,Intent如下所示。当您通过调用启动另一个活动时,从 FriendsActivity 传递值startActivity()

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();
    }
于 2013-06-13T12:01:30.037 回答