我正在实现一个onListItemClick方法,我得到IndexOutofBoundsException:
java.lang.IndexOutOfBoundsException: Index: 4, Size: 0
at java.util.ArrayList.get(ArrayList.java:411)
at com.lalosoft.roomdemo.MainActivity.onListItemClick(MainActivity.java:91)
at com.lalosoft.roomdemo.ProductAdapter$ProductViewHolder.onClick(ProductAdapter.java:114)
at android.view.View.performClick(View.java:5647)
at android.view.View$PerformClick.run(View.java:22465)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6577)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
我的MainActivity代码是:
public class MainActivity extends AppCompatActivity implements
ProductAdapter.ListItemClickListener {
private RecyclerView recyclerView;
private Toast mToast;
List<Pelates> pelates;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
// run the sentence in a new thread
new Thread(new Runnable() {
@Override
public void run() {
pelates = App.get().getDB().productDao().getAll();
boolean force = App.get().isForceUpdate();
if (force || pelates.isEmpty()) {
retrieveProducts();
} else {
populateProducts(pelates);
}
}
}).start();
}
@Override
public void onListItemClick(int clickedItemIndex) {
String toastMessage = "Item #" + clickedItemIndex + " name= "+ pelates.get(clickedItemIndex).getName();
mToast = Toast.makeText(this, toastMessage, Toast.LENGTH_SHORT);
mToast.show();
}
private void retrieveProducts() {
List<Pelates> list = new ArrayList<>();
String names[] = {"Νεκτάριος", "Giannis", "Kostas", "Panagiotis", "Athina", "Maria", "Αντωνία"};
String surnames[] = {"Κοντολαιμάκης", "Papadak", "Androulak", "Iliopoulos", "Kouts", "Ioannidou", "Ιωακειμίδου"};
int arxikoPoso[] = {500, 3000, 5000, 4500, 10000, 1000, 100};
for (int i = 0; i < 7; i++) {
Pelates pelates = new Pelates();
pelates.setName(names[i]);
pelates.setSurname(surnames[i]);
pelates.setArxiko_poso(arxikoPoso[i]);
pelates.setImageUrl("http://lorempixel.com/500/500/technics/" + i);
list.add(pelates);
}
// insert product list into database
App.get().getDB().productDao().insertAll(list);
// disable flag for force update
App.get().setForceUpdate(false);
populateProducts(list);
}
private void populateProducts(final List<Pelates> pelates) {
runOnUiThread(new Runnable() {
@Override
public void run() {
recyclerView.setAdapter(new ProductAdapter(pelates, MainActivity.this));
}
});
}
}
我在第一次我得到IndexOutOfBoundsException 我在以下代码行运行我的应用程序:
String toastMessage = "Item #" + clickedItemIndex + " name= "+ pelates.get(clickedItemIndex).getName();
第一次之后我没有得到任何错误。 我正在使用Room数据库,并且只在我第一次运行应用程序时才使用数据填充数据库。
我试着看到pelates的大小,但它是零。为什么呢?
答案 0 :(得分:1)
您应该在List<Pelates> pelates;
内填充retrieveProducts()
,而不是创建新列表。