使用Volley Library获取onItemClick上的数据

时间:2017-12-22 22:55:06

标签: android android-volley

我是Android开发的新手,我已经使用volley和RecyclerView设置了api调用,感谢来自这里和其他教程的一些很好的帮助,但是我遇到了在RecyclerView中为新活动获取项目点击数据的问题获取数据,该应用程序只是崩溃。以下是我的课程:

    public class Main_Food extends AppCompatActivity {

        private RecyclerView mRecyclerView;
        private RecyclerView.Adapter mAdapter;
        private ArrayList<ListItem> mListItems;


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

            mRecyclerView = (RecyclerView) findViewById(recyclerView);
            mRecyclerView.setHasFixedSize(true);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));


            mListItems = new ArrayList<>();

            recyclerLoadViewData();

        }


        private void recyclerLoadViewData() {

            String sURL = getString(R.string.originalUrl) + getString(R.string.API_KEY);

            StringRequest stringRequest = new StringRequest(Request.Method.GET, sURL,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String s) {
                            try {

                                JSONObject jsonObject = new JSONObject(s);
                                JSONArray array = jsonObject.getJSONArray("recipes");

                                for (int i = 0; i < array.length(); i++) {
                                    JSONObject oB = array.getJSONObject(i);
                                    ListItem item = new ListItem(
                                            oB.getString("publisher"),
                                            oB.getString("title"),
                                            oB.getString("image_url"),
                                            oB.getString("recipe_id")
                                    );
                                    mListItems.add(item);
                                }
                                mAdapter = new myAdapter(mListItems, getApplicationContext());
                                mRecyclerView.setAdapter(mAdapter);

                            } catch (JSONException e) {

                                e.printStackTrace();

                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {

                        }
                    });
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
        }

以下是myAdapter类:

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

    private ArrayList<ListItem> mItems;
    private Context context;

    public myAdapter(ArrayList<ListItem> mItems, Context context) {
        this.mItems = mItems;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_list_item, parent, false);
        RecyclerView.ViewHolder viewHolder = new ViewHolder(v, context, mItems);
        return (ViewHolder) viewHolder;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ListItem listItem = mItems.get(position);

        holder.textViewTitle.setText(listItem.getTitle());
        holder.textViewPub.setText(listItem.getPub());

        Picasso.with(context)
                .load(listItem.getImageUrl())
                .into(holder.mImageView);
    }

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

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

        public TextView textViewTitle;
        public TextView textViewPub;
        public ImageView mImageView;

        ArrayList<ListItem> listItems = new ArrayList<>();
        Context context;

        public ViewHolder(@NonNull View view, Context context, ArrayList<ListItem> listItems) {
            super(view);

//            view.setOnClickListener(this);

            this.listItems = listItems;
            this.context = context;

            textViewTitle = (TextView) view.findViewById(R.id.title);
            textViewPub = (TextView) view.findViewById(R.id.pub);
            mImageView = (ImageView) view.findViewById(R.id.image);

        }



//        @Override
//        public void onClick(View v) {
//            int position = getAdapterPosition();
//            ListItem listItem = this.listItems.get(position);
//            Intent intent = new Intent(this.context,recipeDetails.class);
//            intent.putExtra("repId", listItem.getRepId().toString());
//
//            this.context.startActivity(intent);
//        }
    }
}

我知道onclick事件都被注释掉了,因为我已经看到使用recyclertouchlistner的实例,但我不确定如何实现它来获取点击数据的项目?请帮忙。

1 个答案:

答案 0 :(得分:0)

onBindViewHolder方法

中添加此内容
 holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this.context,recipeDetails.class);
        intent.putExtra("repId", listItem.getRepId().toString());
        context.startActivity(intent);
    }
});