gpt4 book ai didi

android - 如何将数据库记录传递给第二个 Activity 并使用 ListView 显示在其中?

转载 作者:行者123 更新时间:2023-11-30 01:55:47 26 4
gpt4 key购买 nike

我是 android 和 java 的新手。让我解释一下我的项目。我正在编写一个 Android 应用程序。我想创建一个数据库并从 TextView 中插入、删除、修改数据。并使用 ListView 另一个 Activity 显示这些数据。我的第一个问题是,如何将这些数据发送到另一个 Activity ?其次,我找到了一些代码:

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 并在其中显示?

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>

感谢任何帮助。

最佳答案

哦,这里有很多不同的问题。让我们从您的第一个问题开始:如果您希望将数据从一个 Activity 发送到另一个 Activity ,您应该考虑创建一个可以实例化的类。实例(Java 对象)保存所有数据并且可以从不同的 Activity 中访问。

所以如果你正在处理评论,你应该像这样创建一个评论类:

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();
}

}

然后在您的第一个 Activity 中,您像这样创建 Comment 类的实例

Comment newComment = new Comment();

然后用你喜欢的任何内容填充它:

newComment.setCommentTitle("My first comment");
newComment.setCommentText("Here goes my comment text...");

现在您可以像这样将此 Comment 对象传递给您的下一个 Activity :

Intent i = new Intent(getActivity(), SecondActivity.class);
i.putExtra("newCommentKey", newComment);
startActivity(i);

在您调用的 onCreate() 方法中的第二个 Activity 中:

Intent i = getIntent();
Comment myComment = (Comment ) i.getParcelableExtra("newCommentKey");

然后他们可以通过getter获取所有的内容,例如:

String titleOfComment = myComment.getCommentTitle();

祝你好运!

关于android - 如何将数据库记录传递给第二个 Activity 并使用 ListView 显示在其中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32258422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com