0

したがって、基本的には ListView と、データを保持している 2 つの EditText を持つ 2 つ目のアクティビティがあります (メモ アプリケーションのようなものです)。ListView はそれらが作成されたことを示していますが、ListView のアイテムをクリックすると、データのない空白の 2 番目のアクティビティが表示されます。これを修正するためにバンドルを使用する複数の方法を試しましたが、これまでのところ何も表示されません。

これが私の最初のアクティビティ(ListViewアクティビティ)のために現在持っているものです...

    List.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Intent launchActivity = new Intent(MainActivityPlanes.this, MainActivityPaper.class);

            public static final String keyname = "android.application.project.planes.MainActivityPlanes.Extra_keyname1";
            Bundle b = new Bundle();
            String keyname = title1.getText().toString();
            String key = text1.getText().toString(); 
            b.putString("Extra__keyname", keyname);
            b.putString("Extra__key", key);
            launchActivity.putExtras(b);

            startActivity(launchActivity);

2 番目のアクティビティ (2 つの TextView を保持するアクティビティ) のバンドルの 2 番目のコードは次のとおりです...

    Intent launchActvity = getIntent();

    Bundle b = launchActvity.getExtras();

    String keyname = launchActvity.getStringExtra(MainActivityPlanes.Extra_keyname);
    String key = launchActvity.getStringExtra(MainActivityPlanes.Extra_key);

    TextView title1 = (TextView)findViewById(R.id.editText3);
    TextView text1 = (TextView)findViewById(R.id.editText1);

    title1.setText("keyname");
    text1.setText("key");

これを修正する方法について何か考えはありますか?

最初の活動

 package android.application.project.planes;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
 import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

 public class MainActivityPlanes extends Activity {

Button Add;
ListView List;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main_activity_planes);

    Add = (Button) findViewById(R.id.button1);
    List = (ListView) findViewById(R.id.listView1);

    Add.setOnClickListener(new OnClickListener () {

        public void onClick (View v) {
            Intent intent = new Intent (v.getContext(), MainActivityPaper.class);
            startActivityForResult(intent, 0);
        }

    });

    List.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Intent launchActivity = new Intent(MainActivityPlanes.this, MainActivityPaper.class);

             Bundle b = new Bundle();
             String keyname = title1.getText().toString();
             String key = text1.getText().toString(); 
             b.putString("Extra__keyname", keyname);
             b.putString("Extra__key", key);
             launchActivity.putExtras(b);

             startActivity(launchActivity);

        }

    });

    MainActivityChampagne info = new MainActivityChampagne(this);       
    info.open();        
    ArrayList<String> data = info.getData();
    info.close();
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, data);
    List.setAdapter(arrayAdapter);

}

}

第二の活動

package android.application.project.planes;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class MainActivityPaper extends Activity implements OnClickListener {

TextView text1, title1;
Button save1, update1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main_activity_paper);

    text1 = (TextView) findViewById(R.id.editText1);
    title1 = (TextView) findViewById(R.id.editText3);
    save1 = (Button) findViewById(R.id.button3);
    update1 = (Button) findViewById(R.id.button5);

    save1.setOnClickListener(this);
    update1.setOnClickListener(this);

}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

    switch (arg0.getId()) {
    case R.id.button3:

        String titles = title1.getText().toString();
        String notes = text1.getText().toString();

        MainActivityChampagne entry = new MainActivityChampagne(MainActivityPaper.this);
        entry.open();
        entry.createEntry(titles, notes);
        entry.close();

        break;

    case R.id.button5:

        String thetitles = title1.getText().toString();
        String thenotes = text1.getText().toString();

        MainActivityChampagne up = new MainActivityChampagne(MainActivityPaper.this);
        up.open();
        up.updateEntry(thetitles, thenotes);
        up.close();

        break;

    }

    Intent launchActvity = getIntent();

    Bundle b = launchActvity.getExtras();

    String keyname = launchActvity.getStringExtra("keyname");
    String key = launchActvity.getStringExtra("key");

    TextView title1 = (TextView)findViewById(R.id.editText3);
    TextView text1 = (TextView)findViewById(R.id.editText1);

    title1.setText("keyname");
    text1.setText("key");

}

}

3 番目のアクティビティ (SQL データベース アクティビティ)...

 package android.application.project.planes;

 import java.util.ArrayList;

 import android.app.Activity;
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;

public class MainActivityChampagne extends Activity {

public static final String KEY_NAME = "title";
public static final String KEY_NOTES = "lastcall";

private static final String DATABASE_NAME = "Champagnedb";
private static final String DATABASE_TABLE = "champagneTable";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub\
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
        KEY_NAME + " TEXT NOT NULL);"
        );

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
        onCreate(db);

    }

}

public MainActivityChampagne(Context c){
    ourContext = c;

}

public MainActivityChampagne open(){
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;

}

public void close(){
    ourHelper.close();

}

public long createEntry(String titles, String notes) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, titles);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);

}

public ArrayList<String> getData() {
    //TODO Auto-generated method stub
    String[] columns = new String[] { KEY_NAME };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iNotes = c.getColumnIndex(KEY_NAME);

    ArrayList<String> string_array = new ArrayList<String>();
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())

    {

        result = c.getString (iNotes) + "\n";
        string_array.add(result);

        }

    return string_array;

}

public void updateEntry(String thetitles, String thenotes) {
    // TODO Auto-generated method stub
    ContentValues cvUpdate = new ContentValues();
    cvUpdate.put(KEY_NAME, thetitles);
    cvUpdate.put(KEY_NOTES, thenotes);
    ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "=" + thetitles + KEY_NOTES + "=" + thenotes, null);

}

 }

LogCat

06-23 15:42:19.710: E/StrictMode(638):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
06-23 15:42:19.710: E/StrictMode(638):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
06-23 15:42:19.710: E/StrictMode(638):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
06-23 15:42:19.710: E/StrictMode(638):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
06-23 15:42:19.710: E/StrictMode(638):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
06-23 15:42:19.710: E/StrictMode(638):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
06-23 15:42:19.710: E/StrictMode(638):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
06-23 15:42:19.710: E/StrictMode(638):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
06-23 15:42:19.710: E/StrictMode(638):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
06-23 15:42:19.710: E/StrictMode(638):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
06-23 15:42:19.710: E/StrictMode(638):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
06-23 15:42:19.710: E/StrictMode(638):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
06-23 15:42:19.710: E/StrictMode(638):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-23 15:42:19.710: E/StrictMode(638):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-23 15:42:19.710: E/StrictMode(638):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-23 15:42:19.710: E/StrictMode(638):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-23 15:42:19.710: E/StrictMode(638):  at java.lang.Thread.run(Thread.java:856)
06-23 15:42:19.710: W/ActivityManager(284): Unbind failed: could not find connection for android.os.BinderProxy@40d605b0
06-23 15:42:19.729: E/ActivityThread(638): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d11750 that was originally bound here
06-23 15:42:19.729: E/ActivityThread(638): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d11750 that was originally bound here
06-23 15:42:19.729: E/ActivityThread(638):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
06-23 15:42:19.729: E/ActivityThread(638):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
06-23 15:42:19.729: E/ActivityThread(638):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
06-23 15:42:19.729: E/ActivityThread(638):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
06-23 15:42:19.729: E/ActivityThread(638):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
06-23 15:42:19.729: E/ActivityThread(638):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
06-23 15:42:19.729: E/ActivityThread(638):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
06-23 15:42:19.729: E/ActivityThread(638):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
06-23 15:42:19.729: E/ActivityThread(638):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
06-23 15:42:19.729: E/ActivityThread(638):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
06-23 15:42:19.729: E/ActivityThread(638):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
06-23 15:42:19.729: E/ActivityThread(638):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-23 15:42:19.729: E/ActivityThread(638):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-23 15:42:19.729: E/ActivityThread(638):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-23 15:42:19.729: E/ActivityThread(638):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-23 15:42:19.729: E/ActivityThread(638):  at java.lang.Thread.run(Thread.java:856)
06-23 15:42:19.858: E/StrictMode(638): null
06-23 15:42:19.858: E/StrictMode(638): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d11750 that was originally bound here
06-23 15:42:19.858: E/StrictMode(638):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
06-23 15:42:19.858: E/StrictMode(638):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
06-23 15:42:19.858: E/StrictMode(638):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
06-23 15:42:19.858: E/StrictMode(638):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
06-23 15:42:19.858: E/StrictMode(638):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
06-23 15:42:19.858: E/StrictMode(638):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
06-23 15:42:19.858: E/StrictMode(638):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
06-23 15:42:19.858: E/StrictMode(638):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
06-23 15:42:19.858: E/StrictMode(638):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
06-23 15:42:19.858: E/StrictMode(638):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
06-23 15:42:19.858: E/StrictMode(638):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
06-23 15:42:19.858: E/StrictMode(638):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-23 15:42:19.858: E/StrictMode(638):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-23 15:42:19.858: E/StrictMode(638):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-23 15:42:19.858: E/StrictMode(638):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-23 15:42:19.858: E/StrictMode(638):  at java.lang.Thread.run(Thread.java:856)
06-23 15:42:19.889: W/ActivityManager(284): Unbind failed: could not find connection for android.os.BinderProxy@40db5f30
06-23 15:42:19.889: D/dalvikvm(638): GC_CONCURRENT freed 466K, 20% free 2493K/3080K, paused 75ms+84ms, total 272ms
4

3 に答える 3

0

あなたの問題はここにあると思います:

b.putString("Extra__keyname", keyname);
b.putString("Extra__key", key);

そしてここ:

String keyname = launchActivity.getStringExtra("keyname");
String key = launchActivity.getStringExtra("key");

最初のアクティビティで使用されなかった文字列を使用して StringExtra を取得しようとしています ( "Extra__keyname" and "Extra__key"2 番目のアクティビティで使用した場合は、これらの特定の文字列を使用する必要があります。また、良い方法として、extra の「キー」文字列はパブリック定数にする必要があります。最初のアクティビティを作成し、次のように getStringExtra の 2 番目のアクティビティで同じ定数を使用します。

 public static final String EXTRA_KEY_NAME = "com.example.yourclass.EXTRA_KEY_NAME";

これを最初のアクティビティに追加してから、次のようにエクストラを配置します。

 b.putString(EXTRA_KEY_NAME, keyname);

次に、2 番目のアクティビティで、次のように Extra を取得します。

 launchActivity.getStringExtra(FirstActivity.EXTRA_KEY_NAME);

これはプログラミング用語のより良いアプローチであり、グーグルが推奨しています。私の英語で申し訳ありません、よろしく。

于 2013-06-23T16:01:52.703 に答える
0

最初のアクティビティ コードを

package android.application.project.planes;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
 import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

 public class MainActivityPlanes extends Activity {

Button Add;
ListView List;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main_activity_planes);

    Add = (Button) findViewById(R.id.button1);
    List = (ListView) findViewById(R.id.listView1);

    Add.setOnClickListener(new OnClickListener () {

        public void onClick (View v) {
            Intent intent = new Intent (v.getContext(), MainActivityPaper.class);
            startActivityForResult(intent, 0);
        }

    });

    List.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Intent launchActivity = new Intent(MainActivityPlanes.this, MainActivityPaper.class);

             Bundle b = new Bundle();
             String keyname =                   ((TextView)arg1.findViewById(android.R.id.text1)).getText().toString();
             //String key = text1.getText().toString(); 
             b.putString("Extra__keyname", keyname);
            //b.putString("Extra__key", key);
             launchActivity.putExtras(b);

             startActivity(launchActivity);

        }

    });

    MainActivityChampagne info = new MainActivityChampagne(this);       
    info.open();        
    ArrayList<String> data = info.getData();
    info.close();
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, data);
    List.setAdapter(arrayAdapter);

}

}

2 番目のアクティビティ クラスを

package android.application.project.planes;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class MainActivityPaper extends Activity implements OnClickListener {

TextView text1, title1;
Button save1, update1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main_activity_paper);

    text1 = (TextView) findViewById(R.id.editText1);
    title1 = (TextView) findViewById(R.id.editText3);
    save1 = (Button) findViewById(R.id.button3);
    update1 = (Button) findViewById(R.id.button5);


    Intent launchActvity = getIntent();

    Bundle b = launchActvity.getExtras();

    String keyname = b.getString("Extra__keyname");
   //this string same as b.putString("Extra__keyname", keyname);
   // String key = b.getString("Extra__key");

    TextView title1 = (TextView)findViewById(R.id.editText3);
    TextView text1 = (TextView)findViewById(R.id.editText1);

    title1.setText(keyname);
    //text1.setText(key);

    save1.setOnClickListener(this);
    update1.setOnClickListener(this);

}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

    switch (arg0.getId()) {
    case R.id.button3:

        String titles = title1.getText().toString();
        String notes = text1.getText().toString();

        MainActivityChampagne entry = new MainActivityChampagne(MainActivityPaper.this);
        entry.open();
        entry.createEntry(titles, notes);
        entry.close();

        break;

    case R.id.button5:

        String thetitles = title1.getText().toString();
        String thenotes = text1.getText().toString();

        MainActivityChampagne up = new MainActivityChampagne(MainActivityPaper.this);
        up.open();
        up.updateEntry(thetitles, thenotes);
        up.close();

        break;

    }
}

}
于 2013-06-23T17:31:12.053 に答える