MainActivity:
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import java.util.*;
import android.content.Intent;
public class MainActivity extends Activity {
private Spinner spinner1;
private Button btnSubmit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
// get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Get Value from EditText //Save value to a Variable
EditText person0 = findViewById(R.id.spot0); String saved_spot0 = person0.getText().toString();
EditText person1 = findViewById(R.id.spot1); String saved_spot1 = person1.getText().toString();
EditText person2 = findViewById(R.id.spot2); String saved_spot2 = person2.getText().toString();
EditText person3 = findViewById(R.id.spot3); String saved_spot3 = person3.getText().toString();
EditText person4 = findViewById(R.id.spot4); String saved_spot4 = person4.getText().toString();
EditText person5 = findViewById(R.id.spot5); String saved_spot5 = person5.getText().toString();
EditText person6 = findViewById(R.id.spot6); String saved_spot6 = person6.getText().toString();
EditText person7 = findViewById(R.id.spot7); String saved_spot7 = person7.getText().toString();
EditText person8 = findViewById(R.id.spot8); String saved_spot8 = person8.getText().toString();
EditText person9 = findViewById(R.id.spot9); String saved_spot9 = person9.getText().toString();
List<String> filled_spots = Arrays.asList(saved_spot0, saved_spot1, saved_spot2, saved_spot3, saved_spot4, saved_spot5, saved_spot6, saved_spot7, saved_spot8, saved_spot9);
Collections.shuffle(filled_spots); //Shuffle List
Collections.sort(filled_spots); //Sort List
Intent teams_Screen = new Intent (MainActivity.this, DisplayTeamsActivity.class);
teams_Screen.putStringArrayListExtra ("mylist", (ArrayList<String>) filled_spots);
startActivity(teams_Screen);
}
});
}
}
DisplayTeamsActivity:
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class DisplayTeamsActivity extends AppCompatActivity {
private ListView lv;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_teams);
ArrayList<String> stringList = getIntent().getStringArrayListExtra("mylist");
}
}
I need to pass the List array from one activity to another and populate the listView I have from the shuffeld array, but something is crashing my app when i click on the button that takes me to the next activity. I dont know if this is correctly done and what is the problem causing it to entirely crash...
you can try this
in first acticity
Intent intent = new Intent(context, NewActivity.class);
intent.putExtra("mylist", yourlist_array);
context.startActivity(intent);
in second activity
Intent intent = getIntent();
List stringArray = intent.getStringArrayExtra("mylist");
Do this:
ArrayList<String> filled_spots = new ArrayList<>(Arrays.asList("nome1", "nome2"));
Collections.shuffle(filled_spots); //Shuffle List
Collections.sort(filled_spots); //Sort List
Intent teams_Screen = new Intent (MainActivity.this, DisplayTeamsActivity.class);
teams_Screen.putStringArrayListExtra("mylist", filled_spots);
At DisplayTeamsActivity you'll need do this:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> stringList = getIntent().getStringArrayListExtra("mylist");
}
Related
I have an android studio assignment that requires us to create 3 activities, one of these activities has a spinner and text and we must pass the text and selected spinner value from the activity to another activity to show the input I figured out how to pass the text but I can't quite figure how to do the same to the spinner. this is the spinner code.
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class Homepage<adapter> extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Button btn;
EditText et;
String st;
String selectedItem="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
btn = findViewById(R.id.Btnstart);
et = findViewById(R.id.Std_Name);
Spinner spinner = findViewById(R.id.major_spinner);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Homepage.this, Input.class);
st = et.getText().toString();
i.putExtra("Value", st);
startActivity(i);
finish();
}
});
/**
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Homepage.this, Input.class);
selectedItem = spinner.getSelectedItem().toString();
intent.putExtra("selectedItem", selectedItem);
startActivity(intent);
}
});
**/
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.majors, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Move your text variable to the scope of the activity and pass in the Intent, like you did with your st variable.
Hi there i have a problem error that from NextActivity cannot go to the MainActivity3. Here is the Code NextActivity
package com.example.java;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class NextActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView= findViewById(R.id.textView);
textView.setText(message);
}
public void onBtnClick(View view){
Intent intent = getIntent();
Intent intent1= new Intent(this,MainActivity3.class);
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String message1 = intent.getStringExtra(MainActivity.EXTRA_MESSAGE1);
TextView textView= findViewById(R.id.textView);
TextView plain1= findViewById(R.id.plain1);
TextView plain2= findViewById(R.id.plain2);
String b=plain1.getText().toString();
if(b.equals(message.toString())&&plain2.getText().toString().equals(message1.toString())){
startActivity(intent1);
}
else{
textView.setText("Password or Username is Wrong");
}
}
}
and here is the code of MainActivity3 that cannot be open through NextActivity
package com.example.java;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.view.ActionProvider;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
public class MainActivity3 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent= getIntent();
String number = intent.getStringExtra(MainActivity2.extraint);
FrameLayout lay= (FrameLayout) findViewById(R.id.frames);
if(number.equals("1")) {
lay.setVisibility(View.INVISIBLE);
}
else{
}
}
public void onBtnClick (View view){
Intent intent = new Intent(this,MainActivity2.class);
startActivity(intent);
}
}
The Problem is that through the NextActivity button click the apps will restart in it's own that supposed to go to the page of MainActivity3
I have fixed it by
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent = getIntent();
String number = intent.getStringExtra(MainActivity2.extraint);
if(number != null) {
FrameLayout lay = (FrameLayout) findViewById(R.id.frames);
if (number.equals("1")) {
lay.setVisibility(View.INVISIBLE);
} else {
}
}
else{}
}
Maybe any others that have a better ways?
I have XML layout like this:
[....textedit....][addbutton]
=======list1=========
=======list2=========
=======list3=========
=======list4=========
What to do if I want to Load and Show the list onCreate from SharePreferences, be able to Add "item" to the list, and save it to SharedPreferences? Any extra simple beginner explanation are welcome since I'm a total newb.
My code below is a big mess, and I got it 100% from stitching from one example to another example that I got from anywhere.
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.widget.EditText;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.Toast;
import android.view.View;
import android.content.SharedPreferences;
import android.content.Context;
public class MainActivity extends Activity {
String FileName = "myFile";
Button BtnSave;
EditText editName;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
BtnSave = findViewById(R.id.btn1);
BtnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
saveFile();
}
});
lv = (ListView) findViewById(R.id.list1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,empty);
lv.setAdapter(adapter);
//Setting onClickListener on ListView
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(),"Item Clicked: "+i,Toast.LENGTH_SHORT).show();
}
});
editName = findViewById(R.id.edit1);
}
private void saveFile() {
String strName = editName.getText().toString();
SharedPreferences sharedPref = getSharedPreferences(FileName,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("name", strName);
editor.commit();
Toast.makeText(this,"Data Saved Successfully",Toast.LENGTH_SHORT).show();
}
}
I think this should help you.
fun addProduct(productsItem: Product) {
sharedPreferences=context.getSharedPreferences(TAG,Context.MODE_PRIVATE)
var gson=Gson()
var cartProduct=getAllProducts()
cartProduct.add(productsItem)
var json=gson.toJson(cartProduct)
println(json)
sharedPreferences.edit().putString("cart_products",json).apply()
}
fun getAllProducts(): ArrayList<Product?> {
sharedPreferences=context.getSharedPreferences(TAG,Context.MODE_PRIVATE)
val listType =
object : TypeToken<List<Product?>?>() {}.type
var productsItemList:ArrayList<Product?> = ArrayList();
val json=sharedPreferences.getString("cart_products",null)
if (json !=null){
var gson=Gson()
productsItemList=gson.fromJson(json,listType)
}
return productsItemList
}
Okay, so you need to:
1)input some word in EditText
2) show it in ListView
3) save it to preferences
right?
I think you must do next steps:
Create private ArrayList<String> list = new ArrayList();
When you input something in EditText and clicked on button, call list.add(editName.getText.toString()); and then call saveFile and save list.toString();
When you need to load file, create function
private void loadFile() {
SharedPreferences sharedPref = getSharedPreferences(FileName,Context.MODE_PRIVATE);
String string = sharedPref.getString("name", "");
ArrayList<String> newList = new ArrayList<String>(Arrays.asList(string.split(", ")));
}
Load in adapter your newList.
I have made this app where in one particular activity i have a all the items listed in a list view. when you click the list item it goes to another activity where similar thing is happening. after that i was the clicked list items to be converted into a strings and transported into a 3rd activity where i can display those.
when i try to display them this shows in the text view where the clicked text item should have appeared:
this is code for the first activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.internal.Objects;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class TicketCategory extends AppCompatActivity {
public static String Category;
public String getCategory() {
return Category;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ticket_category);
populateTicketCategoryList();
final ListView listView = (ListView) findViewById(R.id.lvTicketCategory);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (i == 0) {
Category = listView.getItemAtPosition(i).toString();
Intent intent = new Intent(TicketCategory.this, Subcategory.class);
startActivity(intent);
}
}
});
}
private void populateTicketCategoryList()
{
ArrayList<CompTicketCategory> arrayOfTicket = CompTicketCategory.getTicket();
CompTicketCategoryAdapter adapter = new CompTicketCategoryAdapter(this, arrayOfTicket);
ListView listView = (ListView) findViewById(R.id.lvTicketCategory);
listView.setAdapter(adapter);
}
}
the code for the second activity is:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class Subcategory extends AppCompatActivity {
public String Category;
public static String Subcat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_subcategory);
populateSubcategoryList();
final ListView listView = (ListView) findViewById(R.id.lvSubcategory);
ArrayAdapter arrayAdapter = new ArrayAdapter<String>(Subcategory.this, android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Subcat = listView.getItemAtPosition(i).toString();
Intent intent = new Intent(Subcategory.this, SubmitTicket.class);
startActivity(intent);
}
});
and this is the code for the activity where both of the clicked items should be displayed:
public class SubmitTicket extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_submit_ticket);
Spinner spinner = (Spinner) findViewById(R.id.spinner_priority);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.priority_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final Button butt = findViewById(R.id.submit);
butt.setOnClickListener(new View.OnClickListener()
{
public void onClick (View view){
Toast.makeText(getApplicationContext(), "The ticket has been submitted", Toast.LENGTH_SHORT).show();
}
});
TextView textView = (TextView)findViewById(R.id.Category_submit_report);
textView.setText(TicketCategory.Category);
TextView tv = (TextView)findViewById(R.id.Subcategory_submit_report);
tv.setText(Subcategory.Subcat);
}
Please help me. i would appreciate any output. thanks!
UPDATE:
after trying
CompTicketCategory model = listView.getItemAtPosition(i);
Category=model.Category; // your Category variable
Category=model.getCategory();
this error is shown;
screenshot
You can use Intent Extra Feature.
In the First Activity,
Intent intent = new Intent(Subcategory.this, SubmitTicket.class);
switch1.putExtra("deviceID", listView.getItemAtPosition(i).toString(););
startActivity(intent);
Then Next activity recall them,
Intent intent = getIntent();
String data = intent.getStringExtra("data");
Try this in your TicketCategory actvity
Use this:
CompSubcategory model = listView.getItemAtPosition(i);
Category=model.Category; // your Category variable
Category=model.getCategory(); // or use getter setter method
Instead of this:
Category = listView.getItemAtPosition(i).toString();
I want to move Interface array called DisplayItem
this is activity1 code:
package com.example.eranp.clientpage;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class Main2Activity extends Activity implements Serializable{
private EditText pro_device_det;
private Button saveDataBase, btnViewAll ;
private DatabaseReference databaseCustomer, databaseDevice, databaseProblem, db;
private Spinner spinner;
private String option1, option2,option3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main3);
db = FirebaseDatabase.getInstance().getReference();
databaseCustomer = FirebaseDatabase.getInstance().getReference("customer");
databaseDevice = FirebaseDatabase.getInstance().getReference("device");
databaseProblem = FirebaseDatabase.getInstance().getReference("problem");
option1 = getResources().getString(R.string.option1u);
option2 = getResources().getString(R.string.option2u);
option3 = getResources().getString(R.string.option3u);
saveDataBase = (Button) findViewById(R.id.save_btn);
btnViewAll = (Button)findViewById(R.id.button_viewAll);
final List<String> list = new ArrayList<String>();
list.add(option1);
list.add(option2);
list.add(option3);
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
pro_device_det = (EditText) findViewById(R.id.pro_device_det);
final Intent intent = getIntent();
final String fName = intent.getStringExtra("fName");
final String lName = intent.getStringExtra("lName");
final String phone = intent.getStringExtra("phone");
final String email = intent.getStringExtra("email");
final String modelDevice = intent.getStringExtra("modelDevice");
final String nameDevice = intent.getStringExtra("nameDevice");
saveDataBase.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String proDeviceDet = pro_device_det.getText().toString().trim();
String shortProDeviceDet = string3Words(proDeviceDet);
DateFormat df = new SimpleDateFormat("d MMM yyyy, HH:mm");
String date = df.format(Calendar.getInstance().getTime());
if (!TextUtils.isEmpty(proDeviceDet)) {
String id = databaseCustomer.push().getKey();
Customer customer = new Customer(id, fName, lName, email, phone);
databaseCustomer.child(id).setValue(customer);
Device device = new Device(id, nameDevice, modelDevice);
databaseDevice.child(id).setValue(device);
int urgency = spinnerUrgency(spinner);
Problem problem = new Problem(id, date, proDeviceDet, urgency, shortProDeviceDet);
databaseProblem.child(id).setValue(problem);
DisplayItem[] displayItem= new DisplayItem[]{customer, problem};
final int REQ_CODE = 1;
Intent intent2 = new Intent(Main2Activity.this, CustomersPage.class);
intent2.putExtra("DisplayItem",displayItem);
startActivity(intent2);
Toast.makeText(Main2Activity.this , "Customer added", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Main2Activity.this, "Please write on an empty cell", Toast.LENGTH_LONG).show();
}
}
});
}
});
}
}
The Interface code:
package com.example.eranp.clientpage;
import java.io.Serializable;
/**
* Created by Eran P on 15/04/2018.
*/
public interface DisplayItem extends Serializable {
}
this is the second activity that I want to move the interface code:
package com.example.eranp.clientpage;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class CustomersPage extends Activity {
ListView listViewCustomers;
List<Customer> customers;
List<Problem> problems;
List<Device> devices;
DatabaseReference databaseCustomers, databaseProblem, databaseDevice,db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customers_page);
db = FirebaseDatabase.getInstance().getReference();
databaseCustomers = FirebaseDatabase.getInstance().getReference("customer");
databaseProblem = FirebaseDatabase.getInstance().getReference("problem");
databaseDevice = FirebaseDatabase.getInstance().getReference("device");
listViewCustomers = (ListView)findViewById(R.id.listViewCostumers);
customers = new ArrayList<>();
devices = new ArrayList<>();
problems = new ArrayList<>();
Intent intentl = getIntent();
listViewCustomers.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
Customer customer = customers.get(i);
showUpdateDeleteDialog(customer.getCustomerId(), customer.getfName() + " " + customer.getlName());
return true;
}
});
listViewCustomers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Customer customer = customers.get(i);
Problem problem = problems.get(i);
Device device = devices.get(i);
Intent intent = new Intent(CustomersPage.this, ClientPageData.class);
intent.putExtra("fName", customer.getfName());
intent.putExtra("lName", customer.getlName());
intent.putExtra("phone", customer.getPhoneNum());
intent.putExtra("email", customer.geteMail());
intent.putExtra("problemShort", problem.getProDeviceDet());
intent.putExtra("nameDevice", device.getDeviceName());
intent.putExtra("modelDevice", device.getDeviceModel());
startActivity(intent);
}
});
}
private void showUpdateDeleteDialog(final String customerId, String customerName) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.update_dialog, null);
dialogBuilder.setView(dialogView);
final EditText editTextFName = (EditText) dialogView.findViewById(R.id.ET_fName);
final EditText editTextLName = (EditText) dialogView.findViewById(R.id.ET_lName);
final EditText editTextPhone = (EditText) dialogView.findViewById(R.id.ET_phone);
final EditText editTextEmail = (EditText) dialogView.findViewById(R.id.ET_email);
final EditText editTextProDetDev = (EditText) dialogView.findViewById(R.id.ET_proDetDevice);
final Button buttonUpdate = (Button) dialogView.findViewById(R.id.buttonUpdateArtist);
final Button buttonDelete = (Button) dialogView.findViewById(R.id.buttonDeleteArtist);
dialogBuilder.setTitle(customerName);
final AlertDialog b = dialogBuilder.create();
b.show();
buttonUpdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String fName = editTextFName.getText().toString().trim();
String lName = editTextLName.getText().toString().trim();
String phone = editTextPhone.getText().toString().trim();
String email = editTextEmail.getText().toString().trim();
String proDetDevice = editTextProDetDev.getText().toString().trim();
if (!TextUtils.isEmpty(fName)) {
updateCustomer(customerId, fName, lName,phone,email, proDetDevice);
b.dismiss();
}
}
});
buttonDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
deleteCustomer(customerId);
}
});
}
private boolean deleteCustomer(String id) {
//getting the specified Customer reference
DatabaseReference dR = FirebaseDatabase.getInstance().getReference("customer").child(id);
DatabaseReference dRP = FirebaseDatabase.getInstance().getReference("device").child(id);
DatabaseReference dRD = FirebaseDatabase.getInstance().getReference("problem").child(id);
//removing Customer
dR.removeValue();
dRP.removeValue();
dRD.removeValue();
Toast.makeText(getApplicationContext(), "Customer Deleted", Toast.LENGTH_LONG).show();
return true;
}
private boolean updateCustomer(String id, String fName, String lName, String phone, String email, String proDetDevice) {
//getting the specified artist reference
DatabaseReference dR = FirebaseDatabase.getInstance().getReference("customer").child(id);
DatabaseReference dRp = FirebaseDatabase.getInstance().getReference("problem").child(id);
//updating artist
Customer customer= new Customer(id, fName, lName, email,phone);
dR.setValue(customer);
DateFormat df = new SimpleDateFormat("d MMM yyyy, HH:mm");
String date = df.format(Calendar.getInstance().getTime());
String shortProDeviceDet = string3Words(proDetDevice);
Problem problem= new Problem(id, date,proDetDevice,0, shortProDeviceDet );
dRp.setValue(problem);
Toast.makeText(getApplicationContext(), "Customer Updated", Toast.LENGTH_LONG).show();
return true;
}
//attaching value event listener
databaseCustomers.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//clearing the previous artist list
customers.clear();
//iterating through all the nodes
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//getting customer
Customer customer = postSnapshot.getValue(Customer.class);
//adding customer to the list
customers.add(customer);
}
Intent intent2 = getIntent();
DisplayItem [] displayItem = (DisplayItem[]) intent2.getExtras().getSerializable("DisplayItem");
//creating adapter
CustomerAdapter customerAdapter = new CustomerAdapter(CustomersPage.this,displayItem );
//attaching adapter to the listview
listViewCustomers.setAdapter(customerAdapter);
}
}
when I tried to run the app this crashed and in the log file he says error in this line:
DisplayItem [] displayItem = (DisplayItem[])
intent2.getExtras().getSerializable("DisplayItem");
Thank you very much for helping!
Your code is not too clear but try use this format:
//When you want to pass an array as intent:
private ArrayList<String> arrayList = new ArrayList<String>();
Intent intent = new Intent();
intent.putStringArrayListExtra("data", arrayList);
startActivity(intent);
// To receive intent in the second activity
private ArrayList<String> newArrayList = new ArrayList<String>();
newArrayList = getIntent.getStringArrayListExtra("data");
Check if your classes Device and Customer implements serializable