gpt4 book ai didi

android - 如何在 Android 中创建可打包对象列表?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:28:14 25 4
gpt4 key购买 nike

所以我有一个应用程序可以通过 API 在互联网上搜索艺术家,并在执行手头的搜索后显示结果列表(艺术家姓名)。我想在更改设备的方向(纵向 - 横向)时保留此列表,而不必再次执行搜索。这是一些代码:

public class MainActivityFragment extends Fragment{

public static final String ARTIST_ID = "artistId";
private final String NO_RESULTS = "No artists found. Please check your input!";

private List<Artist> artists = new ArrayList<>();
private ArtistArrayAdapter adapter;

public MainActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_main, container, false);

final SearchView searchView = (SearchView) rootView.findViewById(R.id.search_artist);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String artistName) {
SearchArtist(artistName);
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
return true;
}

@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});

ListView listView = (ListView) rootView.findViewById(android.R.id.list);
adapter = new ArtistArrayAdapter(getActivity(), R.layout.artist_list_item, artists);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Artist artist = artists.get(position);
String artistId = artist.id;
Intent detailArtist = new Intent(getActivity(), DetailActivity.class);
detailArtist.putExtra(ARTIST_ID, artistId);
startActivity(detailArtist);
}
});

return rootView;
}

所以我认为我必须使艺术家列表可打包,然后将其传递给 onSaveInstanceState 方法。但是如何使此列表可打包?

非常感谢,我希望这不会太困惑...

最佳答案

您需要为您的艺术家数据模型类实现 Parcelable 接口(interface)。例如

public class ArtistParcelable implements Parcelable {
public String id;
public String artistName;
public List<String> artistImageUrls = Collections.emptyList();

public ArtistParcelable(Artist artist) {
// Do the assignments here
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
//write
dest.writeString(this.id);
dest.writeString(this.artistName);
dest.writeStringList(this.artistImageUrls);
}

protected ArtistParcelable(Parcel in) {
//retrieve
this.id = in.readString();
this.artistName = in.readString();
this.artistImageUrls = in.createStringArrayList();
}

public static final Parcelable.Creator<ArtistParcelable> CREATOR = new Parcelable.Creator<ArtistParcelable>() {
public ArtistParcelable createFromParcel(Parcel source) {
return new ArtistParcelable(source);
}

public ArtistParcelable[] newArray(int size) {
return new ArtistParcelable[size];
}
};
}

在你的 fragment 中 - 如果你有 List mArtists,那么你可以像

@Override
public void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList(ARTISTS, (ArrayList<? extends Parcelable>) mArtists);
}

并在onCreateView中

if (savedInstanceState != null) {
mArtists = savedInstanceState.getParcelableArrayList(ARTISTS);

这里有一个很好的指南,您可以遵循它。 https://guides.codepath.com/android/Using-Parcelable

有一个名为 Android Parcelable Code Generator 的 IDE 插件也可用,它会为您生成所有样板代码。

还有一个库叫做 Parceler ,这非常方便。它完全删除了样板。

关于android - 如何在 Android 中创建可打包对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31006373/

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