list.addAll(paireDevice);
Intent i= new Intent(bluetooth.this, ShowPairedDevice.class);
i.getParcelableArrayListExtra("Device.list",(ArrayList<?extends Parcelable>)list);
startActivity(i);
try this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> list = new ArrayList<>();
list.add("Device1");
list.add("Device2");
list.add("Device3");
Intent i = new Intent(this, NewActivity.class);
i.putExtra("Device.list", list);
startActivity(i);
}
}
public class NewActivity extends AppCompatActivity {
private String TAG = NewActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
ArrayList<String> list = (ArrayList<String>) getIntent().getSerializableExtra("Device.list");
for (String device: list){
Log.i(TAG, "Devices: " + device);
}
}
}
now it's work with the was in getParcelableArrayListExtra instead ofputParcelableArrayListExtra
list.addAll(paireDevice); Intent i= new Intent(bluetooth.this, ShowPairedDevice.class); i.putParcelableArrayListExtra("Device.list", (ArrayList) list); startActivity(i);
Related
I have 3 activities, activity 1 is fetching data from firebase, activity 2 is splash screen and activity 3 is show the data. I want to pass the data from activity 1 to activity 3 using intent. but it doesnt work. If i dont use activity 2 it works, but i want to use activity 2 as well.
Activity 1 fetching data from firebase
matchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDbref = FirebaseDatabase.getInstance().getReference();
mkomref = mDbref.child("Students");
string = matchKriteria.getText().toString();
Query query = mkomp.orderByChild("Komp").equalTo(string);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
if(nameList.size()>0)
nameList.clear();
for (DataSnapshot data : dataSnapshot.getChildren()) {
nameList.add(data.child("Name").getValue(String.class));
}
Intent intent = new Intent(FirstActivity.this, LoadingActivity.class);
intent.putExtra("value",nameList);
startActivity(intent);
}
}
Activity 2 splash screen
public class LoadingActivity extends AppCompatActivity {
public static int SPLASH_TIME_OUT= 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent =new Intent(LoadingActivity.this, ResultActivity.class);
startActivity(intent);
finish();
}
},SPLASH_TIME_OUT);
Activity 3 show the result
public class ResultActivity extends AppCompatActivity {
private ArrayList<String> nameList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bedrift_match_resultat);
Intent intent = getIntent();
if (intent != null)
nameList = intent.getStringArrayListExtra("value");
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
if (nameList.size() > 0) {
CustomAdapter adapter = new CustomAdapter(this, nameList);
recyclerView.setAdapter(adapter);
}
}
}
i think it didn't work because, in your LoadingActivity you didn’t retrieve data from FirstActivity and you’re not sending it to ResultActivity. So, your namelist in ResultActivity is null
You can try this:
public class LoadingActivity extends AppCompatActivity {
public static int SPLASH_TIME_OUT= 5000;
private ArrayList<String> nameList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading);
if (getIntent() != null) {
namelist.addAll(getIntent().getStringArrayListExtra("value");
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent =new Intent(LoadingActivity.this, ResultActivity.class);
intent.putExtra("value",nameList);
startActivity(intent);
finish();
}
},SPLASH_TIME_OUT);
I am trying to send data through an activity to another but there is a variable error that I didn't understand, please Help. Thank you very much for your time and assistance in this matter.
public class MainActivity extends AppCompatActivity {
EditText Message_Text;
public final static String MESSAGE_KEY="com.example.zeeshan.userinterface.message_key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View views) {
Message_Text= (EditText) findViewById(R.id.Message_Text);
String message=Message_Text.getText().toString();
Intent intent= new Intent(this, SecondActivity.class);
intent.putExtra(MESSAGE_KEY,message);
startActivity(intent);
}
}
the second activity code is:
public class SecondActivity extends AppCompatActivity {
public final static String MESSAGE_KEY="com.example.zeeshan.userinterface.message_key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
String message = intent.getStringExtra(MESSAGE_KEY);
TextView textView = new TextView(this);
textView.setTextSize(35);
// setContentView(R.layout.second_layout);
}
}
you have to write like below.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendMessage();
}
public void sendMessage() {
EditText Message_Text= (EditText) findViewById(R.id.Message_Text);
String message=Message_Text.getText().toString();
Intent intent= new Intent(this, SecondActivity.class);
intent.putExtra("MESSAGE_KEY",message);
startActivity(intent);
}
I am new to learning Array in android studio. Please show me some examples in details. I have write an example here and I want to display the Array data from MainActivity into second_page activity .
MainActivity.java
public class MainActivity extends AppCompatActivity {
String my_array[]={"dog","cat","tiger"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void next_page(View view){
Intent intent = new Intent(this,second_page.class);
intent.putExtra("my_array_next", my_array);
startActivity(intent);
}
}
second_page.java
public class second_page extends MainActivity {
TextView get_data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_page);
get_data=(TextView)findViewById(R.id.tv);
Intent intent=getIntent();
// coding here to display the array data
// sth like abc.setText(display_array_data);
}
Please advice. Thank you!
If you are trying to send a String-array from one Activity to another this can be done in the Intent.
In ClassA:
Intent intent = new Intent(this, ClassB);
String[] my_array = new String[]{"dog","cat","tiger"};
intent.putExtra("myArr", my_array);
startActivity(intent);
In ClassB:
public void onCreate() {
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("myArr");
}
this may helps you
In second_page.java, receive the array you pass via Intent and set it to your TextView like this
#Override
protected void onCreate(Bundle savedInstanceState) {
...
String[] array = intent.getStringArrayExtra("my_array_next");
// TextView display a String so you should convert your Array to String
String str1 = Arrays.toString(array);
get_data.setText(str1);
}
First take the array:
Intent intent = getIntent();
List array;
if (intent.getExtras() != null) {
array= intent.getExtras().getBoolean("my_array_next");
}
Then print
get_data.setText(array.toString());
Sending Class:
Intent intent = new Intent(this, ClassB);
String[] myStrings = new String[] {"test", "test2"};
intent.putExtra("strings", myStrings);
startActivity(intent);
Reciving Class:
public void onCreate() {
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("strings");
}
In your Second activity:
String[] array=getIntent().getStringArrayExtra("my_array_next");
I think you need to go through basics, go to https://developer.android.com/index.html to get started.
I have a grand parent activity called Department
public class Department extends AppCompatActivity {
RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_department);
.........
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, DeptDetail.class);
Bundle extra = new Bundle();
extra.putString("Department", getAdapterPosition()+"");
intent.putExtras(extra);
context.startActivity(intent);
}
});
}
}
Sends data about Department position to DepartmentDeatail activity
public class DeptDetail extends AppCompatActivity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dept_detail);
Bundle extra = getIntent().getExtras();
deptpos = Integer.parseInt(extra.getString("Department"));
.........
public void onClick(View v) {
int id= v.getId();
Intent in;
Bundle extras = new Bundle();
in = new Intent(DeptDetail.this, Mission.class);
extras.putString("Mission",mission[deptpos]);
extras.putString("Deptid", deptpos+"");
in.putExtras(extras);
startActivityForResult(in,1);
}
}
and DeptDetail activity sends same Deptpos to its child activity Mission
public class Mission extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mission);
Bundle extra = getIntent().getExtras();
String mission = extra.getString("Mission");
deptid=Integer.parseInt(extra.getString("Deptid"));
TextView txtmission = (TextView)findViewById(R.id.txtmission);
try {
txtmission.setText(mission);
}
catch (NullPointerException e)
{
txtmission.setText("");
}
}
}
And now I want same Deptid to be accessed in DeptDetail activity which always calls for intent from Department activity, which is not available as usual..
So please show me the way to pass the data to child and back to parent.
I tried
onActivityResult(..)
but it wasn't called before onCreate where extra is read and generating NullPointerException
I am trying to post in an ArrayList of strings from one activity to another when the option menu is pressed. However, when I try to getIntent() in the next page, it returns null value.
Option menu when item is selected code:
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==2){
ArrayList<String>data1=new ArrayList<>();
data1.add("Hello");
data1.add("bye");
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("dataList",data1);
startActivity(i);
}
return true;
}
Retrieve Data Activity :
public class SecondActivity extends AppCompatActivity {
ListView lListView;
ArrayAdapter<String> adapter;
Intent intent = getIntent();
ArrayList<String> data = intent.getStringArrayListExtra("dataList");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
lListView = (ListView)findViewById(R.id.lListView);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
lListView.setAdapter(adapter);
}
}
Intent intent = getIntent();
ArrayList<String> data = intent.getStringArrayListExtra("dataList");
add this lines in oncreate
Move this code into your onCreate method:
Intent intent = getIntent();
ArrayList<String> data = intent.getStringArrayListExtra("dataList");
Intent does not exist at the point that you try to get it, it is being passed later in your activity's lifecycle. That is why you are getting null.
Check changes i have made in your code.. getIntent() should be called
inside oncreate...
public class SecondActivity extends AppCompatActivity {
ListView lListView;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//changes
Intent intent = getIntent();
ArrayList<String> data =
intent.getExtras().getStringArrayListExtra("dataList");
//changes
lListView = (ListView)findViewById(R.id.lListView);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
lListView.setAdapter(adapter);
}
}
At first set
i.putStringArrayListExtra("dataList",data1);
Then call in Oncreate() section ;
Intent intent = getIntent();
ArrayList<String> data = intent.getStringArrayListExtra("dataList");
Finally
public class SecondActivity extends AppCompatActivity {
ListView lListView;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
ArrayList<String> data = intent.getStringArrayListExtra("dataList");
lListView = (ListView)findViewById(R.id.lListView);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
lListView.setAdapter(adapter);
}
}
You should use i.putStringArrayListExtra("dataList", data1); to set the ArrayList in your code
Try in the other Activity in onCreate():
ArrayList<String> data = (ArrayList<String>) getIntent().getSerializableExtra("dataList");
or
ArrayList<String> data = (ArrayList<String>) getIntent().getParcelableExtra("dataList");