-1

こんにちは、ユーザーが1つのアクティビティから別のアクティビティに入力した文字列値を渡して、それらの値を2番目のアクティビティのテーブルに表示しようとしていますが、2番目のアクティビティに移動すると、テーブルは表示されますが空です。私の問題が私のコードのどこにあるかを見つけるのに役立つかもしれません、

<h1>Activity 1 where user enters information</h1>  

`package com.example.scheduler;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Save extends Activity implements OnClickListener
{
    Button sbm,bk;
    EditText fname,lname,course,time,inst,title;
    private static final int table = 1810;
    private String newfname,newlname,newcourse,newtitle,newtime,newinst;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newb);
        sbm = (Button) findViewById(R.id.button1);
        sbm.setOnClickListener(this);
        bk = (Button) findViewById(R.id.button2);
        bk.setOnClickListener(this);
        fname = (EditText) findViewById(R.id.editText1);
        lname = (EditText) findViewById(R.id.editText2);
        course = (EditText) findViewById(R.id.editText3);
        time = (EditText) findViewById(R.id.editText4);
        inst = (EditText) findViewById(R.id.editText5);
        title = (EditText) findViewById(R.id.editText6);
    }

    public void onClick(View v)  
    {
        v.getId();
        try
        {
        Intent launch = new Intent(Save.this,table.class);
        Bundle myData = new Bundle();

        if(sbm.isPressed())
        {
            if(fname.getText().toString().length()==0||lname.getText().toString().length()==0||course.getText().toString().length()==0||time.getText().toString().length()==0
            ||inst.getText().toString().length()==0||title.getText().toString().length()==0)
            {
                Toast.makeText(this, "Please fill in all fields!", Toast.LENGTH_LONG).show();
            }else
            {
                newfname = fname.getText().toString();
                newlname = lname.getText().toString();
                newcourse = course.getText().toString();
                newtime = time.getText().toString();
                newinst = inst.getText().toString();
                newtitle = title.getText().toString();
                myData.putString("fname", newfname);
                myData.putString("lname", newlname);
                myData.putString("course", newcourse);
                myData.putString("time", newtime);
                myData.putString("instructor", newinst);
                myData.putString("title", newtitle);
                startActivityForResult(launch,table);
            }
        }
        }catch(Exception e)
        {
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
        if(bk.isPressed())
            {
                finish();
            }
    }
}
`  




<h1>Activity 2 where information is displayed</h1>  

    package com.example.scheduler;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class table extends Activity
{
    TextView titletv,fnametv,lnametv,coursetv,timetv,insttv;

    public void onCreate(Bundle savedInstance)
    {
        super.onCreate(savedInstance);
        setContentView(R.layout.schedule_table);
        titletv = (TextView) findViewById(R.id.titleTextView);
        fnametv = (TextView) findViewById(R.id.fnameTextView);
        lnametv = (TextView) findViewById(R.id.lnameTextView);
        coursetv = (TextView) findViewById(R.id.courseTextView);
        timetv = (TextView) findViewById(R.id.timeTextView);
        insttv = (TextView) findViewById(R.id.instructorTextView);
        try
        {
        Intent myLocalIntent = getIntent();
        Bundle myBundle = myLocalIntent.getExtras();

        String strfname = myBundle.getString("fname");
        String strlname = myBundle.getString("lname");
        String strcourse = myBundle.getString("course");
        String strtime = myBundle.getString("time");
        String strinst = myBundle.getString("instructor");
        String strtitle = myBundle.getString("title");
        titletv.setText(strtitle);
        fnametv.setText(strfname);
        lnametv.setText(strlname);
        coursetv.setText(strcourse);
        timetv.setText(strtime);
        insttv.setText(strinst);
        myLocalIntent.putExtras(myBundle);
        setResult(Activity.RESULT_OK, myLocalIntent);
        }catch(Exception e)
        {
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
        finally
        {

        }
    }
}`
4

4 に答える 4

2

bundleあなたは中に入れなければなりませんIntent

launch.putExtras(myData)
于 2012-11-29T17:19:56.097 に答える
1

あなたは宣言します:

  Bundle myData = new Bundle();

しかし、あなたはテーブルで活動を始めています:

 startActivityForResult(launch,table);

次のように変更します。

  launch.putExtras(myData)
于 2012-11-29T17:21:49.733 に答える
0

このようにメインアクティビティで文字列を宣言できます

static String yourString="";

文字列に値を割り当てます

yourString="what_Ever_Value";

次のような他のアクティビティから呼び出します。

String yourString=packgeName.ClassName.yourString;
于 2012-11-29T17:44:22.533 に答える
0

これがまだ開いていて、表示されている場合は、実際にこれに共有設定を使用する必要があります。http://developer.android.com/guide/topics/data/data-storage.html#pref

インテントよりもシンプルで効率的であり、共有設定を使用すると、データをフラグメントやアクティビティに渡すことができ、その逆も可能です。

于 2013-07-17T13:52:49.000 に答える