如何将价值从 recyclerview 项目传递到另一个活动

Posted

技术标签:

【中文标题】如何将价值从 recyclerview 项目传递到另一个活动【英文标题】:How to pass value from recyclerview item to another activity 【发布时间】:2015-11-21 06:43:19 【问题描述】:

当我们单击 recyclerview 项目时,我试图将 recyclerview 项目中的值传递给另一个活动。这里我使用OnItemTouchListener

我从 JSON 中检索数据并将其解析为 ArrayList。我保存了 5 个参数。 Title、ID、Rating、ReleaseDate、urlPoster,但现在我只显示 urlposter 中的 2 个参数、Title 和图像。

我想将其他参数传递给另一个活动,但我不知道该怎么做。

还有另一个类似这样的问题 (Values from RecyclerView item to other Activity),但他使用的是 OnClick,而不是 OnItemTouch,他在 ViewHolder 中执行此操作。我在互联网的某个地方读到这不是正确的做法。

这是我的代码

public class Tab1 extends Fragment 

    private static final String ARG_PAGE = "arg_page";
    private static final String STATE_MOVIES = "state movies";
    private TextView txtResponse;
    private String passID;

    // Progress dialog
    private ProgressDialog pDialog;


    //private String urlJsonArry = "http://api.androidhive.info/volley/person_array.json";
    private String urlJsonArry = "http://api.themoviedb.org/3/movie/popular?api_key=someapikeyhere";
    private String urlJsonImg = "http://image.tmdb.org/t/p/w342";

    // temporary string to show the parsed response
    private String jsonResponse = "";

    RecyclerView mRecyclerView;
    RecyclerView.LayoutManager mLayoutManager;
    RecyclerView.Adapter mAdapter;

    private ArrayList<Movies> movies = new ArrayList<Movies>();


    public Tab1() 
        // Required empty public constructor
    

    @Override
    public void onSaveInstanceState(Bundle outState) 
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList(STATE_MOVIES, movies);
    

    public static Tab1 newInstance(int pageNumber)

        Tab1 myFragment = new Tab1();
        Bundle arguments = new Bundle();
        arguments.putInt(ARG_PAGE, pageNumber);
        myFragment.setArguments(arguments);
        return myFragment;
    

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);



        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
    


    /**
     * Method to make json object request where json response starts wtih 
     * */
    private void makeJsonObjectRequest() 

        showpDialog();

        final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(urlJsonArry, new Response.Listener<JSONObject>() 


            @Override
            public void onResponse(JSONObject response) 
                //Log.d(TAG, response.toString());

                try 

                    JSONArray result = response.getJSONArray("results");

                    //Iterate the jsonArray and print the info of JSONObjects
                    for(int i=0; i < result.length(); i++)
                        JSONObject jsonObject = result.getJSONObject(i);

                        String id = jsonObject.getString("id");
                        String originalTitle = jsonObject.getString("original_title");
                        String releaseDate = jsonObject.getString("release_date");
                        String rating = jsonObject.getString("vote_average");
                        String urlThumbnail = urlJsonImg + jsonObject.getString("poster_path");

                        //jsonResponse = "";
                        jsonResponse += "ID: " + id + "\n\n";
                        jsonResponse += "Title: " + originalTitle + "\n\n";
                        jsonResponse += "Release Date: " + releaseDate + "\n\n";
                        jsonResponse += "Rating: " + rating + "\n\n";
                
                    //Toast.makeText(getActivity(),"Response = "+jsonResponse,Toast.LENGTH_LONG).show();
                    parseResult(response);

                catch (JSONException e) 
                    e.printStackTrace();
                    Toast.makeText(getActivity(),"Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                
                hidepDialog();
            
        , new Response.ErrorListener() 

            @Override
            public void onErrorResponse(VolleyError error) 
                //VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getActivity(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                // hide the progress dialog
                hidepDialog();
            
        );

        // Adding request to request queue
        VolleySingleton.getInstance(getActivity()).addToRequestQueue(jsonObjectRequest);
    


    private void showpDialog() 
        if (!pDialog.isShowing())
            pDialog.show();
    

    private void hidepDialog() 
        if (pDialog.isShowing())
            pDialog.dismiss();
    

    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
        super.onActivityCreated(savedInstanceState);

        //txtResponse = (TextView) getActivity().findViewById(R.id.txtResponse);
        //makeJsonArrayRequest();

        // Calling the RecyclerView
        mRecyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view_movie);
        mRecyclerView.setHasFixedSize(true);

        // The number of Columns
        mLayoutManager = new GridLayoutManager(getActivity(),2);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new MovieAdapter(getActivity(),movies);
        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), mRecyclerView, new ClickListener() 
            @Override
            public void onMovieClick(View view, int position) 
                Toast.makeText(getActivity(), "Kepencet " + position, Toast.LENGTH_SHORT).show();

                **//what to do here?**
            

            @Override
            public void onMovieLongClick(View view, int position) 
                Toast.makeText(getActivity(),"Kepencet Lama "+position,Toast.LENGTH_LONG).show();
            
        ));

        makeJsonObjectRequest();

        if (savedInstanceState!=null)
            movies=savedInstanceState.getParcelableArrayList(STATE_MOVIES);
            mAdapter.notifyDataSetChanged();
        
    



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.tab_1, container, false);


        return view;
    

    private void parseResult(JSONObject response) 
        try 
            JSONArray arrayMovies = response.getJSONArray("results");


            if (movies == null) 
                movies = new ArrayList<Movies>();
            



            for (int i = 0; i < arrayMovies.length(); i++) 

                JSONObject currentMovies = arrayMovies.getJSONObject(i);

                Movies item = new Movies();
                item.setTitle(currentMovies.optString("original_title"));
                item.setRating(currentMovies.optString("vote_average"));
                item.setReleaseDate(currentMovies.optString("release_date"));
                item.setId(currentMovies.optString("id"));
                item.setUrlThumbnail(urlJsonImg+currentMovies.optString("poster_path"));
                movies.add(item);
                mAdapter.notifyDataSetChanged();

                //Toast.makeText(getActivity(),"Movie : "+movies,Toast.LENGTH_LONG).show();
            
         catch (JSONException e) 
            e.printStackTrace();
        
    

    class RecyclerTouchListener implements RecyclerView.OnItemTouchListener
        private GestureDetector mGestureDetector;
        private ClickListener mClickListener;


        public RecyclerTouchListener(final Context context, final RecyclerView recyclerView, final ClickListener clickListener) 
            this.mClickListener = clickListener;
            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 && clickListener!=null)
                        clickListener.onMovieLongClick(child,recyclerView.getChildAdapterPosition(child));
                    
                    super.onLongPress(e);
                
            );
        

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) 
            View child = rv.findChildViewUnder(e.getX(), e.getY());
            if (child!=null && mClickListener!=null && mGestureDetector.onTouchEvent(e))
                mClickListener.onMovieClick(child,rv.getChildAdapterPosition(child));
            
            return false;
        

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) 

        

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) 

        
    

    public static interface ClickListener
        public void onMovieClick(View view, int position);
        public void onMovieLongClick(View view, int position);
    


任何帮助将不胜感激。谢谢!

【问题讨论】:

【参考方案1】:

您需要将其他两个值添加到意图中的捆绑包中。所以是这样的:

Intent intent = new Intent(getActivity(), YourNextActivity.class);
intent.putExtra("movie_id_key", movies.get(position).getId); //you can name the keys whatever you like
intent.putExtra("movie_rating_key", movies.get(position).getRating); //note that all these values have to be primitive (i.e boolean, int, double, String, etc.)
intent.putExtra("movie_release_date_key", movies.get(position).getReleaseDate);
startActivity(intent)

在您的新活动中,只需执行以下操作即可检索:

 String id = getIntent().getExtras().getString("movie_id_key");

【讨论】:

但我将解析后的数据直接存储到 RecyclerView,查看私有 void parseResult(JSONObject 响应)。如何从那里获取项目数据? 电影项目是否显示在您的回收站视图中? 是的,他们出现了。我已经找到了解决方案。只需将代码中的“id”更改为 movies.get(position).getId();谢谢【参考方案2】:

将它们作为额外内容添加到您开始活动的意图中:

Intent intent = new Intent(currentActivity, targetActivity);
// Sree was right in his answer, it's putExtra, not putStringExtra. =(
intent.putExtra("title", title); 
// repeat for ID, Rating, ReleaseDate, urlPoster
startActivity(intent);

然后在另一个活动的 onCreate 中将它们拉出来

Intent startingIntent = getIntent();
String title = startingIntent.getStringExtra("title"); // or whatever.

回应以下评论: 我不是 100% 确定你是如何实现它的,但看起来你有传递位置和视图引用的 onclick 处理程序,对吗?随心所欲地调整它,但基本上......:

public void onClick(View v, int position)
    Movie m = movies.get(position);
    Intent intent = new Intent(v.getContext(), AnotherActivity.class);
    intent.putExtra("my_movie_foo_key", m.getFoo());
    intent.putExtra("my_movie_bar_key", m.getBar());
    // etc.
    v.getContext().startActivity(intent);

只要你有一个有效的上下文引用(并且他们不能在没有有效上下文的情况下点击视图),你就可以从任何你喜欢的地方开始一个活动。

【讨论】:

我把解析后的数据直接存到RecyclerView,看private void parseResult(JSONObject response)。如何从那里获取每个项目的“id”、“title”? 我在上面编辑过。只要你有一个有效的上下文,你就可以从任何你想要的地方开始一个活动(你可以从 onclick 中传递的视图中获取它)。

以上是关于如何将价值从 recyclerview 项目传递到另一个活动的主要内容,如果未能解决你的问题,请参考以下文章

将数据从 RecyclerView.Adapter 传递到片段 onClick

使用 Serializable - AndroidX 将数据从 RecyclerView 传递到 RecyclerViewMore

如何从 recyclerview 片段传递到另一个 recyclerview 片段

如何将 Web 服务数据传递到 Android 中的 RecyclerView?

为啥我不能将数据从 RecyclerView 传递到另一个 RecyclerView - AndroidX

如何将 ItemDecoration 从左侧和右侧都应用到 RecyclerView 项目?