在Android中设置onItemClickListener以便RecyclerView项目

时间:2019-06-02 11:50:10

标签: java android android-recyclerview indexoutofboundsexception onitemclicklistener

我正在尝试为RecyclerView项设置OnItemClickListener,以在单击RecyclerView中的项时打开一个新活动。

我为Android手机开发,并且正在使用API​​ 19实际设备(我的应用程序支持的最低API是15)。我已经在线上尝试了不同的示例,但是我无法通过工作代码来解决它。

这些是我在错误消息中得到的行:

LevelActivity:

public class LevelActivity extends AppCompatActivity implements ViewHolder.OnItemListener {

    private static final String TAG = "LevelActivity";

    private List<Item> list = new ArrayList<>();

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

                List<Item> list = fill_with_data();

                RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_item_list);
                RecyclerViewAdapter adapter = new RecyclerViewAdapter(list, getApplication(),this);
                recyclerView.setAdapter(adapter);
                recyclerView.setLayoutManager(new LinearLayoutManager(this));

            }

    public List<Item> fill_with_data() {

        List<Item> list = new ArrayList<>();

        list.add(new Item("Batman vs Superman"));
        list.add(new Item("X-Men: Apocalypse"));
        list.add(new Item("Captain America: Civil War"));
        list.add(new Item("Kung Fu Panda 3"));
        list.add(new Item("Warcraft"));
        list.add(new Item("Alice in Wonderland"));

        return list;
    }

    @Override
    public void onItemClick(int position) {

        list.get(position);
        Intent intent = new Intent(this,QuestionActivity.class);
            startActivity(intent);
    }


}

ViewHolder:

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView name;
    OnItemListener mOnItemListener;

    ViewHolder(View itemView, OnItemListener onItemListener) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.row_item);
        this.mOnItemListener = onItemListener;

        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        mOnItemListener.onItemClick(getAdapterPosition());
    }

    public interface OnItemListener {
        void onItemClick(int position);
    }

AndroidManifest:

        <!-- A child of a the level activity -->
        <activity android:name=".QuestionActivity"
            android:label="Question Activity"
            android:parentActivityName=".LevelActivity" >
            <!-- Parent activity meta-data to support 4.0 and lower -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".LevelActivity" />>
        </activity>

如果您需要更多代码,请告诉我。

我希望单击RecyclerView列表项并打开一个新的活动(QuestionActivity)。这将是一个测验应用程序,当您单击不同级别时,它将打开一个不同的开始问题,但布局与QuestionActivity相同。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ..., PID: 1269
    java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at com.example.marvelquiz.LevelActivity.onItemClick(LevelActivity.java:58)
        at com.example.marvelquiz.ViewHolder.onClick(ViewHolder.java:22)
        at android.view.View.performClick(View.java:4637)
        at android.view.View$PerformClick.run(View.java:19422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5586)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您的list

您的错误意味着您的列表为空,并且您希望给索引0,但列表为空

list.get(position);//Error line

您错误地将数据放入列表的新实例中,因此您的全局列表仍然为空。

更改此行

List<Item> list = fill_with_data();

对此

list = fill_with_data();