您好我是Android开发的新手,我正在尝试创建一个应用程序,它将用户的姓名,邮政编码,年龄和性别添加到SQLite数据库,然后在下一页显示。我已经为此尝试了很多教程,但收效甚微。在EditText中输入数据后,按下提交按钮会导致android崩溃任何帮助将不胜感激。 这是我用户输入数据的主要活动
package com.example.votesmart;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class Zip extends Activity {
private static final int TIME_ENTRY_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.zip);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.zip, menu);
return true;
}
public void onSubmit(View view){
Intent intent = new Intent(this, EditProfile.class);
startActivityForResult(intent, TIME_ENTRY_REQUEST_CODE);
EditText firstView = (EditText)findViewById(R.id.fname_input);
intent.putExtra("first", firstView.getText().toString());
EditText lastView = (EditText)findViewById(R.id.lname_input);
intent.putExtra("last", lastView.getText().toString());
EditText zipView = (EditText)findViewById(R.id.zipcode_input);
intent.putExtra("zip", zipView.getText().toString());
EditText ageView = (EditText)findViewById(R.id.age_input);
intent.putExtra("age", ageView.getText().toString());
EditText sexView = (EditText)findViewById(R.id.sex_input);
intent.putExtra("sex", sexView.getText().toString());
}
}
其布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Zip"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="First Name" />
<EditText
android:id="@+id/fname_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Last Name" />
<EditText
android:id="@+id/lname_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Zip Code" />
<EditText
android:id="@+id/zipcode_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Age" />
<EditText
android:id="@+id/age_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sex" />
<EditText
android:id="@+id/sex_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:onClick="onSubmit" />
</LinearLayout>
显示数据的下一页
package com.example.votesmart;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
public class EditProfile extends Activity {
VSAdapter vsAdapter;
public static final int TIME_ENTRY_REQUEST_CODE = 1;
private DatabaseHandler databaseHelper;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_profile);
databaseHelper = new DatabaseHandler(this);
ListView listView = (ListView)findViewById(R.id.list);
vsAdapter = new VSAdapter(this, databaseHelper.getAllRecords());
listView.setAdapter(vsAdapter);
}
public void onBack (View view){
finish();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == TIME_ENTRY_REQUEST_CODE){
if (resultCode == RESULT_OK){
String first = data.getStringExtra("first");
String last = data.getStringExtra("last");
String zip = data.getStringExtra("zip");
String age = data.getStringExtra("age");
String sex = data.getStringExtra("sex");
databaseHelper.addVoter(first, last, zip, age, sex);
//vsAdapter.changeCursor(databaseHelper.getAllRecords());
//databaseHelper.addVoter(first, last, zip, age, sex);
}
}
}
}
我的适配器
package com.example.votesmart;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
public class VSAdapter extends CursorAdapter {
public VSAdapter(Context context, Cursor cursor){
super(context, cursor);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView nameTextView = (TextView) view.findViewById(R.id.first_view);
nameTextView.setText(cursor.getString(cursor.getColumnIndex("1")));
TextView lastTextView = (TextView) view.findViewById(R.id.last_view);
lastTextView.setText(cursor.getString(cursor.getColumnIndex("2")));
TextView zipTextView = (TextView) view.findViewById(R.id.zip_view);
zipTextView.setText(cursor.getString(cursor.getColumnIndex("3")));
TextView ageTextView = (TextView) view.findViewById(R.id.age_view);
ageTextView.setText(cursor.getString(cursor.getColumnIndex("4")));
TextView sexTextView = (TextView) view.findViewById(R.id.sex_view);
sexTextView.setText(cursor.getString(cursor.getColumnIndex("5")));
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.zip_list_item, parent, false);
return view;
}
}
and finally my database class
private class DatabaseOpenHelper extends SQLiteOpenHelper{
DatabaseOpenHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_VOTERS_TABLE = "create table " + TABLE_NAME + "(" + KEY_ID + " integer primary key, " + KEY_FIRST + " text," + KEY_LAST + " text," + KEY_ZIP + " text," + KEY_AGE + " text," + KEY_SEX + "text" ;
db.execSQL(
CREATE_VOTERS_TABLE
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
public void addVoter(String first, String last, String zip, String age, String sex){
//public void addVoter{Voter voter){
ContentValues values = new ContentValues();
values.put(KEY_FIRST, first);
values.put(KEY_LAST, last);
values.put(KEY_ZIP, zip);
values.put(KEY_AGE, age);
values.put(KEY_SEX, sex);
db.insert(TABLE_NAME, null, values);
//return db.update(TABLE_NAME, values, KEY_ID + " = ?", new String[] {String.valueOf(voter.getId()) });
}
public Cursor getAllRecords(){
return db.rawQuery(
"select * from " + TABLE_NAME,
null
);
}
}
再次感谢任何帮助,谢谢!
答案 0 :(得分:0)
这有一些问题,
public void onSubmit(View view){
Intent intent = new Intent(this, EditProfile.class);
EditText firstView = (EditText)findViewById(R.id.fname_input);
intent.putExtra("first", firstView.getText().toString());
EditText lastView = (EditText)findViewById(R.id.lname_input);
intent.putExtra("last", lastView.getText().toString());
EditText zipView = (EditText)findViewById(R.id.zipcode_input);
intent.putExtra("zip", zipView.getText().toString());
EditText ageView = (EditText)findViewById(R.id.age_input);
intent.putExtra("age", ageView.getText().toString());
EditText sexView = (EditText)findViewById(R.id.sex_input);
intent.putExtra("sex", sexView.getText().toString());
startActivityForResult(intent, TIME_ENTRY_REQUEST_CODE);
}
您需要在发送数据之前添加数据。您使用onActivityResult的方法也是错误的,
在EditProfile类的oncreate方法中,输入以下内容
Intent data = getIntent().getExtras();
String first = data.getStringExtra("first");
String last = data.getStringExtra("last");
String zip = data.getStringExtra("zip");
String age = data.getStringExtra("age");
String sex = data.getStringExtra("sex");
databaseHelper.addVoter(first,last,zip,age,sex);
理想情况下,这应该是一个线程但是是的。
答案 1 :(得分:0)
在这段代码中:
public void onSubmit(View view){
Intent intent = new Intent(this, EditProfile.class);
startActivityForResult(intent, TIME_ENTRY_REQUEST_CODE);
您正在第3行开始您的活动,但之后您正在向意图“添加数据”......意图被“startActivity”消耗。把它放在那个街区的底部。
并且您没有在其他Activity的“onResult”中获取意图数据 - 您可以在销毁它之前设置活动的结果。收集的意图数据需要在显示活动之前 - onCreate,onStart或onResume。 OnCreate会做一次。其他用于您可能想要回收活动的情况(例如在已经开始之后添加另一个选民)。但你需要检查欺骗行为。