所以基本上我有ListView和第二个带有两个EditText的活动来保存数据(有点像notes应用程序)。 ListView显示它们已创建但当我单击ListView中的项目时,它显示没有数据的空白第二个活动。我尝试了多种方法来使用bundle来解决这个问题,但到目前为止它仍然没有显示任何内容。
这是我目前第一次活动(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);
这是我的第二个活动(包含两个TextViews的活动)中我的第二段代码...
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");
}
}
我的第三个活动(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
答案 0 :(得分:0)
发现它应该是:
Bundle b = launchActivity.getExtras();
String keyname = b.getString("keyname");
String key = b.getString("key");
您可能需要删除Extra __
答案 1 :(得分:0)
我相信你的问题在这里:
b.putString("Extra__keyname", keyname);
b.putString("Extra__key", key);
在这里:
String keyname = launchActivity.getStringExtra("keyname");
String key = launchActivity.getStringExtra("key");
您正在尝试使用在第一个Activity中未使用的String检索StringExtra(您在第二个Activity中使用"Extra__keyname" and "Extra__key"
,您必须使用这些特定的字符串,另外作为一个好习惯,“key”字符串for extra应该是你的第一个Activity中的公共常量,然后在getStringExtra的第二个Activity中使用相同的常量,如下所示:
public static final String EXTRA_KEY_NAME = "com.example.yourclass.EXTRA_KEY_NAME";
这是你的第一个活动,然后你把额外的东西放在这样:
b.putString(EXTRA_KEY_NAME, keyname);
然后在你的第二个活动中,你得到你的额外照片:
launchActivity.getStringExtra(FirstActivity.EXTRA_KEY_NAME);
这是编程术语中更好的方法,谷歌推荐,对不起我的英语,问候。
答案 2 :(得分: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);
}
}
将您的第二个活动类更改为
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;
}
}
}