Java 如何从另一个数组中存在的值创建数组
我有一个我无法解决的小问题: 我有一个从API获取的对象数组(国家)。 每个对象(国家)中的一个参数是border Country-value name=border的数组。 在一个片段中,我展示了从API获得的所有国家。 我的目标之一是单击某个国家,并将打开一个新屏幕,其中将显示与我单击的国家接壤的所有国家 我已经具备了在屏幕之间切换的所有逻辑,在屏幕和更多屏幕上都可以循环使用新闻,但我不知道如何创建和解析新屏幕上的新边界国家数组 这就是我现在所拥有的: 所有国家:Java 如何从另一个数组中存在的值创建数组,java,android,Java,Android,我有一个我无法解决的小问题: 我有一个从API获取的对象数组(国家)。 每个对象(国家)中的一个参数是border Country-value name=border的数组。 在一个片段中,我展示了从API获得的所有国家。 我的目标之一是单击某个国家,并将打开一个新屏幕,其中将显示与我单击的国家接壤的所有国家 我已经具备了在屏幕之间切换的所有逻辑,在屏幕和更多屏幕上都可以循环使用新闻,但我不知道如何创建和解析新屏幕上的新边界国家数组 这就是我现在所拥有的: 所有国家: public class
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);
}
}
});
}
公共类CountryAdapter扩展了RecyclerView.Adapter{
@凌驾
公共无效onBindViewHolder(@NonNull CountryViewHolder holder,最终整数位置){
holder.mTVCountryName.setText(countries.get(position.getName());
holder.mTVCountryNativeName.setText(countries.get(position.getNativeName());
滑翔向你
.init()
.with(mContext)
.load(Uri.parse(countries.get(position.getFlag()),holder.mCountryFlag);
holder.mCountryFlag.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
如果(iOnItemClickListener!=null){
//按字母3值列出的所有边境国家的数组列表
List borderCountriesAlpha3=新建ArrayList();
对于(int i=0;ipublic 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) {
}
});
}
public void getAllCountries(){
ApiService ApiService=ApiClient.getRefughtInstance().create(ApiService.class);
call=apiService.getAllCountries();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
mCountryPresenter.generateDataList(response.body());
}
@凌驾
失败时公共无效(调用调用,可丢弃的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;
}
}
公共类国家/地区{
@序列化名称(“名称”)
@暴露
私有字符串名称;
@序列化名称(“字母代码”)
@暴露
私有字符串字母代码;
@序列化名称(“边框”)
@暴露
私有列表边框=null;
@SerializedName(“nativeName”)
@暴露
私有字符串nativeName;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getAlpha3Code(){
返回字母3码;
}
公共无效集合alpha3Code(字符串alpha3Code){
this.alpha3Code=alpha3Code;
}
公共列表getBorders(){
返回边界;
}
公共命令(列表边框){
this.borders=边界;
}
公共字符串getNativeName(){
返回本机名称;
}
公共void setNativeName(字符串nativeName){
this.nativeName=nativeName;
}
}
包含所有国家/地区的回收者视图的片段:
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();
}
}
公共类CountryListFragment扩展片段实现CountryPresenter{
私人视野;
私人回收站查看MRV国家;
私有CountryAdapter mCountryAdapter;
私人国家视图mccountriesview;
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u country\u list,container,false);
initView(视图);
初始化类();
initContentView();
返回视图;
}
私有类(){
mccountriesview=newcountriesview(this,getContext());
}
私有void initView(视图){
mRVCountries=view.findviewbyd(R.id.rvCountries);
}
私有void initContentView(){
mccountriesview.getAllCountries();
}
@凌驾
public void GenerateDalitist(列出国家/地区){
mCountryAdapter=新的CountryAdapter(国家,getContext(),getActivity());
RecyclerView.LayoutManager LayoutManager=新的LinearLayoutManager(getContext());
mRVCountries.setLayoutManager(layoutManager);
mRVCountries.setAdapter(mCountryAdapter);
mCountryAdapter.setiOnItemClickListener(新的CountryAdapter.OnItemClickListener(){
@凌驾
公共区域(国际位置,列出边界国家){
Log.d(“myDebug”、“onItemClick:+位置+”+边界国家);
}
});
}
public void sortByNativeName(){
mCountryAdapter.sortByValeName();
}
公共无效Sortbyrea(){
mCountryAdapter.sortByArea();
}
}
您可以使用Java的流式API或集合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;
}
}
package com.advision.pic.etl.claim.standarding;
导入java.util.array;
导入java.util.List;
导入java.util.stream.collector;
公开课考试{
公共静态void main(字符串[]args){
}
国家的公共部门(){
列表国家=Arrays.asList(新国家(“美国”、“美国”、Arrays.asList(“MXO”、“CDA”)),
新国家(“墨西哥”、“MXO”、Arrays.asList(“美国”、“CLB”),
mCountryAdapter.setiOnItemClickListener(new CountryAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position, List<String> borderCountries) {
Bundle bundle=new Bundle();
bundle.putSerializable("list",(ArrayList<String>) borderCountries);
// then you can add bundle to intent if you navigate activity or navigate
//fragment set this bundle
Log.d("myDebug", "onItemClick: " + position + " " + borderCountries);
}
});