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);
}
}