如何将信息从RecyclerView传递到另一个活动

时间:2018-06-28 00:05:52

标签: java android

我如何使用Array和MySQL将信息从recyclerView传递到另一个活动,我正在搜索不同的方式,但是我做不到,但是我尝试按照以下方式进行操作:

private void ReadDataFromDB() {
    JsonObjectRequest jreq = new JsonObjectRequest(Request.Method.GET, url,
            new com.android.volley.Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    try {
                        int success = response.getInt("success");
                        if (success == 1) {
                            JSONArray ja = response.getJSONArray("rutas");
                            for (int i = 0; i < ja.length(); i++) {

                                JSONObject jobj = ja.getJSONObject(i);
                                HashMap<String, String> item = new HashMap<String, String>();
                                // item.put(ITEM_ID, jobj.getString(ITEM_ID));
                                item.put(ITEM_RUTA,
                                        jobj.getString(ITEM_RUTA));
                                item.put(ITEM_VEH,
                                        jobj.getString(ITEM_VEH));

                                Item_List.add(item);
                            }
                            String[] from = {ITEM_RUTA, ITEM_VEH};
                            int[] to = {R.id.i_ruta, R.id.i_veh};

                            adapter = new SimpleAdapter(
                                    getApplicationContext(), Item_List,
                                    R.layout.message_list_row, from, to);

                            mRecyclerView.setAdapter(mAdapter);

                            mRecyclerView.setOnClickListener(new onMessageRowClicked() );

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    AppController.getInstance().addToRequestQueue(jreq);
}

我需要将after信息传递给follow方法

@Override
public void onMessageRowClicked (int i) {
    if (mAdapter.getSelectedItemCount() > i) {
        enableActionMode(i);
    } else {
        Message message = messages.get(i);
        message.setRead(true);
        messages.set(i, message);
        mAdapter.notifyDataSetChanged();

        Toast.makeText(getApplicationContext(), "Read: hola" + message.getParadas() + message.getVehiculo(), Toast.LENGTH_SHORT).show();

        Intent saag_intent = new Intent(ge_MainActivity.this,
                ge_Traker_maps.class);
        saag_intent.putExtra("ruta", Item_List.get(i));
        saag_intent.putExtra("vehiculo", Item_List.get(i));
        startActivity(saag_intent);
    }
}

这是在以下行中显示的问题或错误

  

无法解析符号onMessageRowClicked

mRecyclerView.setOnClickListener(new onMessageRowClicked() );

1 个答案:

答案 0 :(得分:0)

RecyclerItemClickListener.java

public class RecyclerItemClickListener implements 

RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;

public interface OnItemClickListener {
    public void onItemClick(View view, int position);

    public void onLongItemClick(View view, int position);
}

    GestureDetector mGestureDetector;

    public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && mListener != null) {
                    mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
                }
            }
        });
    }

    @Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
        View childView = view.findChildViewUnder(e.getX(), e.getY());
        if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
            mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
            return true;
        }
        return false;
    }

    @Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }

    @Override
    public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}

//使用方法

mRecyclerView.addOnItemTouchListener(
                new RecyclerItemClickListener(this, file_list ,new RecyclerItemClickListener.OnItemClickListener() {
                    @Override public void onItemClick(View view, int position) {
                       // write code here for single click
    Message message = messages.get(i);
    message.setRead(true);
    messages.set(i, message);
    mAdapter.notifyDataSetChanged();

    Toast.makeText(getApplicationContext(), "Read: hola" + message.getParadas() + message.getVehiculo(), Toast.LENGTH_SHORT).show();

    Intent saag_intent = new Intent(ge_MainActivity.this,
            ge_Traker_maps.class);
    saag_intent.putExtra("ruta", Item_List.get(i));
    saag_intent.putExtra("vehiculo", Item_List.get(i));
    startActivity(saag_intent);
                    }
                    @Override public void onLongItemClick(View view, int position) {        
                      // write code here for long click
                        }
                    })
        );