安卓:数据传递给新活动

2023-09-06 05:25:00 作者:行凶手段

我是新到Android编程,我就是pretty肯定,这正是我需要的。我有一个加载在启动时,有一个微调,一些编辑文本框和两个按钮的页面。一键清除出箱和复位微调。其他的按钮应该装入新的活动(我猜),我需要什么的EditTexts,并通过加载微调。

I'm new to android programming, and I'm pretty sure this is what I need. I've got a page that loads on start that has a spinner, some edit text boxes, and two buttons. One button clears out the boxes and resets the spinner. The other button should load a new activity (I guess) and I need what was in the EditTexts and the spinner to be loaded by that.

现在我把它制作面包,所以我可以测试一切都被正确地抓住。这里是我的活动我现在有:

Right now I have it making a toast so I could test that everything was being grabbed properly. Here is my activity I have now:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class directoryApp extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchscreen);

        //Declaring these so I can use them when the button is clicked.
        final Spinner depts = (Spinner) findViewById(R.id.dept);
        final ArrayAdapter<CharSequence> deptsAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
        deptsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        final EditText fname = (EditText) findViewById(R.id.fname);
        final EditText lname = (EditText) findViewById(R.id.lname);
        final EditText aim = (EditText) findViewById(R.id.aim);
        final EditText phone = (EditText) findViewById(R.id.phone);
        depts.setAdapter(deptsAdapter);
        for(int i = 0 ; i < fillSpinner().length ; i++){
            deptsAdapter.add(fillSpinner()[i]);
        }


        //Search Button on click, makes a "toast" message with grabbed web string and whatever was entered into the fields.
        findViewById(R.id.search).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //Here is where the search button does it's thing.
                Toast.makeText(BNYDirectory.this, fname.getText() + " " + lname.getText() + " " + aim.getText() + " " + phone.getText() + " " + depts.getSelectedItem() + " " + deptIDs()[(int)depts.getSelectedItemId()], Toast.LENGTH_SHORT).show();
            }
        });
    }
}

我需要通过[(int)的depts.getSelectedItemId()](所有这些fname.getText(),lname.getText(),aim.getText(),phone.getText(),和deptIDs()我我得到的字符串)。

I need to pass fname.getText(), lname.getText(), aim.getText(), phone.getText(), and deptIDs()[(int)depts.getSelectedItemId()] (all of which I am getting as strings).

什么是进入下一个页面(我将使用这些得到的结果和显示器)的最好方法?这也有可能是在点击搜索,我可以把它抓住从网页的结果,并把它变成一个字符串数组,只传递,但我宁愿没有这样做。

What's the best way to go to the next page (which I will be using these to get results and display)? It's also possible that on click on the search I can have it grab the results from the web and put it into a string array and only pass that, but I'd rather not do that.

任何例子我可以使用?

推荐答案

您创建一个Intent的第二个活动指定的类名。然后你额外的数据添加到意图,每件有一个唯一的字符串,可作为查找数据的密钥相关的数据。然后,新的活动可以检索使用相同的密钥的数据。请参见打开一个新的屏幕在样品$ C的资源文档的常用任务部分$ C。

You create an Intent specifying the class name for the second activity. Then you add extra data to the intent, with each piece of data associated with a unique string that serves as a key for finding the data. The new activity can then retrieve the data using the same keys. See the section Opening a New Screen in the Common Tasks section of the resources docs for sample code.

相关推荐