我試圖在有人搜索某個單詞時替換我的RecyclerView中的項目。 GIF列表被髮送到我的回調方法中,並且我遍歷了這些對象,並且想要替換RecyclerView中的每個對象。但是,當我嘗試執行此操作時,如果我在我的適配器中調用ArrayList.set()
,則會發生IndexOutOfBoundsException。更換RecyclerView項目時獲取IndexOutOfBoundsException
我得到它發生的原因......我的適配器中的ArrayList中有零個項目。我的問題是爲什麼?我究竟做錯了什麼?
我的活動代碼:
package hidden.package;
// Imports hidden
public class GifSearchActivity extends AppCompatActivity {
@BindView(R.id.gifSearchBar)
FloatingSearchView searchGifs;
@BindView(R.id.gifSearchResults)
RecyclerView searchResults;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gifsearch);
ButterKnife.bind(this);
final GIFResultsAdapter adapter = new GIFResultsAdapter();
final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(GifSearchActivity.this);
final GIFInterface i = new GIFInterface();
i.setSearchListener(new GIFInterface.GIFSearchListener() {
@Override
public void onSearchResultsRetrieved(final GIFSearchResults gifSearchResults) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("GAB", "Results Retrieved...");
int count = 0;
for (GIF gif : gifSearchResults.getGIFs()) {
adapter.replaceItem(gif, count);
count++;
}
}
});
}
});
searchResults.setLayoutManager(new GridLayoutManager(getBaseContext(), 1, GridLayoutManager.VERTICAL, false));
searchResults.setItemAnimator(new DefaultItemAnimator());
searchResults.setAdapter(adapter);
searchGifs.setOnQueryChangeListener(new FloatingSearchView.OnQueryChangeListener() {
@Override
public void onSearchTextChanged(String oldQuery, String newQuery) {
i.searchForGif(prefs.getString("token", ""), newQuery);
}
});
}
}
我的適配器代碼:
package hidden.package;
// Imports hidden
@SuppressWarnings("deprecation")
public class GifResultsAdapter extends RecyclerView.Adapter<GifResultsAdapter.GifsViewHolder> {
private ArrayList<GIF> gifs = new ArrayList<>();
private Context con;
public class GifsViewHolder extends RecyclerView.ViewHolder {
ImageView gif;
ProgressBar gifProgressBar;
public GifsViewHolder(View itemView) {
super(itemView);
gif = (ImageView) itemView.findViewById(R.id.gif);
gifProgressBar = (ProgressBar) itemView.findViewById(R.id.gifProgressBar);
}
}
public void replaceItem(GIF gif, int position) {
gifs.set(position, gif);
notifyItemChanged(position);
}
@Override
public GifsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d("GAB", "ViewHolder Created");
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.util_gif_row, parent, false);
this.con = parent.getContext();
return new GifsViewHolder(itemView);
}
@Override
public void onBindViewHolder(final GifsViewHolder holder, int position) {
Log.d("GAB", "ViewHolder bound");
final GIF gif = gifs.get(position);
Glide.with(con)
.load(gif.getUrl())
.override(700, 700)
.fitCenter()
.placeholder(new ColorDrawable(AndroidHelper.darkenColor(con.getResources().getColor(R.color.colorAccent), 0.95f)))
.into(holder.gif);
}
@Override
public int getItemCount() {
return gifs.size();
}
}
編輯:這裏是例外...
02-25 00:41:46.371 29205-29205/ai.gab.android E/UncaughtException: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.set(ArrayList.java:427)
at ai.gab.android.ui.adapter.GIFResultsAdapter.replaceItem(GIFResultsAdapter.java:59)
at ai.gab.android.ui.activity.GifSearchActivity$1$1.run(GifSearchActivity.java:68)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
一套全新的眼光,將不勝感激!
我應該提到,起初,我嘗試將GIF添加到RV ...但是最終添加了用戶所做的每個查詢的GIF。因此,如果我輸入'angry',它將以gif的形式加載查詢'a','an','ang'等,這就是爲什麼我要替換結果的原因。 –
您創建一個GIF對象的空列表('gifs')。你需要在構造函數中傳遞你的列表(例如) – GVillani82