gpt4 book ai didi

java - 如何从另一个数组中存在的值创建数组

转载 作者:行者123 更新时间:2023-12-01 18:44:17 26 4
gpt4 key购买 nike

我有一个小问题无法解决:我有一个从 API 获取的对象数组(国家/地区)。每个对象中的参数之一(国家/地区)是一个边界国家/地区 - 值名称 = 边界的数组。在一个 fragment 中,我显示了从 API 获取的所有国家/地区。我的目标之一是单击某个国家/地区,然后打开一个新屏幕,其中将显示与我单击的国家/地区接壤的所有国家/地区。

我已经拥有在屏幕之间切换的所有逻辑,两个屏幕上的 RecyclerViews 等等,但我不知道如何创建和解析新屏幕上的接壤国家/地区的新数组。

这就是我现在所拥有的:

所有国家/地区适配器:

public class CountryAdapter extends RecyclerView.Adapter<CountryAdapter.CountryViewHolder> {


@Override
public void onBindViewHolder(@NonNull CountryViewHolder holder, final int position) {
holder.mTVCountryName.setText(countries.get(position).getName());
holder.mTVCountryNativeName.setText(countries.get(position).getNativeName());

GlideToVectorYou
.init()
.with(mContext)
.load(Uri.parse(countries.get(position).getFlag()), holder.mCountryFlag);

holder.mCountryFlag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {


if (iOnItemClickListener != null) {
//Array list of all border countries by alpha3 value
List<String> borderCountriesAlpha3 = new ArrayList<>();
for (int i = 0; i < countries.get(position).getBorders().size(); i++) {
try {
borderCountriesAlpha3.add(countries.get(position).getBorders().get(i));
} catch (Exception e) {

}
}
iOnItemClickListener.onItemClick(position, borderCountriesAlpha3);
}
}
});
}

从API获取数据:

public void getAllCountries(){
ApiService apiService = ApiClient.getRetrofitInstance().create(ApiService.class);
call = apiService.getAllCountries();
call.enqueue(new Callback<List<Country>>() {
@Override
public void onResponse(Call<List<Country>> call, Response<List<Country>> response) {
mCountryPresenter.generateDataList(response.body());
}

@Override
public void onFailure(Call<List<Country>> call, Throwable t) {

}
});
}

国家 pojo 类:

public class Country implements Comparable<Country> {

@SerializedName("name")
@Expose
private String name;
@SerializedName("alpha3Code")
@Expose
private String alpha3Code;
@SerializedName("borders")
@Expose
private List<String> borders = null;
@SerializedName("nativeName")
@Expose
private String nativeName;

public String getName() {
return name;
}

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

public String getAlpha3Code() {
return alpha3Code;
}

public void setAlpha3Code(String alpha3Code) {
this.alpha3Code = alpha3Code;
}


public List<String> getBorders() {
return borders;
}

public void setBorders(List<String> borders) {
this.borders = borders;
}

public String getNativeName() {
return nativeName;
}

public void setNativeName(String nativeName) {
this.nativeName = nativeName;
}
}

包含所有国家/地区回收商 View 的 fragment :

public class CountryListFragment extends Fragment implements CountryPresenter {

private View view;
private RecyclerView mRVCountries;
private CountryAdapter mCountryAdapter;
private CountriesView mCountriesView;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_country_list, container, false);
initView(view);
initClasses();
initContentView();
return view;
}

private void initClasses() {
mCountriesView = new CountriesView(this, getContext());
}

private void initView(View view) {
mRVCountries = view.findViewById(R.id.rvCountries);
}

private void initContentView() {
mCountriesView.getAllCountries();
}

@Override
public void generateDataList(List<Country> countries) {
mCountryAdapter = new CountryAdapter(countries, getContext(), getActivity());
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
mRVCountries.setLayoutManager(layoutManager);
mRVCountries.setAdapter(mCountryAdapter);

mCountryAdapter.setiOnItemClickListener(new CountryAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position, List<String> borderCountries) {
Log.d("myDebug", "onItemClick: " + position + " " + borderCountries);

}
});
}

public void sortByNativeName() {
mCountryAdapter.sortByNativeName();
}

public void sortByArea() {
mCountryAdapter.sortByArea();
}

}

最佳答案

您可以使用Java的流API或Collections API来搜索列表、将子列表复制到另一个列表等。以下是您要求查找给定国家/地区的边界国家/地区并丢失边界国家/地区对象的示例-

package com.advisory.pic.etl.claim.standardize;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Test {

public static void main(String[] args) {

}

public void workOnCountries(){
List<Country> countries = Arrays.asList(new Country("United States","USA",Arrays.asList("MXO","CDA")),
new Country("Mexico","MXO",Arrays.asList("USA","CLB")),
new Country("Colombia","CLB",Arrays.asList("MXO","VNZ")),
new Country("Canada","CNA",Arrays.asList("USA")));

//get list of neighbouring countries for USA
//first find USA in the list of countries
Country country = null;
for(Country c : countries){
if(c.alpha3Code.equalsIgnoreCase("USA")){
country = c;
}
}

//find border countries
List<String> b = country.borders;
//get border contries from main country list
List<Country> borderCountries = countries.stream().filter(c -> b.contains(c)).collect(Collectors.toList());
}

private class Country{

Country(String name, String alpha3Code, List<String> borders){
this.name = name;
this.alpha3Code = alpha3Code;
this.borders = borders;
}

private String name;
private String alpha3Code;
private List<String> borders = null;
}
}

关于java - 如何从另一个数组中存在的值创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59868969/

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