gpt4 book ai didi

java - 如何将 ArrayList 转换为 ArrayList

转载 作者:行者123 更新时间:2023-12-01 16:38:13 27 4
gpt4 key购买 nike

我目前正在尝试为 SQLite 数据库的 ListView 创建搜索功能。但要做到这一点,我需要将数组列表“userList”中的数据库值转换为字符串,以便可以在菜单中使用它?我需要 userList 是一个字符串数组,以便我可以在搜索功能中使用它们。

编辑:我可以不这样做而只将“Id Entry”变量放入数组列表中吗?我怎样才能做到这一点?

这是我的代码:

public class ViewListContents extends AppCompatActivity {


DatabaseHelper deviceDB;
ArrayList<User> userList;
//ArrayList<String> listItem;

ListView listView;
User user;


ArrayAdapter adapter1;
ArrayList<String> list;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewcontents_layout); //sets layout

deviceDB = new DatabaseHelper(this); //initialises DB for database class
userList = new ArrayList<>();
Cursor data = deviceDB.getListContents();


list = new ArrayList<>();
//FiveColumn_ListAdapter adapter1 = new FiveColumn_ListAdapter(this,R.layout.list_adapter_view,list);


int numRows = data.getCount();
if (numRows == 0 ){
Toast.makeText(ViewListContents.this,"The database was empty",Toast.LENGTH_LONG).show();
}
else {
while (data.moveToNext()){
user = new User(data.getString(0),data.getString(1),data.getString(2),data.getString(3),data.getString(4),data.getString(5)); //retrieving columns from data
userList.add(user); //userlist array takes items of type user
}
}

FiveColumn_ListAdapter adapter = new FiveColumn_ListAdapter(this,R.layout.list_adapter_view,userList);
listView = (ListView) findViewById(R.id.ListView);
listView.setAdapter(adapter);




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);

MenuItem searchItem = menu.findItem(R.id.item_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {



return false;
}

@Override
public boolean onQueryTextChange(String s) {

//ArrayList<String> List = new ArrayList<>();

for (User user : userList){
if (user.
}

return true;
}
});




return super.onCreateOptionsMenu(menu);
}

}

User 是我为获取此处的变量而创建的自定义类:

public User (String fID_Entry, String fName, String fAddress, String fLatitude, String fLongtitude, String fTime){
ID_Entry = fID_Entry;
Name = fName;
Address = fAddress;
Latitude = fLatitude;
Longtitude = fLongtitude;
Time = fTime;

}

public String getID_Entry() {
return ID_Entry;
}

public String getName() {
return Name;
}

public String getAddress() {

return Address;
}

public String getLatitude() {

return Latitude;
}

public String getLongtitude() {

return Longtitude;
}
public String getTime() {
return Time;
}
}

最佳答案

重写 toString 方法或创建您自己的方法来获取 User 类中的类 String 表示形式,然后您可以执行以下操作:

List<String> usersStrings = userList.stream().map(User::representation).collect(Collectors.toList());

或者例如:

ArrayList<String> idList = new ArrayList<>();
for (int i = 0; i < user_list.size(); i++) {
// This line depend on how you want to represent your user string
idList.add(user_list.get(i).getID_Entry());
}

我还更改了您的字段名称。它们应该始终以小​​写字母开头,以免与类混淆。而且也不需要前缀,因为大多数 IDE 都会为您着色。尝试对字段和方法坚持使用驼峰命名法,对类使用帕斯卡命名法。

https://en.wikipedia.org/wiki/Camel_case

class User {

String idEntry;
String name;
String address;
String latitude;
String longtitude;
String time;

public String representation() {
// This could be implemented as you want
return name + " " + idEntry;
}

public User (String fID_Entry, String fName, String fAddress, String fLatitude, String fLongtitude, String fTime){
idEntry = fID_Entry;
name = fName;
address = fAddress;
latitude = fLatitude;
longtitude = fLongtitude;
time = fTime;
}

public String getID_Entry() {
return idEntry;
}

public String toString() {
// This is also used when you will try to print the object
// You can change how you want it to look
return "User{" +
"idEntry='" + idEntry + '\'' +
", name='" + name + '\'' +
", address='" + address + '\'' +
", latitude='" + latitude + '\'' +
", longtitude='" + longtitude + '\'' +
", time='" + time + '\'' +
'}';
}

public String getName() {
return name;
}

public String getAddress() {
return address;
}

public String getLatitude() {
return latitude;
}

public String getLongtitude() {
return longtitude;
}

public String getTime() {
return time;
}
}

请注意,当调用第一个代码段中的代码时,您的用户将表示为他们的姓名 + ""+ id。如果您想要不同的东西,您可以调整表示方法。

关于java - 如何将 ArrayList<User> 转换为 ArrayList<String>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61914492/

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