0

I have two different activities and those two activity leads to one activity and i'm setting text of result activity based on intent received however when i run the app i get intent from first activity only i searched on google for same but couldnt understand where i'm going wrong. I think there is some mistake in condition but dont where so please if someone can guide me here. My ResultActivity code

Bundle extras = getIntent().getExtras();
        if(extras != null) {
            if(extras.containsKey("title")) {
               // title_text = getIntent().getStringExtra("");
                title_text = extras.getString("title");
                toolbar_title.setText(title_text);
            }
        }
        else{

            title_text2 = getIntent().getStringExtra("title2");
            //title_text2 = extras.getString("title2");
            toolbar_title.setText(title_text2);
        }

FirstActivity.class

public void startMoreDetailActivity(){
        Intent startintent = new Intent(this, Main_Page_Details.class);
        startintent.putExtra("title",title_text);
        startActivity(startintent);
    }

SecondActivity(In recyclerview)

public void startMoreDetailActivity(){
            Intent startintent = new Intent(context, Main_Page_Details.class);
            int position  = getAdapterPosition();
            startintent.putExtra("title2",title_text);
            context.startActivity(startintent);
        }

My AdapterClass

    public class SnapAdapter extends RecyclerView.Adapter<SnapAdapter.ViewHolder> implements GravitySnapHelper.SnapListener {

    public static final int VERTICAL = 0;
    public static final int HORIZONTAL = 1;
    private Context context;
    private ArrayList<Snap> mSnaps;
    // Disable touch detection for parent recyclerView if we use vertical nested recyclerViews
    private View.OnTouchListener mTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    };

    public SnapAdapter(Context context) {
        mSnaps = new ArrayList<>();
        this.context = context;
    }

    public void addSnap(Snap snap) {
        mSnaps.add(snap);
    }

    @Override
    public int getItemViewType(int position) {
        Snap snap = mSnaps.get(position);
        switch (snap.getGravity()) {
            case Gravity.CENTER_VERTICAL:
                return VERTICAL;
            case Gravity.CENTER_HORIZONTAL:
                return HORIZONTAL;
            case Gravity.START:
                return HORIZONTAL;
            case Gravity.TOP:
                return VERTICAL;
            case Gravity.END:
                return HORIZONTAL;
            case Gravity.BOTTOM:
                return VERTICAL;
        }
        return HORIZONTAL;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = viewType == VERTICAL ? LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_snap_vertical, parent, false)
                : LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_snap, parent, false);

        if (viewType == VERTICAL) {
            view.findViewById(R.id.recyclerView).setOnTouchListener(mTouchListener);
        }

        return new ViewHolder(view,context);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Snap snap = mSnaps.get(position);
        holder.snapTextView.setText(snap.getText());

        if (snap.getGravity() == Gravity.START || snap.getGravity() == Gravity.END) {
            holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder
                    .recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
            holder.recyclerView.setOnFlingListener(null);
            new GravitySnapHelper(snap.getGravity(), false, this).attachToRecyclerView(holder.recyclerView);
        } else if (snap.getGravity() == Gravity.CENTER_HORIZONTAL) {
            holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder
                    .recyclerView.getContext(), snap.getGravity() == Gravity.CENTER_HORIZONTAL ?
                    LinearLayoutManager.HORIZONTAL : LinearLayoutManager.VERTICAL, false));
            holder.recyclerView.setOnFlingListener(null);
            new LinearSnapHelper().attachToRecyclerView(holder.recyclerView);
        } else if (snap.getGravity() == Gravity.CENTER) { // Pager snap
            holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder
                    .recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
            holder.recyclerView.setOnFlingListener(null);
            new PagerSnapHelper().attachToRecyclerView(holder.recyclerView);
        } else { // Top / Bottom
            holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder
                    .recyclerView.getContext()));
            holder.recyclerView.setOnFlingListener(null);
            new GravitySnapHelper(snap.getGravity()).attachToRecyclerView(holder.recyclerView);
        }


        holder.recyclerView.setAdapter(new Adapter(snap.getGravity() == Gravity.START
                || snap.getGravity() == Gravity.END
                || snap.getGravity() == Gravity.CENTER_HORIZONTAL,
                snap.getGravity() == Gravity.CENTER, snap.getApps()));
    }

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

    @Override
    public void onSnap(int position) {
        Log.d("Snapped: ", position + "");
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        public static CustomTextViewMedium snapTextView;
        public RecyclerView recyclerView;
        public ImageView see_more_image2;
        public Context context;
        String title_text;
        public ViewHolder(View itemView, Context context) {

            super(itemView);
            this.context = context;
            snapTextView = (CustomTextViewMedium) itemView.findViewById(R.id.snapTextView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView);
            see_more_image2 = (ImageView)itemView.findViewById(R.id.see_more_image2);
            title_text = snapTextView.getText().toString();
            itemView.setOnClickListener(this);

            see_more_image2.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.see_more_image2:
                startMoreDetailActivity();
            }
        }

        public void startMoreDetailActivity(){
            Intent startintent = new Intent(context, Main_Page_Details.class);
            int position  = getAdapterPosition();
            startintent.putExtra("title",title_text);
            context.startActivity(startintent);
        }
    }
}
cooldev
  • 57
  • 9

2 Answers2

0

You are trying to get title2 only when the intent is null, that's why you're not getting it!

Change it to the following:

Bundle extras = getIntent().getExtras();
    if(extras != null) {
        if(extras.containsKey("title")) {
           // title_text = getIntent().getStringExtra("");
            title_text = extras.getString("title");
            toolbar_title.setText(title_text);
        } else  if(extras.containsKey("title2"){

        title_text2 = getIntent().getStringExtra("title2");
        //title_text2 = extras.getString("title2");
        toolbar_title.setText(title_text2);
        }
    }

if this doesn't solve your problem, then if you can show how are you passing the intent that would be helpful to understand your problem more.

Parag Pawar
  • 827
  • 3
  • 12
  • 23
0

Dude you are doing it all wrong..

there is no need to pass values with two different keys. keep them both same.

First Activity

public void startMoreDetailActivity(){
    Intent startintent = new Intent(this, Main_Page_Details.class);
    startintent.putExtra("title",title_text);
    startActivity(startintent);
}

Second Activity

public void startMoreDetailActivity(){
    Intent startintent = new Intent(context, Main_Page_Details.class);
    int position  = getAdapterPosition();
    startintent.putExtra("title",title_text);
    context.startActivity(startintent);
}

and in your result Activity..

title_text = extras.getString("title");
toolbar_title.setText(title_text);

if you still want to do it your way...

if(getIntent().hasExtra("title")) {
    title_text = getIntent().getStringExtra("title");
    toolbar_title.setText(title_text);
} else {
    title_text2 = getIntent().getStringExtra("title2");
    toolbar_title.setText(title_text2);
}

EDIT :

you can get postion value by doing following.

public void startMoreDetailActivity(){
    Intent startintent = new Intent(context, Main_Page_Details.class);
    title_text = mSnaps.get(getAdapterPosition()).getText();
    startintent.putExtra("title",title_text);
    context.startActivity(startintent);
}
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
  • Ok but can you tell me how i can send adapter position for textview from recyclerview – cooldev May 28 '18 at 10:45
  • post your adapter – V-rund Puro-hit May 28 '18 at 10:47
  • buddy that is not how it works.. there must me array list from which you are setting value to the textview please post full adapter.. – V-rund Puro-hit May 28 '18 at 11:18
  • hi please see i've added the complete adapter class – cooldev May 28 '18 at 11:29
  • Hi bro could you please help me with pagersnap helper – cooldev May 28 '18 at 13:33
  • ofc.. what is it? – V-rund Puro-hit May 30 '18 at 19:19
  • Actually i made a carousel view using recyclerview using this SO question https://stackoverflow.com/questions/41307578/recycler-view-resizing-item-view-while-scrolling-for-carousel-like-effect?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa but now i'm trying to automatically center scrolled item to center for that i used pagersnap helper using this SO question https://stackoverflow.com/questions/29134094/recyclerview-horizontal-scroll-snap-in-center/42128379?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – cooldev May 31 '18 at 06:05
  • now the problem is it doesnt position the last and first item in recyclerview and i'm having hard timr understanding where i'm going wrong – cooldev May 31 '18 at 06:06