我是android和java的新手。让我解释一下我的项目。我正在编写一个 Android 应用程序。我想创建一个数据库并从文本视图中插入、删除、修改数据。ListView
并使用另一个活动显示这些数据。我的第一个问题是,如何将这些数据发送到另一个活动?其次,我找到了一些代码:
List<Comment> values = datasource.getAllComments();
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
在这些代码之后,写入插入记录代码。对我来说这是错误的,因为没有记录以及如何使用getAllComments
方法获取数据,请解释一下。第三,如何以更好的方式将数据插入数据库 - 如果有的话 - 我想说的是,我正在调用我的插入方法MainActivity
:
Contact contact = dbOperations.addContact(editName.getText().toString(), editSurname.getText().toString(), editPhone.getText().toString(), editEmail.getText().toString());
但我不想得到这样的参数。如果我接受参数联系并编写我的代码,例如:
ContentValues values = new ContentValues();
values.put(DBHelper.colName, contact.getName());
values.put(DBHelper.colSurname, contact.getSurname());
values.put(DBHelper.colPhone, contact.getPhone());
values.put(DBHelper.colEmail, contact.getEmail());
我应该如何在文本视图中获取参数(调用插入方法)MainActivity
?我读了一篇关于它的文章,我发现了这段代码:
db.addContact(new Contact("Srinivas", "9199999999"));
addContact
String
方法带有联系参数,那么如果这段代码为真,他如何写值。
第四个也是最后一个问题是,
我的 logcat 错误:
08-26 19:21:06.639 9661-9661/? I/art﹕ Not late-enabling -Xcheck:jni (already on) 08-26 19:21:06.663 9661-9668/? E/art﹕ Failed writing handshake bytes (-1 of 14): Broken pipe 08-26 19:21:06.663 9661-9668/? I/art﹕ Debugger is no longer active 08-26 19:21:06.705 9661-9661/? D/AndroidRuntime﹕ Shutting down VM 08-26 19:21:06.705 9661-9661/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: busra.contactdb, PID: 9661
java.lang.RuntimeException: Unable to start activity ComponentInfo{busra.contactdb/busra.contactdb.ContactActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ListActivity.onContentChanged(ListActivity.java:243)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:382)
at android.app.Activity.setContentView(Activity.java:2145)
at busra.contactdb.ContactActivity.onCreate(ContactActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 08-26 19:26:06.781 9661-9661/? I/Process﹕ Sending signal. PID: 9661 SIG: 9
我不知道问题是什么。这一行:
at busra.contactdb.ContactActivity.onCreate(ContactActivity.java:43)
点此代码:
setContentView(R.layout.activity_contact);
我在 setAdapter 方法上遇到空指针异常。这是我的 ContactActivity:
public class ContactActivity extends Activity{
public static final String SEND_NAME= "";
public static final String SEND_SURNAME= "";
public static final String SEND_PHONE= "";
public static final String SEND_EMAIL= "";
DatabaseOperations dbOperations;
List values = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
findViewAllIDs();
dbOperations = new DatabaseOperations(this);
dbOperations.openDB();
values = dbOperations.getAllContacts();
ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,values);
list.setAdapter(adapter);
View v = findViewById(R.id.listButton);
v.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (arg0.getId() == R.id.listButton) {
//define new intent to DisplayActivity
Intent intent = new Intent(ContactActivity.this, DisplayActivity.class);
String sendName = values.get(0).toString();
intent.putExtra(SEND_NAME, sendName);
String sendSurname = values.get(1).toString();
intent.putExtra(SEND_SURNAME, sendSurname);
String sendPhone = values.get(2).toString();
intent.putExtra(SEND_PHONE, sendPhone);
String sendEmail = values.get(3).toString();
intent.putExtra(SEND_EMAIL, sendEmail);
//start the second activity
startActivity(intent);
}
}
});
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
add();
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
delete();
}
});
btnModify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// modify();
}
});
}
public void add(){
ArrayAdapter adapter1 = (ArrayAdapter) list.getAdapter();
// dbOperations.addContact(new Contact());
Contact contact = dbOperations.addContact(editName.getText().toString(),
editSurname.getText().toString(),editPhone.getText().toString(),
editEmail.getText().toString());
adapter1.add(contact);
adapter1.notifyDataSetChanged();
}
public void delete(){
Contact contact = null;
ArrayAdapter adapter1 = (ArrayAdapter) list.getAdapter();
if(list.getAdapter().getCount() > 0){
contact = (Contact) list.getAdapter().getItem(0);
dbOperations.deleteContact(contact);
adapter1.remove(contact);
adapter1.notifyDataSetChanged();
}
}
public void modify () {
Contact contact = null;
// ArrayAdapter adapter1 = (ArrayAdapter) getListAdapter();
if (getListAdapter().getCount() > 0) {
contact = (Contact) getListAdapter().getItem(0);
dbOperations.modifyContact(contact);
adapter1.notifyDataSetChanged();
}
我在 ContactActivity 的第二个布局 xml 中调用 ListView。list = (ListView) findViewById(android.R.id.list);
这是真的 ?
编辑:
我解决了我的问题,但不幸的是我找不到任何解决方案。如何将数据库记录传递给第二个活动并在其中显示?
activity_contact.xml
<?xml version= "1.0" encoding = "utf-8"?>
<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="wrap_content"
android:orientation="vertical"
tools:context=". MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15sp"
android:text="@string/name"/>
<EditText
android:id="@+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_gravity="end"
android:singleLine="true">
<requestFocus/>
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Surname"
android:textSize="17sp"/>
<EditText
android:id="@+id/editSurname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_gravity="end"
android:singleLine="true">
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Phone"
android:textSize="17sp"/>
<EditText
android:id="@+id/editPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:layout_gravity="bottom"
android:singleLine="true">
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/email"
android:textSize="17sp"/>
<EditText
android:id="@+id/editEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:layout_gravity="bottom"
android:singleLine="true">
</EditText>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add"
android:textColor="#000066"
android:textStyle="bold"
android:textSize="15sp"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/delete"
android:textColor="#000066"
android:textStyle="bold"
android:textSize="15sp"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/modify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/modify"
android:textColor="#000066"
android:textStyle="bold"
android:textSize="15sp"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/listButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/list"
android:textColor="#000066"
android:textStyle="bold"
android:textSize="15sp"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="@+id/count_records"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="13sp"/>
</LinearLayout>
list_contact_info.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="467dp"> </ListView>
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnBack"
android:textColor="#000066"
android:textStyle="bold"
android:textSize="15sp"
android:layout_gravity="right|bottom"
>
</Button>
</FrameLayout>
任何帮助表示赞赏。