how to make Recyclerview & Cardview onClick fetch

2020-05-08 08:44发布

i have some question with my recyclerview and cardview. This is the cases : 1. I've database with 3 values (name, image, description) 2. In my activity, i've recyclerview and cardview, they only show an image and the name of image.

This is my activity :

package com.example.mrm.myapplication;

public class Child_Tab1 extends Activity {

List<GetDataAdapter> GetDataAdapter1;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayaoutManager;
RecyclerView.Adapter recyclerViewadapter;
String GET_JSON_DATA_HTTP_URL = "http://www.kinandayu.com/image_content/getAllImage.php";
String JSON_IMAGE_TITLE_NAME = "name";
String JSON_IMAGE_URL = "image";

JsonArrayRequest jsonArrayRequest;
RequestQueue requestQueue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.child_tab_1);
    GetDataAdapter1 = new ArrayList<>();
    recyclerView = (RecyclerView) findViewById(R.id.rv1);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
    SpacesItemsDecoration decoration = new SpacesItemsDecoration(4);
    recyclerView.addItemDecoration(decoration);

    ActionBarTitleGravity();
    JSON_DATA_WEB_CALL();
}

private void ActionBarTitleGravity() {
    ActionBar actionbar = getActionBar();
    TextView textview = new TextView(getApplicationContext());
    LayoutParams layoutparams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    textview.setLayoutParams(layoutparams);
    textview.setText("Kinandayu");
    textview.setTextColor(Color.parseColor("#00acc1"));
    textview.setGravity(Gravity.START);
    textview.setTextSize(20);
    actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    actionbar.setCustomView(textview);
}

private void JSON_DATA_WEB_CALL() {
    jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse (JSONArray response) {
            JSON_PARSE_DATA_AFTER_WEBCALL(response);
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonArrayRequest);
}

public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array) {
    for (int i = 0; i<array.length(); i++) {
        GetDataAdapter GetDataAdapter2 = new GetDataAdapter();
        JSONObject json = null;
        try {
            json = array.getJSONObject(i);
            GetDataAdapter2.setImageTitleNamee(json.getString(JSON_IMAGE_TITLE_NAME));
            GetDataAdapter2.setImageServerUrl("http://" + json.getString(JSON_IMAGE_URL));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        GetDataAdapter1.add(GetDataAdapter2);
    }
        recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);
        recyclerView.setAdapter(recyclerViewadapter);
}
}

This is my RecyclerViewAdapter.java

package com.example.mrm.myapplication;

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

    Context context;
    List<GetDataAdapter> getDataAdapter;
    ImageLoader imageLoader1;

    public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){

        super();
        this.getDataAdapter = getDataAdapter;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent,false);
        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder Viewholder, int position) {
        GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);
        imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();
        imageLoader1.get(getDataAdapter1.getImageServerUrl(), ImageLoader.getImageListener
                (Viewholder.networkImageView, R.drawable.placeholder, android.R.drawable.ic_dialog_alert
                ));
        Viewholder.networkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1);
        Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
        public TextView ImageTitleNameView;
        public NetworkImageView networkImageView;

        public ViewHolder(View itemView) {
            super(itemView);
            ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item);
            networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1);
    }
}
}

My question is, if in my activity have 2 image on cardview from server, how to make that cardview clickable and take user to new activity who show an image with the decription of the image. I have read some question in here like Passing RecyclerView CardView Clicked Item Data To Activity but I don't know to implements to my code. Every answer is very helpful for me.

Thanks in advance

1条回答
爷、活的狠高调
2楼-- · 2020-05-08 08:57

In your onBindViewHolder-

Viewholder.networkImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent i = new Intent(context, NextActivity.class);
                i.putExtra("URL", getDataAdapter1.getImageServerUrl());
                context.startActivity(i);


            }
        });

Get the URL in your next activity and display the full image.

查看更多
登录 后发表回答