0

I'm trying to pass a value from an ArrayList in one Activity (LensActivity) to a TextView on my MainActivity. On this site i found the Intent method and was experimenting with that, but seem unable to pass it, the info is getting fetched in the String lensString, and passed to the Intent, but in Main Activity seems to not be passing or getting on the TextView, and in some experiments, since the getIntent is on MainActivity, i got a null pointer.

Here is the code for the LensActivity which has the Button that send the info.

    package com.komorebiestudio.cam_report_funcionality;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class LensActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private LensAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private TextView LensChange;
    private String lensString;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lens_activity);

        Intent lensIntent = new Intent(LensActivity.this,MainActivity.class);
        lensIntent.putExtra("LensIntent",lensString);


        final ArrayList <LensItem> lensList = new ArrayList<>();
        lensList.add(new LensItem(R.drawable.zeiss,"24mm","Zeiss Compact Prime"));
        lensList.add(new LensItem(R.drawable.ic_camera,"35mm","Angenieux"));
        lensList.add(new LensItem(R.drawable.cooke,"50mm","Cooke S5I"));

        mRecyclerView = findViewById(R.id.lens_list);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new LensAdapter(lensList);
        LensChange = findViewById(R.id.lensinfo);

        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);

        final Intent ChangeLens = new Intent(this, MainActivity.class);

        mAdapter.setOnItemClickListener(new LensAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
               //Crea String con la informacion de posicion y texto del lente
                String lensPosition = lensList.get(position).getLens();
                lensString = lensPosition;
                Toast.makeText(getApplicationContext(),"this is " + lensString , Toast.LENGTH_SHORT).show();
                startActivity(new Intent(LensActivity.this,MainActivity.class));
            }
        });

    }




}

and here is the code of the MainActivity that receives it.

    package com.komorebiestudio.cam_report_funcionality;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements FpsDialog.FpsDialogListener{

    private TextView textViewinfo1;
    private Button button1;
    private  Button lensButton;
    private TextView lensInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        lensInfo = findViewById(R.id.lensinfo);
        lensInfo.setText(getIntent().getStringExtra("LensIntent"));
        textViewinfo1 = findViewById(R.id.info1);
        button1 = findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
                openDialog();
            }
        });
        lensButton = findViewById(R.id.lensbutton);
        lensButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,LensActivity.class));
            }
        });
    }


    public void openDialog(){
        FpsDialog fps_dialog = new FpsDialog();
        fps_dialog.show(getSupportFragmentManager(),"Fps Dialog");

    }

    @Override
    public void applyText(String fpsinfo) {
        textViewinfo1.setText(fpsinfo);

    }




}
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • 5
    Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Radesh Jan 15 '19 at 15:16
  • you are not calling startActivity(lensIntent) . You have set the value in lensIntent . But when button is clicked different intent is called.Please check in first set of code – Darshn Jan 15 '19 at 15:26

2 Answers2

0

In your LensActivity, you're creating an Intent that you never use. The Intent is this one:

Intent lensIntent = new Intent(LensActivity.this,MainActivity.class);
lensIntent.putExtra("LensIntent",lensString);

Instead, you should create it in the Item Click Listener. Just remove the code above, and modify the listener in this way:

mAdapter.setOnItemClickListener(new LensAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
           //Crea String con la informacion de posicion y texto del lente
            String lensPosition = lensList.get(position).getLens();
            lensString = lensPosition;
            Intent lensIntent = new Intent(LensActivity.this,MainActivity.class);
            lensIntent.putExtra("LensIntent",lensString);
            Toast.makeText(getApplicationContext(),"this is " + lensString , Toast.LENGTH_SHORT).show();
            startActivity(lensIntent);
        }
    });
Gregorio Palamà
  • 1,965
  • 2
  • 17
  • 22
  • i noticed that you removed the startActivity(new Intent(LensActivity.this,MainActivity.class)); part and replaced it with startActivity(lensIntent); does this call the intent using the previously assigned (LensActivity.this,MainActivity.class); ? – Diego Ortiz Jan 15 '19 at 19:47
  • Exactly. You were using an empty, newly created intent. In this way, instead, you're creating an intent just when you need it, adding the value you want to pass and starting the activity with the the intent you just created – Gregorio Palamà Jan 15 '19 at 21:01
0

You need to use the putExtra method after you assign a value to the variable lensString

mAdapter.setOnItemClickListener(new LensAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
           //Crea String con la informacion de posicion y texto del lente
            String lensPosition = lensList.get(position).getLens();
            lensString = lensPosition;
            lensIntent.putExtra("LensIntent",lensString);
            Toast.makeText(getApplicationContext(),"this is " + lensString , Toast.LENGTH_SHORT).show();
            // Notice that you are not using your previously created intent in you 
            // original code.  
            startActivity(lensIntent);
        }
    });
MIR
  • 31
  • 1
  • 2