我一直在尝试从不同的教程,问题响应等不同的方式,仍然无法让我的应用程序不仅从多个edittext检索文本,而是保存到一个对象,然后移动到新的活动传递要显示为确认的文本。我将发布我写过的两次尝试。
第一次尝试 包com.zombiecatandroidapp;import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RatingBar;
public class ChefCookRateActivity extends Activity implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public final static String FIRST_NAME_MESSAGE = "com.zombiecatandroidapp.MESSAGE";
public final static String LAST_NAME_MESSAGE = "com.zombiecatandroidapp.MESSAGE";
public final static String COMPANY_NAME_CHEF_COOK_MESSAGE = "com.zombiecatandroidapp.MESSAGE";
public final static String COMPANY_ADDRESS_CHEF_COOK_MESSAGE = "com.zombiecatandroidapp.MESSAGE";
public final static String COMPANY_CITY_CHEF_COOK_MESSAGE = "com.zombiecatandroidapp.MESSAGE";
public final static String COMPANY_STATE_CHEF_COOK_MESSAGE = "com.zombiecatandroidapp.MESSAGE";
public final static String COMPANY_ZIP_CHEF_COOK_MESSAGE = "com.zombiecatandroidapp.MESSAGE";
public final static String FOOD_RATE_CHEf_COOK_BAR_MESSAGE = "com.zombiecatandroidapp.MESSAGE";
public final static String FILE_NAME = "C:\\Users\\Cassey\\AppData\\Local\\Development\\ProjectFile\\ChefCookRating.ser";
ChefCook person = new ChefCook();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chef_cook_rate);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.chef_cook_rate, menu);
return true;
}
public void startSubmitDisplay(View view) {
Intent intent = new Intent(this, DisplayInfoActivity.class);
final EditText editText = (EditText) findViewById(R.id.first_name);
String firstName = editText.getText().toString();
final EditText editText1 = (EditText) findViewById(R.id.last_name);
String lastName = editText1.getText().toString();
final EditText editText2 = (EditText) findViewById(R.id.company_name_chef_cook);
String companyNameChefCook = editText2.getText().toString();
final EditText editText3 = (EditText) findViewById(R.id.company_address_chef_cook);
String companyAddressChefCook = editText3.getText().toString();
final EditText editText4 = (EditText) findViewById(R.id.company_city_chef_cook);
String companyCityChefCook = editText4.getText().toString();
final EditText editText5 = (EditText) findViewById(R.id.company_state_chef_cook);
String companyStateChefCook = editText5.getText().toString();
final EditText editText6 = (EditText) findViewById(R.id.company_zip_chef_cook);
String companyZipChefCook = editText6.getText().toString();
int companyZipChefCookConvert = Integer.parseInt(companyZipChefCook);
RatingBar ratingbar = (RatingBar) findViewById(R.id.food_rate_chef_cook_bar);
String foodRateChefCookBar = String.valueOf(ratingbar.getRating());
int foodRateChefCookBarConvert = Integer.parseInt(foodRateChefCookBar);
person.setFirstName(firstName);
person.setLastName(lastName);
person.setCompanyName(companyNameChefCook);
person.setCompanyAddress(companyAddressChefCook);
person.setCompanyCity(companyCityChefCook);
person.setCompanyState(companyStateChefCook);
person.setCompanyZip(companyZipChefCookConvert);
person.setRating(firstName, lastName, foodRateChefCookBarConvert, 1);
saveChefCook(person);
startActivity(intent);
}
public void saveChefCook(ChefCook person) {
try{
// Serialize data object to a file
ObjectOutputStream fileOut = new ObjectOutputStream(new FileOutputStream("FILE_NAME"));
fileOut.writeObject(person);
fileOut.close();
} catch (IOException e) {
}
}
}
下一个活动看起来像这样。 包com.zombiecatandroidapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.content.Intent;
public class DisplayInfoActivity extends Activity {
TextView textView3 = null;
TextView textView4 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_display_info);
Intent intent = getIntent();
String message = intent.getStringExtra(ChefCookRateActivity.FIRST_NAME_MESSAGE);
message += intent.getStringExtra(ChefCookRateActivity.LAST_NAME_MESSAGE);
String message2 = intent.getStringExtra(ChefCookRateActivity.COMPANY_NAME_CHEF_COOK_MESSAGE);
String message3 = intent.getStringExtra(ChefCookRateActivity.COMPANY_ADDRESS_CHEF_COOK_MESSAGE);
String message4 = intent.getStringExtra(ChefCookRateActivity.COMPANY_CITY_CHEF_COOK_MESSAGE);
message4 += intent.getStringExtra(ChefCookRateActivity.COMPANY_STATE_CHEF_COOK_MESSAGE);
message4 += intent.getStringExtra(ChefCookRateActivity.COMPANY_ZIP_CHEF_COOK_MESSAGE);
String message5 = intent.getStringExtra(ChefCookRateActivity.FOOD_RATE_CHEf_COOK_BAR_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setText(message);
TextView textView2 = new TextView(this);
textView.setText(message2);
TextView textView3 = new TextView(this);
textView.setText(message3);
TextView textView4 = new TextView(this);
textView.setText(message4);
TextView textView5 = new TextView(this);
textView.setText(message5);
// Set the text view as the activity layout
setContentView(textView);
if(textView2 != null) {
setContentView(textView2);
}
if(textView3 != null) {
setContentView(textView3);
}
if(textView4 != null) {
setContentView(textView4);
}
setContentView(textView5);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_info, menu);
return true;
}
}
然后第二次尝试就是这样。 包com.zombiecatandroidapp;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.RatingBar;
public class ChefCookRateActivity extends Activity implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public final static String FILE_NAME = "C:\\Users\\Cassey\\AppData\\Local\\Development\\ProjectFile\\ChefCookRating.ser";
// get Edit Text component
final EditText editText = (EditText) findViewById(R.id.first_name);
final EditText editText1 = (EditText) findViewById(R.id.last_name);
final EditText editText2 = (EditText) findViewById(R.id.company_name_chef_cook);
final EditText editText3 = (EditText) findViewById(R.id.company_address_chef_cook);
final EditText editText4 = (EditText) findViewById(R.id.company_city_chef_cook);
final EditText editText5 = (EditText) findViewById(R.id.company_state_chef_cook);
final EditText editText6 = (EditText) findViewById(R.id.company_zip_chef_cook);
final RatingBar ratingBar = (RatingBar) findViewById(R.id.food_rate_chef_cook_bar);
public String firstName;
public String lastName;
public String companyNameChefCook;
public String companyAddressChefCook;
public String companyCityChefCook;
public String companyStateChefCook;
public int companyZipChefCookConvert;
private String foodRateChefCookBar;
int foodRateChefCookBarConvert = Integer.parseInt(foodRateChefCookBar);
ChefCook person = new ChefCook();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chef_cook_rate);
addKeyListener();
person.setFirstName(firstName);
person.setLastName(lastName);
person.setCompanyName(companyNameChefCook);
person.setCompanyAddress(companyAddressChefCook);
person.setCompanyCity(companyCityChefCook);
person.setCompanyState(companyStateChefCook);
person.setCompanyZip(companyZipChefCookConvert);
person.setRating(firstName, lastName, foodRateChefCookBarConvert, 1);
saveChefCook(person);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.chef_cook_rate, menu);
return true;
}
public void addKeyListener() {
// add a keylistener to keep track of user input
editText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
editText.getText().toString();
firstName = editText.getText().toString();
return true;
} else {
return false;
}
}
});
editText1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
editText1.getText().toString();
lastName = editText.getText().toString();
return true;
} else {
return false;
}
}
});
editText2.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
editText2.getText().toString();
companyNameChefCook = editText.getText().toString();
return true;
} else {
return false;
}
}
});
editText3.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
editText3.getText().toString();
companyAddressChefCook = editText.getText().toString();
return true;
} else {
return false;
}
}
});
editText4.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
editText4.getText().toString();
companyCityChefCook = editText.getText().toString();
return true;
} else {
return false;
}
}
});
editText5.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
editText5.getText().toString();
companyStateChefCook = editText.getText().toString();
return true;
} else {
return false;
}
}
});
editText6.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
editText6.getText().toString();
String companyZipChefCook = editText6.getText().toString();
companyZipChefCookConvert = Integer.parseInt(companyZipChefCook);
return true;
} else {
return false;
}
}
});
ratingBar.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
foodRateChefCookBar = String.valueOf(ratingBar.getRating());
return true;
} else {
return false;
}
}
});
}
public void startSubmitDisplay(View view) {
Intent intent = new Intent(this, DisplayInfoActivity.class);
startActivity(intent);
}
public void saveChefCook(ChefCook person) {
try{
// Serialize data object to a file
ObjectOutputStream fileOut = new ObjectOutputStream(new FileOutputStream("FILE_NAME"));
fileOut.writeObject(person);
fileOut.close();
} catch (IOException e) {
}
}
}
我尚未编写此活动的显示活动,因为当我尝试至少延长此活动时,我的模拟器会因活动线程错误而崩溃。有关未找到来源的事情。
3rd attempt looks like this
package com.zombiecatandroidapp;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.RatingBar;
public class ChefCookRateActivity extends Activity implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public final static String FILE_NAME = "C:\\Users\\Cassey\\AppData\\Local\\Development\\ProjectFile\\ChefCookRating.ser";
// get Edit Text component
final EditText editText = (EditText) findViewById(R.id.first_name);
final EditText editText1 = (EditText) findViewById(R.id.last_name);
final EditText editText2 = (EditText) findViewById(R.id.company_name_chef_cook);
final EditText editText3 = (EditText) findViewById(R.id.company_address_chef_cook);
final EditText editText4 = (EditText) findViewById(R.id.company_city_chef_cook);
final EditText editText5 = (EditText) findViewById(R.id.company_state_chef_cook);
final EditText editText6 = (EditText) findViewById(R.id.company_zip_chef_cook);
final RatingBar ratingBar = (RatingBar) findViewById(R.id.food_rate_chef_cook_bar);
public String firstName;
public String lastName;
public String companyNameChefCook;
public String companyAddressChefCook;
public String companyCityChefCook;
public String companyStateChefCook;
public int companyZipChefCookConvert;
private String foodRateChefCookBar;
int foodRateChefCookBarConvert = Integer.parseInt(foodRateChefCookBar);
ChefCook person = new ChefCook();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chef_cook_rate);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.chef_cook_rate, menu);
return true;
}
public void addKeyListener() {
// add a keylistener to keep track of user input
editText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
editText.getText().toString();
firstName = editText.getText().toString();
return true;
} else {
return false;
}
}
});
editText1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
editText1.getText().toString();
lastName = editText.getText().toString();
return true;
} else {
return false;
}
}
});
editText2.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
editText2.getText().toString();
companyNameChefCook = editText.getText().toString();
return true;
} else {
return false;
}
}
});
editText3.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
editText3.getText().toString();
companyAddressChefCook = editText.getText().toString();
return true;
} else {
return false;
}
}
});
editText4.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
editText4.getText().toString();
companyCityChefCook = editText.getText().toString();
return true;
} else {
return false;
}
}
});
editText5.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
editText5.getText().toString();
companyStateChefCook = editText.getText().toString();
return true;
} else {
return false;
}
}
});
editText6.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
editText6.getText().toString();
String companyZipChefCook = editText6.getText().toString();
companyZipChefCookConvert = Integer.parseInt(companyZipChefCook);
return true;
} else {
return false;
}
}
});
ratingBar.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
foodRateChefCookBar = String.valueOf(ratingBar.getRating());
return true;
} else {
return false;
}
}
});
}
public void startSubmitDisplay(View view) {
Intent intent = new Intent(this, DisplayInfoActivity.class);
addKeyListener();
person.setFirstName(firstName);
person.setLastName(lastName);
person.setCompanyName(companyNameChefCook);
person.setCompanyAddress(companyAddressChefCook);
person.setCompanyCity(companyCityChefCook);
person.setCompanyState(companyStateChefCook);
person.setCompanyZip(companyZipChefCookConvert);
person.setRating(firstName, lastName, foodRateChefCookBarConvert, 1);
saveChefCook(person);
startActivity(intent);
}
public void saveChefCook(ChefCook person) {
try{
// Serialize data object to a file
ObjectOutputStream fileOut = new ObjectOutputStream(new FileOutputStream("FILE_NAME"));
fileOut.writeObject(person);
fileOut.close();
} catch (IOException e) {
}
}
}
同样,我还没有写过这个的显示活动,因为当我尝试至少延长这个活动时,我的模拟器会因为活动线程错误而崩溃。有关未找到来源的事情。
我尝试了第四个版本,看起来像这样。
package com.zombiecatandroidapp;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.RatingBar;
public class ChefCookRateActivity extends Activity implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public final static String FILE_NAME = "C:\\Users\\Cassey\\AppData\\Local\\Development\\ProjectFile\\ChefCookRating.ser";
// get Edit Text component
final EditText editText = (EditText) findViewById(R.id.first_name);
final EditText editText1 = (EditText) findViewById(R.id.last_name);
final EditText editText2 = (EditText) findViewById(R.id.company_name_chef_cook);
final EditText editText3 = (EditText) findViewById(R.id.company_address_chef_cook);
final EditText editText4 = (EditText) findViewById(R.id.company_city_chef_cook);
final EditText editText5 = (EditText) findViewById(R.id.company_state_chef_cook);
final EditText editText6 = (EditText) findViewById(R.id.company_zip_chef_cook);
final RatingBar ratingBar = (RatingBar) findViewById(R.id.food_rate_chef_cook_bar);
public String firstName;
public String lastName;
public String companyNameChefCook;
public String companyAddressChefCook;
public String companyCityChefCook;
public String companyStateChefCook;
public int companyZipChefCookConvert;
private String foodRateChefCookBar;
int foodRateChefCookBarConvert = Integer.parseInt(foodRateChefCookBar);
ChefCook person = new ChefCook();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chef_cook_rate);
addOnFocusChangeListener();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.chef_cook_rate, menu);
return true;
}
public void startSubmitDisplay(View view) {
Intent intent = new Intent(this, DisplayInfoActivity.class);
person.setFirstName(firstName);
person.setLastName(lastName);
person.setCompanyName(companyNameChefCook);
person.setCompanyAddress(companyAddressChefCook);
person.setCompanyCity(companyCityChefCook);
person.setCompanyState(companyStateChefCook);
person.setCompanyZip(companyZipChefCookConvert);
person.setRating(firstName, lastName, foodRateChefCookBarConvert, 1);
saveChefCook(person);
startActivity(intent);
}
public void saveChefCook(ChefCook person) {
try{
// Serialize data object to a file
ObjectOutputStream fileOut = new ObjectOutputStream(new FileOutputStream("FILE_NAME"));
fileOut.writeObject(person);
fileOut.close();
} catch (IOException e) {
}
}
public void addOnFocusChangeListener() {
// add a focuslistener to keep track of user input
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
editText.getText().toString();
firstName = editText.getText().toString();
}
}
});
editText1.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
editText1.getText().toString();
lastName = editText.getText().toString();
}
}
});
editText2.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
editText2.getText().toString();
companyNameChefCook = editText.getText().toString();
}
}
});
editText3.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
editText3.getText().toString();
companyAddressChefCook = editText.getText().toString();
}
}
});
editText4.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
editText4.getText().toString();
companyCityChefCook = editText.getText().toString();
}
}
});
editText5.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
editText5.getText().toString();
companyStateChefCook = editText.getText().toString();
}
}
});
editText6.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
editText6.getText().toString();
String companyZipChefCook = editText.getText().toString();
companyZipChefCookConvert = Integer.parseInt(companyZipChefCook);
}
}
});
ratingBar.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and enter is pressed
if(event.getAction() == KeyEvent.ACTION_DOWN) {
foodRateChefCookBar = String.valueOf(ratingBar.getRating());
return true;
} else {
return false;
}
}
});
}
}
再一次,它只是停止我的模拟器。
有人可以帮忙吗?
答案 0 :(得分:0)
我不完全明白确切的问题是什么,但无论你做什么,都不会尝试序列化整个活动。
我会改为使Person可序列化,并将其提供给第二个活动的意图。 E.g:
intent.putSerializable(EXTRA_PERSON, person);
startActivity(intent);