gpt4 book ai didi

java - 访问 ArrayList 中的对象

转载 作者:太空宇宙 更新时间:2023-11-04 10:59:43 25 4
gpt4 key购买 nike

我有一个 SQLite 数据库,其中有一个名为 author 的表。我正在尝试提取 idname 值并将它们传递给 MainActivity,其中 id 将存储为变量,name 将显示在 ListView 中。

name 方面工作正常,但我不确定如何在 MainActivity 中获取 id 值。我在 authorList 上尝试了 ArrayList 的 get 方法,但没有成功。

DatabaseHelper 的 fragment :

public List<Author> getAllAuthors() {
List<Author> authorList = new ArrayList<>();
// Select all query
String selectQuery = "SELECT * FROM " + AUTHORS + " ORDER BY name_alphabetic";

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
// create new author object
Author author = new Author();
// set ID and name of author object
author.setID(Integer.parseInt(cursor.getString(0)));
author.setName(cursor.getString(1));
// pass author object to authorList array
authorList.add(author);
} while (cursor.moveToNext());
}
// return author list
return authorList;
}

作者类别:

public class Author {
int id;
String name;
String name_alphabetic;

@Override
public String toString() {
return name;
}

public Author() {
}

public Author(int id, String name, String name_alphabetic) {
this.id = id;
this.name = name;
this.name_alphabetic = name_alphabetic;
}

// getters
public int getID() {
return this.id;
}

public String getName() {
return this.name;
}

public String getNameAlphabetic() {
return this.name_alphabetic;
}

// setters
public void setID(int id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

public void setNameAlphabetic(String name_alphabetic) {
this.name_alphabetic = name_alphabetic;
}
}

MainActivity 的 fragment :

// connect authorsListView variable to XML ListView
authorsListView = (ListView) findViewById(R.id.authors_list_view);

// create new database helper
DatabaseHelper db = new DatabaseHelper(this);

// create new list through getAllAuthors method (in DatabaseHelper class)
List authorList = db.getAllAuthors();

Log.i("authors", authorList.toString());

// create new Array Adapter
ArrayAdapter<Author> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, authorList);

// link ListView and Array Adapter
authorsListView.setAdapter(arrayAdapter);

// onItemClickListener waits for user to tap a ListView item
authorsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

String author = authorsListView.getItemAtPosition(i).toString();

//Log.i("click", author);

// new Intent specifies which Activity to move from and to
Intent intent = new Intent(MainActivity.this, StoriesActivity.class);
// adds extra author variable to the Intent
intent.putExtra("author", author);
startActivity(intent);
}
});

编辑:

我尝试过 return authorList.get(0)returnauthorList.get(id) (尽管我知道 id 尚未设置)。传递此变量的目的是稍后在 MainActivity 中,我可以使用 Intent extra 将其传递给另一个 Activity,即 intent.putExtra("author",author_id)

最佳答案

在你的代码中,我看到了这个:

List authorList = db.getAllAuthors();

您可能应该将其定义为 List<Author>而不仅仅是List ,这样 get()方法将返回 Author而不是Object 。完成更改后,您应该能够访问作者的 id像这样:

List<Author> authorList = db.getAllAuthors();
int id = authorList.get(0).getID(); // the id of the first author in the list

请注意 List 的一般规则在这里应用,所以如果列表为空则 get(0)会抛出异常。

更新

您可以使用AdapterView.getItemAtPosition(int)方法来访问已在 OnItemClickListener 中单击的数据对象。因为你定义了你的ArrayAdapterArrayAdapter<Author> ,您保证 getItemAtPosition()将返回Author实例,因此您可以编写如下代码:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Author author = (Author) parent.getItemAtPosition(position);

Intent intent = new Intent(MainActivity.this, StoriesActivity.class);
intent.putExtra("author_id", author.getID());
intent.putExtra("author_name", author.getName());

startActivity(intent);
}

当您开始处理更复杂的对象时,如果您发现将每个单独的字段(例如 nameid )添加到 Intent 会很烦人。您可以一一考虑实现 Parcelable对象上的接口(interface):https://developer.android.com/reference/android/os/Parcelable.html

然后您只需添加 AuthorIntent直接,而不是添加其字段。

关于java - 访问 ArrayList 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47024041/

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