我一直在从不同的教程、问题响应等中尝试许多不同的方法,但仍然无法让我的应用程序不仅从多个编辑文本中检索文本,还保存到一个对象中,然后移动到传递文本的新活动显示为确认。我将发布我写的两次尝试。
第一次尝试包 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;
}
}
});
}
}
它再次简单地停止了我的模拟器。
有人可以帮忙吗?