我是android和java的新手。让我解释一下我的项目。我正在编写一个Android应用程序。我想创建一个数据库,并从textviews插入,删除,修改数据。并使用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
中使用textview获取参数(调用插入方法)?我读了一篇关于它的文章,我发现了这段代码:
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>
感谢任何帮助。
答案 0 :(得分:1)
哦,这里有很多不同的问题。让我们从您的第一个问题开始:如果您希望将数据从一个活动发送到另一个活动,您应该考虑创建一个可以实例化的类。实例(Java对象)包含所有数据,可以从不同的活动中访问。
因此,如果您正在处理评论,您应该创建一个这样的评论类:
public class Comment implements Parcelable {
private int commentId;
private String commentTitle;
private String commentText;
// constructor
public Comment() {
}
// setters
public void setCommentId(int id) {
this.commentId = id;
}
public void setCommentTitle(String title) {
this.commentTitle = title;
}
public void setCommentText(String text) {
this.commentText = text;
}
// getters
public int getCommentId() {
return commentId;
}
public String getCommentTitle() {
return commentId;
}
public String getCommentText() {
return commentId;
}
// 99.9% of the time you can just ignore this
public int describeContents() {
return 0;
}
// write your object's data to the passed-in Parcel
public void writeToParcel(Parcel out, int flags) {
out.writeInt(commentId);
out.writeString(commentTitle);
out.writeString(commentText);
}
// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
public Comment createFromParcel(Parcel in) {
return new Comment(in);
}
public Comment[] newArray(int size) {
return new Comment[size];
}
};
// example constructor that takes a Parcel and gives you an object populated with it's values
private Comment(Parcel in) {
commentId = in.readInt();
commentTitle = in.readString();
commentText = in.readString();
}
}
然后在第一个活动中,您可以像这样创建一个Comment类的实例
Comment newComment = new Comment();
并填写您喜欢的任何内容:
newComment.setCommentTitle("My first comment");
newComment.setCommentText("Here goes my comment text...");
现在,您可以将此评论对象传递到下一个活动,如下所示:
Intent i = new Intent(getActivity(), SecondActivity.class);
i.putExtra("newCommentKey", newComment);
startActivity(i);
在您调用的onCreate()
方法中的第二个活动中:
Intent i = getIntent();
Comment myComment = (Comment ) i.getParcelableExtra("newCommentKey");
它可以通过getter获取所有内容,例如:
String titleOfComment = myComment.getCommentTitle();
祝你好运!