简体   繁体   English

将数据从列表视图传递到主要活动

[英]Pass data from listview to main activity

I have a main activity included the button and textview. 我的主要活动包括按钮和textview。 When I click the button, it will display a listview and fill some data into that listview. 当我单击按钮时,它将显示一个列表视图,并将一些数据填充到该列表视图中。 In the listview, I click a item, I want to send the item data to textview in main activity. 在列表视图中,我单击一个项目,我想将该项目数据发送到主活动中的textview。 However, it has crash. 但是,它崩溃了。 I have no idea to find the problem and log (does not show). 我不知道找到问题并登录(不显示)。 Could you look at my code and provide me the solution? 您能否看一下我的代码并提供解决方案?

MainActivity 主要活动

btnManage = (Button) findViewById(R.id.btnManage);
btnManage.setText(manageLabel);
btnManage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View v) {
        Intent intent = new Intent(v.getContext(), ListViewActivity.class);
        intent.putExtra("List_data", "Hello");
        v.getContext().startActivity(intent);

    }
});

In ListViewActivity I have 在ListViewActivity中,我有

Bundle bundle = getIntent().getExtras();
String data_String= bundle.getString("List_data");
ArrayList<String> data = new ArrayList<String>();
data.add(data_String);
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, data);    
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter ); 
//Click one item
public void onItemClick(AdapterView<?> parent, View view, int position,
      long id) {

String  data_send    = (String) mainListView.getItemAtPosition(position);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("Data_Send", data_send);
startActivity(intent);
 }

In the onCreate function in MainActivity, I have 在MainActivity的onCreate函数中,

Bundle bundle = getIntent().getExtras();
String data_activity =bundle.getString("Data_Send");

The Manifest file 清单文件

    <activity
        android:name=".MainActivity"
        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=".ListViewActivity"
        android:label="@string/app_name" 
        android:screenOrientation="portrait"
    />

In ListViewActivity you are trying to start MainActivity which is already started hence the error. 在ListViewActivity中,您尝试启动已经启动的MainActivity,因此出现错误。

You need to finish ListViewActivity acivity so it just goes back to the previous activity which is the MainActivity and pass data 您需要完成ListViewActivity活动性,以便它回到上一个活动(即MainActivity)并传递数据

in ListViewActivity you finish the activity and pass data back like this 在ListViewActivity中,您可以完成活动并像这样将数据传递回去

Intent intent = new Intent();
intent.putExtra("Data_Send", data_send);
setResult(RESULT_OK, intent);
finish();

in MainActivity you start the ListViewActivity like this 在MainActivity中,您可以像这样启动ListViewActivity

Intent i = new Intent(getApplicationContext(), ListViewActivity.class);
i.putExtra("List_data", "Hello");
startActivityForResult(i, 1);

also in MainActivity you need to get data back from the ListViewActivity so you add 同样在MainActivity中,您需要从ListViewActivity中获取数据,因此您需要添加

public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                String data = data.getStringExtra("Data_Send");

              // do whatever with data string
}}};

The if (requestCode == 1) { is the number id you give when you start an activity with startActivityForResult(i, 1); if (requestCode == 1) {是您使用startActivityForResult(i, 1);启动活动时给出的数字ID startActivityForResult(i, 1); . If you want to do the same with another activity you can do startActivityForResult(i, 2); 如果要对另一个活动执行相同的操作,则可以执行startActivityForResult(i, 2); and to get back data you check for id 2 if (requestCode == 2) { ... 并要获取数据,请检查ID 2 if (requestCode == 2) { ...

  1. after clicking and list item if you want to finish the list activity then you can do one thing. 单击并列出项目后,如果您想完成列表活动,则可以做一件事。 you can start the ListActivity using startActivityForResult . 您可以使用startActivityForResult启动ListActivity。 then when the list item is clicked then you can send result to the main activity. 然后,当单击列表项时,您可以将结果发送到主活动。

to know more about it you can check the doc 要了解更多信息,可以查看文档

  1. if you dont want to finish the List activity immediately after list item click then you can do some other things. 如果您不想在单击列表项后立即完成“列表”活动,则可以执行其他操作。 a. 一种。 you can send a broadcast to the main activity and receive the broadcast. 您可以将广播发送到主要活动并接收广播。 b. as your mainactivity's text view will show only one item then you can store the item in a public variable 因为您的mainactivity的文本视图将仅显示一个项目,然后您可以将该项目存储在公共变量中

You should have a look at the following guide which outlines how to start an activity, and then wait for a result. 您应该查看以下指南该指南概述了如何开始活动,然后等待结果。

The problem with the way you are currently doing is that the "back stack" has 2 references to the main activity - ie hitting back will take you to the list activity, then to main activity again. 当前操作方式的问题在于,“后退堆栈”对主活动有2个引用-即回击将使您进入列表活动,然后再次访问主活动。

The advantage of using startActivityForResult() allows you to separate out the logic of creating the MainActivity , and handling the result of the ListViewActivity . 使用startActivityForResult()的优点是,您可以分离出创建MainActivity和处理ListViewActivity的结果的逻辑。

You can use startActivityForResult for this purpose. 为此,您可以使用startActivityForResult。 When you call Activity.startActivityForResult(), you set the requestCode. 当您调用Activity.startActivityForResult()时,您将设置requestCode。 Later, this request code is needed by onActivityResult() in order to determine what Activity is sending data to it. 稍后,onActivityResult()需要此请求代码,以确定哪些Activity向其发送数据。 We don't need to supply requestCode again on setResult() because the requestCode is carried along. 我们不需要在setResult()上再次提供requestCode,因为requestCode是随身携带的。

The data is intent data returned from launched intent. 数据是从启动的意图返回的意图数据。 We usually use this data when we set extras on the called intent. 当我们在被调用的Intent上设置额外内容时,通常会使用此数据。

Consider this example: 考虑以下示例:

CALL SECOND ACTIVITY 呼叫第二活动

Intent i = new Intent(MainActivity.this, CheckActivity.class);
startActivityForResult(i, REQUEST_CODE_CHECK);

ON SECOND ACTIVITY, SET INTENT RESULT 在第二个活动上,设置初始结果

getIntent().putExtra("TADA", "bla bla bla");
setResult(RESULT_OK, getIntent());
finish();

BACK TO FIRST ACTIVITY, ONACTIVITYRESULT() 返回第一个活动,ONACTIVITYRESULT()

if(requestCode == REQUEST_CODE_CHECK && resultCode == RESULT_OK){
    text1.setText(data.getExtras().getString("TADA") );
}

There you go. 妳去 You should now understand what is Intent data and how to set and fetch the value. 现在,您应该了解什么是Intent数据以及如何设置和获取值。

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

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