gpt4 book ai didi

java - 针对特定位置的recyclerview setadapter

转载 作者:行者123 更新时间:2023-12-02 00:10:35 27 4
gpt4 key购买 nike

我使用Recycleview创建了一个“水平 ListView ”。

由于我的程序中有 async 任务,我无法从 fetchbook 类检索图书结果,因此我需要在类内设置适配器。

我的代码如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );


ArrayList<String> names = new ArrayList<>();
names.add("szF_pLGmJTQC");
names.add("QTKD3-mMZFYC");

for (int i = 0; i<names.size(); i++){
fetchBooks(names.get( i ));

}

}


private void fetchBooks(String query) {

client = new MyBookClient();
final ArrayList<String> authors = new ArrayList<>( );
final ArrayList<String> titles = new ArrayList<>( );
final ArrayList<String> image = new ArrayList<>( );
MyBook books = new MyBook();
client.getBooks( query, new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response){
if(response!=null){
MyBook books = MyBook.fromJson(response);

authors.add(books.getAuthor());
titles.add(books.getTitle());
image.add(books.getCoverUrl());

RecyclerView recyclerView = findViewById(R.id.rvAnimals);
LinearLayoutManager horizontalLayoutManager
= new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(horizontalLayoutManager);
adapter = new MyRecyclerViewAdapter(MainActivity.this, authors, titles,image);
adapter.setClickListener(MainActivity.this);
recyclerView.setAdapter(adapter);

Toast.makeText(MainActivity.this, "You clicked " + books.getAuthor(), Toast.LENGTH_SHORT).show();
}
}
});
}

但是,它会导致适配器清理先前的 View 并添加当前的 View 。

适配器的代码添加如下:

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

private List<String> mViewAuthor;
private List<String> mTitles;
private List<String> mimages;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
private Context mContext;

MyRecyclerViewAdapter(Context context, List<String> authors, List<String> titles, List<String> image) {
this.mInflater = LayoutInflater.from(context);
this.mViewAuthor = authors;
this.mTitles = titles;
this.mimages = image;
this.mContext = context;
}

@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate( R.layout.recyclerview_item, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

String color = mViewAuthor.get(position);
String animal = mTitles.get(position);
String image = mimages.get( position);
holder.tvAuthor.setText(color);
holder.tvTitle.setText(animal);

Picasso.with(mContext).load(Uri.parse(image)).error(R.drawable.ic_nocover).into(holder.ivBookCover);
}

@Override
public int getItemCount() {
return mViewAuthor.size();
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tvAuthor;
TextView tvTitle;
ImageView ivBookCover;

ViewHolder(View itemView) {
super(itemView);
tvAuthor = itemView.findViewById(R.id.tvAuthor);
tvTitle = itemView.findViewById(R.id.tvTitle);
ivBookCover = itemView.findViewById(R.id.ivBookCover);
itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}

public String getItem(int id) {
return mViewAuthor.get(id);
}

public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}

// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}

}

MyBook的代码是:

public class MyBook implements Serializable {
private String author;
private String title;
private String bookid;
private String rating;

public String getTitle() {
return title;
}

public String getAuthor() {
return author;
}

public String getId() {
return bookid;
}

public String getRating(){
return rating;
}


public static MyBook fromJson(JSONObject jsonObject) {
MyBook book = new MyBook();
try {

JSONObject volumeInfo = jsonObject.getJSONObject( "volumeInfo" );
book.title = volumeInfo.has("title") ? volumeInfo.getString("title") : "";
book.author = getAuthor(volumeInfo);
book.bookid = jsonObject.has("id") ? jsonObject.getString("id") : "";
book.rating = volumeInfo.has("averageRating") ? String.valueOf(volumeInfo.getDouble("averageRating")) : "0";
} catch (JSONException e) {
e.printStackTrace();
return null;
}
return book;
}

private static String getAuthor(final JSONObject jsonObject) {
try {
final JSONArray authors = jsonObject.getJSONArray("authors");
int numAuthors = authors.length();
final String[] authorStrings = new String[numAuthors];
for (int i = 0; i < numAuthors; ++i) {
authorStrings[i] = authors.getString(i);
}
return TextUtils.join(", ", authorStrings);
} catch (JSONException e) {
return "";
}
}

}

有什么方法可以调用setadapter,同时定义我想要添加 View 的位置吗?

类似于:

fetchBooks(names.get( i ), i);

其中第二个参数i是适配器内的位置,因此我可以在位置i=0处获得第一个结果,然后在i=1处获得第二个结果。

或者有没有一种方法可以从 fetchBooks 类返回书籍?

谢谢

最佳答案

However, it causes the adapter to clean the previous view and add on it the current.

据我所知,您正在尝试将查询结果添加到 RecyclerView 列表中,但问题是它在添加新查询结果时从列表中删除了旧/上一个查询结果。并且您需要保留所有结果。如有错误请指正。

此行为可能是由于每次在 onSucess() 方法内获得新的查询结果时实例化新适配器所致。所以你可以通过将这部分代码转移到onCreate()方法来避免这种情况;然后,对于每个 onSuccess() 调用,您只需调用 adapter.notifyDataSetChanged() 即可通知适配器其数据源已更改,因此请自行更新。

这也要求我们不要用新数据实例化适配器,而只需向适配器添加新方法,以使用 .addAll() 方法更新列表。

所以改变:

MyRecyclerViewAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );

ArrayList<String> names = new ArrayList<>();
names.add("szF_pLGmJTQC");
names.add("QTKD3-mMZFYC");

RecyclerView recyclerView = findViewById(R.id.rvAnimals);
LinearLayoutManager horizontalLayoutManager
= new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(horizontalLayoutManager);
adapter = new MyRecyclerViewAdapter(MainActivity.this, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
adapter.setClickListener(MainActivity.this);
recyclerView.setAdapter(adapter);


for (int i = 0; i<names.size(); i++){
fetchBooks(names.get( i ));
}

}


private void fetchBooks(String query) {

client = new MyBookClient();
MyBook books = new MyBook();
client.getBooks( query, new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response){
if(response!=null){
MyBook books = MyBook.fromJson(response);

adapter.addBooks(books.getAuthor(), books.getTitle(), books.getCoverUrl());

Toast.makeText(MainActivity.this, "You clicked " + books.getAuthor(), Toast.LENGTH_SHORT).show();
}
}
});
}

并将以下方法添加到适配器代码

public void addBook(String author, String title, String image) {
this.mViewAuthor.add(author);
this.mTitles.add(title);
this.mimages.add(image);
this.notifyDataSetChanged();
}

如果您需要进一步的帮助,我随时欢迎

关于java - 针对特定位置的recyclerview setadapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58124511/

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