简体   繁体   中英

Passing data RecyclerView data to another activity on click

I am currently in the process of passing information from my recyclerview to another activity called "partyInformation", in respective to the clicked cell. The information in my recyclerView is fetched from parse.com. My recyclerview consists of textviews as well as imageViews and I would like to populate my "partyInfomation" with this data. When I compile and run I receive no errors but when a cell is clicked from my recyclerview my app quits and reads "Unfortunately, app has stopped". The logcat prompts the error: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.os.Parcelable.

Can you please review my code and help identify where I went wrong in the process of trying to pass data on click?

Main activity

package com.example.jbobo_000.prac;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;


import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.ArrayList;
import java.util.List;

public class HomeScreen extends AppCompatActivity {
    private RecyclerView mRecyclerDrawer;
    private customAdapter mAdapter;
    private Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);
        setupDrawer();
        context=this;
        final List<Information> data = new ArrayList<>();
        Parse.initialize(this, "xxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxx");
        final ParseQuery<ParseObject> query = ParseQuery.getQuery("flyerDataFetch");
        query.orderByDescending("createdAt");

        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> results, com.parse.ParseException e) {
                for (ParseObject a : results) {
                List<ParseObject> dataHolder = null;
                try {
                    dataHolder = query.find();

                if (e == null) {
                        Information information = new Information();
                        information.partyName = a.getString("partyName");
                        information.partyPromoterName = a.getString("partyPromoterName");
                        information.partyCost = a.getString("partyCost");

                        ParseFile bum1 = (ParseFile) a.get("partyFlyerImage");
                        ParseFile bum2 = (ParseFile) a.get("partyPromoterImage");
                        byte[] file1 = bum1.getData();
                        byte[] file2 = bum2.getData();
                        information.flyerPic = BitmapFactory.decodeByteArray(file1, 0, file1.length);
                        information.partyPromoterPic = BitmapFactory.decodeByteArray(file2,0,file2.length);

                        data.add(information);
                }
                else {
                    // something went wrong
                }
                } catch (ParseException e1) {
                    e1.printStackTrace();
                }}
                mRecyclerDrawer = (RecyclerView) findViewById(R.id.drawerList);
                mRecyclerDrawer.setLayoutManager(new LinearLayoutManager(context));
                mAdapter = new customAdapter(context, data);
                mRecyclerDrawer.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();
            }

        });
    }

    private void setupDrawer() {
        Toolbar mToolbar;
        NavigationDrawer mDrawerFragment;
        mToolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //setup the NavigationDrawer

        mDrawerFragment = (NavigationDrawer)
                getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        mDrawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
}

CustomAdapter

package com.example.jbobo_000.prac;



import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Parcelable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.parse.ParseImageView;

import java.util.Collections;
import java.util.List;

public class customAdapter extends RecyclerView.Adapter<customAdapter.MyViewHolder> {
    List<Information> data = Collections.emptyList();
    private LayoutInflater inflater;
    private Context context;
    public customAdapter (Context context,List<Information>data){
        this.context=context;
        inflater=LayoutInflater.from(context);
        this.data= data;
    }

   @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
       View view= inflater.inflate(R.layout.custom_row, parent, false);
       MyViewHolder holder = new MyViewHolder(view);
       return holder;
   }

    @Override
    public void onBindViewHolder(MyViewHolder holder,int position){
        Information current= data.get(position);
        holder.promoterNameText.setText(current.partyPromoterName);
        holder.costText.setText(current.partyCost);
        holder.partyNameText.setText(current.partyName);
        holder.flyerImage.setImageBitmap(current.flyerPic);
        holder.promoterImage.setImageBitmap(current.partyPromoterPic);
    }

    @Override
    public int getItemCount(){

        return data.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView promoterNameText;
        TextView costText;
        TextView partyNameText;
        ImageView flyerImage;
        ImageView promoterImage;

        public MyViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            promoterImage = (ImageView) itemView.findViewById(R.id.promoterPicImage);
            flyerImage = (ImageView) itemView.findViewById(R.id.flyertImage);
            costText = (TextView) itemView.findViewById(R.id.costText);
            promoterNameText = (TextView) itemView.findViewById(R.id.promoterNameText);
            partyNameText = (TextView) itemView.findViewById(R.id.partyNameText);
        }

        @Override
        public void onClick(View v) {
            //context.startActivity(new Intent(context, partyInformation.class));
            int postion = getAdapterPosition();
            Intent i = new Intent(context, partyInformation.class);
            i.putExtra("partyName", (Parcelable) partyNameText);
            i.putExtra("promoterNameText", (android.os.Parcelable) promoterNameText);
            i.putExtra("costText", (android.os.Parcelable) costText);
            i.putExtra("flyerImage", (android.os.Parcelable) flyerImage);
            i.putExtra("promoterImage", (android.os.Parcelable) promoterImage);
            context.startActivity(i);

        }
    }
}

partyInformation

package com.example.jbobo_000.prac;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class partyInformation extends ActionBarActivity implements View.OnClickListener{
Button checkout;
    TextView partyName1;
    ImageView informationFlyer1;
    ImageView promoterPic1;

    String partyName= getIntent().getStringExtra("partyName");
    String promoterNameText= getIntent().getStringExtra("promoterNameText");
    String costText= getIntent().getStringExtra("costText");
    String flyerImage= getIntent().getStringExtra("flyerImage");
    String promoterImage= getIntent().getStringExtra("promoterImage");


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_party_information);
        partyName1 = (TextView) findViewById(R.id.textView2);
        informationFlyer1 = (ImageView) findViewById(R.id.informationFlyer);
        promoterPic1 = (ImageView) findViewById(R.id.imageView);


        partyName1.setText(partyName);
        //informationFlyer1.setImageResource(flyerImage);
       //promoterPic1.setImageResource(promoterImage);




             checkout = (Button) findViewById(R.id.requestTicketBtn);
        checkout.setOnClickListener(this);

    }

    @Override
    public void onClick(View v){
        switch(v.getId()){
            case R.id.requestTicketBtn:
                startActivity(new Intent(this, Checkout.class));}}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_party_information, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

LogCat

06-18 17:27:28.505  28047-28047/? I/art﹕ Late-enabling -Xcheck:jni
06-18 17:27:28.785  28047-28047/com.example.jbobo_000.prac D/CrashReporting﹕ Crash reporting is enabled for com.example.jbobo_000.prac, initializing...
06-18 17:27:28.797  28047-28047/com.example.jbobo_000.prac D/CrashReporting﹕ Looking for error files in /data/data/com.example.jbobo_000.prac/files/com.parse/cr/reports
06-18 17:27:28.797  28047-28047/com.example.jbobo_000.prac D/CrashReporting﹕ Looking for error files in /data/data/com.example.jbobo_000.prac/files/com.parse/cr/minidumps
06-18 17:27:29.051  28047-28075/com.example.jbobo_000.prac D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-18 17:27:29.057  28047-28047/com.example.jbobo_000.prac D/﹕ HostConnection::get() New Host Connection established 0xb42ccb80, tid 28047
06-18 17:27:29.068  28047-28047/com.example.jbobo_000.prac D/Atlas﹕ Validating map...
06-18 17:27:29.125  28047-28075/com.example.jbobo_000.prac D/libEGL﹕ loaded /system/lib/egl/libEGL_emulation.so
06-18 17:27:29.127  28047-28075/com.example.jbobo_000.prac D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_emulation.so
06-18 17:27:29.149  28047-28075/com.example.jbobo_000.prac D/libEGL﹕ loaded /system/lib/egl/libGLESv2_emulation.so
06-18 17:27:29.176  28047-28075/com.example.jbobo_000.prac D/﹕ HostConnection::get() New Host Connection established 0xaf0395c0, tid 28075
06-18 17:27:29.202  28047-28075/com.example.jbobo_000.prac I/OpenGLRenderer﹕ Initialized EGL, version 1.4
06-18 17:27:29.316  28047-28075/com.example.jbobo_000.prac D/OpenGLRenderer﹕ Enabling debug mode 0
06-18 17:27:29.354  28047-28075/com.example.jbobo_000.prac W/EGL_emulation﹕ eglSurfaceAttrib not implemented
06-18 17:27:29.354  28047-28075/com.example.jbobo_000.prac W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xaf035b20, error=EGL_SUCCESS
06-18 17:27:30.025  28047-28047/com.example.jbobo_000.prac I/Choreographer﹕ Skipped 37 frames!  The application may be doing too much work on its main thread.
06-18 17:27:46.016  28047-28047/com.example.jbobo_000.prac I/ViewUtils﹕ app:theme is now deprecated. Please move to using android:theme instead.
06-18 17:27:46.371  28047-28075/com.example.jbobo_000.prac W/EGL_emulation﹕ eglSurfaceAttrib not implemented
06-18 17:27:46.371  28047-28075/com.example.jbobo_000.prac W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb431d440, error=EGL_SUCCESS
06-18 17:27:46.409  28047-28047/com.example.jbobo_000.prac E/RecyclerView﹕ No adapter attached; skipping layout
06-18 17:27:46.700  28047-28047/com.example.jbobo_000.prac E/RecyclerView﹕ No adapter attached; skipping layout
06-18 17:27:47.697  28047-28056/com.example.jbobo_000.prac W/art﹕ Suspending all threads took: 12.419ms
06-18 17:27:49.209  28047-28047/com.example.jbobo_000.prac D/AndroidRuntime﹕ Shutting down VM
06-18 17:27:49.209  28047-28047/com.example.jbobo_000.prac E/CrashReporting﹕ ParseCrashReporting caught a ClassCastException exception for com.example.jbobo_000.prac. Building report.
06-18 17:27:49.210  28047-28047/com.example.jbobo_000.prac E/CrashReporting﹕ Handling exception for crash
    java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.os.Parcelable
            at com.example.jbobo_000.prac.customAdapter$MyViewHolder.onClick(customAdapter.java:77)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-18 17:27:49.213  28047-28047/com.example.jbobo_000.prac D/CrashReporting﹕ Generating report file for crash
06-18 17:27:49.284  28047-28047/com.example.jbobo_000.prac D/CrashReporting﹕ Retrieving logcat output...
06-18 17:27:49.312  28047-28047/com.example.jbobo_000.prac D/CrashReporting﹕ Retrieving logcat output...
06-18 17:27:49.329  28047-28047/com.example.jbobo_000.prac D/CrashReporting﹕ Retrieving logcat output...
06-18 17:27:49.388  28047-28047/com.example.jbobo_000.prac W/Settings﹕ Setting stay_on_always has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
06-18 17:27:49.529  28047-28047/com.example.jbobo_000.prac V/CrashReporting﹕ About to start ReportSenderWorker from #handleException
06-18 17:27:49.538  28047-28251/com.example.jbobo_000.prac D/CrashReporting﹕ #checkAndSendReports - start
06-18 17:27:49.538  28047-28251/com.example.jbobo_000.prac D/CrashReporting﹕ Looking for error files in /data/data/com.example.jbobo_000.prac/files/com.parse/cr/reports
06-18 17:27:49.540  28047-28251/com.example.jbobo_000.prac D/CrashReporting﹕ Loading file 1434662869213-ClassCastException-1.stacktrace
06-18 17:27:49.547  28047-28251/com.example.jbobo_000.prac I/CrashReporting﹕ Sending file 1434662869213-ClassCastException-1.stacktrace
06-18 17:27:49.548  28047-28251/com.example.jbobo_000.prac D/CrashReporting﹕ Sending crash report to Parse...
06-18 17:27:49.556  28047-28251/com.example.jbobo_000.prac D/CrashReporting﹕ #checkAndSendReports - finish
06-18 17:27:49.631  28047-28047/com.example.jbobo_000.prac E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.jbobo_000.prac, PID: 28047
    java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.os.Parcelable
            at com.example.jbobo_000.prac.customAdapter$MyViewHolder.onClick(customAdapter.java:77)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Change this:

i.putExtra("partyName", (Parcelable) partyNameText);
i.putExtra("promoterNameText", (android.os.Parcelable) promoterNameText);
i.putExtra("costText", (android.os.Parcelable) costText)

To this:

i.putExtra("partyName", partyNameText.getText().toString());
i.putExtra("promoterNameText", promoterNameText.getText().toString()));
i.putExtra("costText", costText.getText().toString()))

This adds data to intent 'i' as Strings. You can't cast from a TextView to a Parcelable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM