i have a growing arraylist with a custom object, for it to grow i need to save it so it doesn't get cleared, but even after implementing parcelable still doesn't work. did i implement parcelble wrong? all code works apart from being able to save my arraylist, if some one could help it will be a great help
//class
public class team implements Parcelable {
// Defining Activity class to use
private Activity activity;
// Defining text box
LinearLayout lay;
View Tbox;
// Members Defining
String teamname;
String teacher;
ArrayList<Players> members = new ArrayList<Players>();
public team(Activity activity, String teamname, String teacher) {
// To be able to use activity
this.activity = activity;
// Normal things to define
this.teamname = teamname;
this.teacher = teacher;
// initialize the lay and Tbox variables
lay = activity.findViewById(R.id.Scontainer);
Tbox = activity.getLayoutInflater().inflate(R.layout.text_box_output, null);
}
public void addtext() {
// add text to the layout and view
TextView title = Tbox.findViewById(R.id.daTeam);
title.setText(teamname);
lay.addView(Tbox);
}
//bs
protected team(Parcel in) {
teamname = in.readString();
teacher = in.readString();
}
public static final Creator<team> CREATOR = new Creator<team>() {
#Override
public team createFromParcel(Parcel in) {
return new team(in);
}
#Override
public team[] newArray(int size) {
return new team[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(#NonNull Parcel parcel, int i) {
parcel.writeString(teamname);
parcel.writeString(teacher);
}
}
//mainactivity
public class MainActivity extends AppCompatActivity {
ArrayList<team> listofteams ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null ) {
listofteams = savedInstanceState.getParcelableArrayList("key");
}
else{
listofteams = new ArrayList<team>();
}
setContentView(R.layout.sports_screen);
//sending out variable
Intent intent = new Intent (this, Creating_Class_main.class);
Activity ma = this;
//names
Intent MB = getIntent();
String Classinp = MB.getStringExtra("strC");
String tchinp = MB.getStringExtra("strT");
//adding team
team sportsteam;
System.out.println(listofteams);
boolean flag = MB.getBooleanExtra("flag", false);
if (flag){
sportsteam = new team(ma, Classinp,tchinp);
listofteams.add(sportsteam);
System.out.println(listofteams);
}
}
//saveing layout
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putParcelableArrayList("key", listofteams);
super.onSaveInstanceState(outState);
}
}
Here's what inside Player.class
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by pietsteph on 15/09/17.
*/
public class Player implements Parcelable{
String name;
int score;
protected Player(Parcel in) {
name = in.readString();
score = in.readInt();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(score);
}
#Override
public int describeContents() {
return 0;
}
public static final Creator<Player> CREATOR = new Creator<Player>() {
#Override
public Player createFromParcel(Parcel in) {
return new Player(in);
}
#Override
public Player[] newArray(int size) {
return new Player[size];
}
};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public Player(String name) {
this.name = name;
this.score = 0;
}
}
In the MainActivity.class, I make my custom object as an array with size = 2 (2 player) and put it in intent.
Player players[] = new Player[2];
players[0] = new Player(name1);
players[1] = new Player(name2);
Intent intent = new Intent(getApplicationContext(), TurnActivity.class);
intent.putExtra(PLAYER_KEY, players);
startActivity(intent);
Want to get my intent in TurnActivity.class, tried getParcelableExtra.
Intent intent = getIntent();
Player players[] = intent.getParcelableExtra(MainActivity.PLAYER_KEY);
But it gave me an error
Error:(28, 59) error: incompatible types: inferred type does not conform to upper bound(s)inferred: INT#1
upper bound(s): Player[],Parcelable
where INT#1 is an intersection type:
INT#1 extends Player[],Parcelable
Even tried getParcelableArrayExtra and gave me a red line says Incompatible type.
This is a problem with Java 8. You probably need to do something like this:
Intent intent = getIntent();
Parcelable parcelable[] = intent.getParcelableArrayExtra(MainActivity.PLAYER_KEY);
Now, when you use it, you need to cast it to its proper type. Something like this:
Player player = (Player)parcelable[0];
Implements
Parcalable
interface with
Player
class
I used
Android Parcelable code generator plugin
to do this task.
then you will be able to save your custom objects array in intents.
I use to do something like this
public void moveToNext(ArrayList images){
Intent mainIntent = new Intent(SplashScreen.this,MainActivity.class);
//add data to parcalable
mainIntent.putParcelableArrayListExtra("data",images);
startActivity(mainIntent);
finish();
}
//while getting data
ArrayList v =
getIntent().getParcelableArrayListExtra("data");
I have a second activity that handles all the user input and another activity that handles all the data from the second activity. What I want to do is call a class "SubmitName" from the activity to the second activity so that I dont need to pass the values from second activity to the main activity anymore. Here are the codes..
MainActivity (Where the class "SubmitName" are located and values are passed.)
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
TextView Name;
String lastname;
String licensenumber;
String mviolation;
String maplace;
String maddress;
String phonenumber;
String officername;
String contactnumber;
String datetime;
RecyclerView.LayoutManager layoutManager;
RecyclerAdapter adapter;
ArrayList<Violator> arrayList = new ArrayList<>();
BroadcastReceiver broadcastReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addBtn = (Button)findViewById(R.id.btnAdd);
addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, FragActivity.class);
startActivity(intent);
}
});
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
Name = (TextView) findViewById(R.id.tvName);
Intent intent = getIntent();
String str = intent.getStringExtra("firstname");
lastname = intent.getStringExtra("lastname");
licensenumber = intent.getStringExtra("licensenumber");
mviolation = intent.getStringExtra("violation");
maplace = intent.getStringExtra("arrestplace");
maddress = intent.getStringExtra("address");
phonenumber = intent.getStringExtra("phonenumber");
contactnumber = intent.getStringExtra("contactnumber");
officername = intent.getStringExtra("officername");
datetime = intent.getStringExtra("datetime");
Name.setText(str);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter = new RecyclerAdapter(arrayList);
recyclerView.setAdapter(adapter);
readFromLocalStorage();
broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
readFromLocalStorage();
}
};
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.nav_bar, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.TrafficAd:
Intent i = new Intent(this, TrafficAdvisory.class);
this.startActivity(i);
break;
}
return super.onOptionsItemSelected(item);
}
public void submitName(View view)
{
String name = Name.getText().toString();
String lname = lastname;
String lnumber = licensenumber;
String violation = mviolation;
String aplace = maplace;
String address = maddress;
String pnumber = phonenumber;
String cnumber = contactnumber;
String oname = officername;
String dtime = datetime;
saveToAppServer(name,lname,lnumber,violation,aplace,address,pnumber,cnumber,oname,dtime);
Name.setText("");
}
public void readFromLocalStorage()
{
arrayList.clear();
DbHelper dbHelper = new DbHelper(this);
SQLiteDatabase database = dbHelper.getReadableDatabase();
Cursor cursor = dbHelper.readFromLocalDatabase(database);
while (cursor.moveToNext())
{
String name = cursor.getString(cursor.getColumnIndex(DBContract.NAME));
String lname = cursor.getString(cursor.getColumnIndex(DBContract.LNAME));
String lnumber = cursor.getString(cursor.getColumnIndex(DBContract.LNUMBER));
String violation = cursor.getString(cursor.getColumnIndex(DBContract.VIOLATION));
String aplace = cursor.getString(cursor.getColumnIndex(DBContract.ARRESTPLACE));
String address = cursor.getString(cursor.getColumnIndex(DBContract.ADDRESS));
String pnumber = cursor.getString(cursor.getColumnIndex(DBContract.PNUMBER));
String cnumber = cursor.getString(cursor.getColumnIndex(DBContract.CNUMBER));
String oname = cursor.getString(cursor.getColumnIndex(DBContract.ONAME));
String dtime = cursor.getString(cursor.getColumnIndex(DBContract.DTIME));
int sync_status = cursor.getInt(cursor.getColumnIndex(DBContract.SYNC_STATUS));
arrayList.add(new Violator(name,lname,lnumber,violation,aplace,address,pnumber,cnumber,oname,dtime,sync_status));
}
adapter.notifyDataSetChanged();
cursor.close();
}
public void saveToAppServer(final String name,final String lname, final String lnumber,final String violation, final String aplace,final String address, final String pnumber, final String cnumber, final String oname, final String dtime)
{
if (checkNetworkConnection())
{
StringRequest stringRequest = new StringRequest(Request.Method.POST,DBContract.SERVER_URL,
new Response.Listener<String>(){
#Override
public void onResponse(String response){
try {
JSONObject jsonObject = new JSONObject(response);
String Response = jsonObject.getString("response");
if(Response.equals("OK"))
{
saveToLocalStorage(name,lname,lnumber,violation,aplace,address,pnumber,cnumber,oname,dtime,DBContract.SYNC_STATUS_OK);
}
else
{
saveToLocalStorage(name,lname,lnumber,violation,aplace,address,pnumber,cnumber,oname,dtime,DBContract.SYNC_STATUS_FAILED);
}
} catch (JSONException e){
e.printStackTrace();
}
}
},new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error){
saveToLocalStorage(name,lname,lnumber,violation,aplace,address,pnumber,cnumber,oname,dtime,DBContract.SYNC_STATUS_FAILED);
}
})
{
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("name",name);
params.put("lname",lname);
params.put("lnumber",lnumber);
params.put("violation", violation);
params.put("aplace", aplace);
params.put("address",address);
params.put("pnumber",pnumber);
params.put("cnumber",cnumber);
params.put("oname",oname);
params.put("dtime",dtime);
return params;
}
}
;
MySingleton.getInstance(MainActivity.this).addToRequestQue(stringRequest);
}
else
{
saveToLocalStorage(name,lname,lnumber,violation,aplace,address,pnumber,cnumber,oname,dtime,DBContract.SYNC_STATUS_FAILED);
}
}
SecondActivity (Where inputs are handled and data passing to the mainactivity)
public class ViolatorDetail extends AppCompatActivity implements View.OnClickListener{
EditText Name;
Button btnClose;
TextView DTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_violator_detail);
DTime = (TextView)findViewById(R.id.tvDTime);
final String currentDT = DateFormat.getDateTimeInstance().format(new Date());
DTime.setText(currentDT);
btnClose = (Button) findViewById(R.id.btnClose);
btnClose.setOnClickListener(this);
Button btnSubmit = (Button)findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText Name = (EditText)findViewById(R.id.etfName);
EditText LName = (EditText)findViewById(R.id.etlName);
EditText LNumber = (EditText)findViewById(R.id.etlNumber);
EditText Violation = (EditText)findViewById(R.id.etViolation);
EditText Arrestplace = (EditText)findViewById(R.id.etaPlace);
EditText Address = (EditText)findViewById(R.id.etAddress);
EditText PNumber = (EditText)findViewById(R.id.etpNumber);
EditText CNumber = (EditText)findViewById(R.id.etcNumber);
EditText OName = (EditText)findViewById(R.id.etoName);
String DT = DTime.getText().toString();
Intent intent = new Intent(ViolatorDetail.this, MainActivity.class);
intent.putExtra("firstname", Name.getText().toString());
intent.putExtra("lastname", LName.getText().toString());
intent.putExtra("licensenumber", LNumber.getText().toString());
intent.putExtra("violation", Violation.getText().toString());
intent.putExtra("arrestplace", Arrestplace.getText().toString());
intent.putExtra("address", Address.getText().toString());
intent.putExtra("phonenumber", PNumber.getText().toString());
intent.putExtra("contactnumber", CNumber.getText().toString());
intent.putExtra("officername", OName.getText().toString());
intent.putExtra("datetime", DT);
startActivity(intent);
}
});
}
}
What I want to do is call the "SUBMITNAME" class to the second activity so that no data passing will be done anymore.
As other friends mentioned Intent is a correct and good way to transfer data between activities. But if you want to avoid writing so much code to transfer data I suggest to create a pure java class (or java bean) and define all needed fields in that class (note: this class should implement java.io.Serializable interface). Now you could transfer instances of this class between activities.
I don’t think there is a better way of passing data between activities than Intents.
What you probably need is encapsulation of passing of extra. You can achieve this by making a static method in the ViolatorDetail class, which accepts as arguments as values you would like to pass, and returns Intent.
public static Intent newIntent(Context packageContext, String ... args){
Intent intent = new Intent(packageContext, ViolatorDetail.this);
intent.putExtra(EXTRA_STRING_ARGS, args);
return intent;
}
Then in the caller class you make an intent by makeing a static call on that function, and pass values as arguments
Intent intent = ViolatorDetail.newIntent(getActivity(), strings)
startActivity(intent);
However, in your case, you should probably make a more sensible way of passing data than as array of strings.
If you don't want to pass data between Activities with Intent, you can do it by writing certain data in a file and when you need it just read from it... I did it like this and I'm still happy i did it that way, it's simple and relatively quick, you just have to care a little about IOExceptions.
I am having some hard time trying to figure out how to write data with a Parcel object, I am new using this class and I've been doing some research but don't understand how to, here is the parcelable class
public class Item implements Parcelable {
private String iItemName;
private String iType;
private String iSerial;
private String iRetailer;
private String iLocation;
private String iValue;
private String iDescription;
public Item() {
super();
}
public Item (Parcel in) {
super();
this.iItemName = in.readString();
this.iType = in.readString();
this.iSerial = in.readString();
this.iRetailer = in.readString();
this.iLocation = in.readString();
this.iValue = in.readString();
this.iDescription = in.readString();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(getItemName());
parcel.writeString(getType());
parcel.writeString(getSerial());
parcel.writeString(getRetailer());
parcel.writeString(getLocation());
parcel.writeString(getValue());
parcel.writeString(getDescription());
}
public static final Parcelable.Creator<Item> CREATOR = new Parcelable.Creator<Item>(){
#Override
public Item createFromParcel(Parcel source) {
Item item = new Item();
item.iItemName = source.readString();
return item;
}
public Item [] newArray(int size) {
return new Item[size];
}
};
public String getItemName() {
return iItemName;
}
public void setItemName(String iItemName) {
this.iItemName = iItemName;
}
my question is if I want to create a new Item object I need to send as a parameter a Parcel Object am I right? so I can do something like:
String name = getString("name");
// how to add name to the parcel object?
Parcel parcel;
Item i = new Item(parcel);
No you do not need to. Your object now has two constructors. You should create the object like normal and call its setter methods. You should avoid manually creating parcels and rather use writeValue() and readValue() methods.
// Create an item as usual
Item myItem = new Item();
myItem.setItemName("name");
// Add it to a parcel
Parcel parcel = Parcel.obtain();
parcel.writeValue(myItem);
// Read the item back from the parcel
parcel.setDataPosition(0);
Item newItem = (Item) parcel.readValue(Item.class.getClassLoader());
// When you are finished with the parcel
parcel.recycle();
Usually you would create an object from a Parcel when receiving it from an Intent. In which case you would do the following.
// Create intent
Intent intent = new Intent(this, MyActivity.class);
Bundle itemBundle = new Bundle();
itemBundle.putParcelable("item_extra", myItem);
intent.putExtras(itemBundle);
startActivity(intent);
// In the activities onCreate(Bundle savedInstanceState)
Item myItem = getIntent().getParcelableExtra("item_extra");
I have this problem with this piece of code and I have been a few days trying to solve it and I can't find the solution.
I have this parcelable class:
public class Sistema implements Parcelable{
private ArrayList<Lista> listas;
private ArrayList<Articulo> articulos;
public Sistema() {
listas = new ArrayList<Lista>();
articulos = new ArrayList<Articulo>();
}
public ArrayList<Lista> getListas() {
return listas;
}
public void agregarLista(Lista lista) {
this.getListas().add(lista);
}
public ArrayList<Articulo> getArticulos(){
return this.articulos;
}
public void agregarArticulo(Articulo articulo){
this.getArticulos().add(articulo);
}
public static final Parcelable.Creator<Sistema> CREATOR =
new Parcelable.Creator<Sistema>() {
#Override
public Sistema createFromParcel(Parcel parcel) {
return new Sistema(parcel);
}
#Override
public Sistema[] newArray(int size) {
return new Sistema[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeTypedList(listas);
parcel.writeTypedList(articulos);
}
public Sistema(Parcel parcel)
{
parcel.readTypedList(listas, Lista.CREATOR);
parcel.readTypedList(articulos, Articulo.CREATOR);
}
}
And then in the main i use an intent to send a Sistema object to an other activity:
public void addArticle(View view) {
Intent intent = new Intent(this, AgregarArticulo.class);
intent.putExtra("objectSystem", s);
this.startActivityForResult(intent, 1);
}
Here is where the problem is, i recieve the intent in the second activity with this code:
Intent intento = getIntent();
s = intento.getParcelableExtra("objectSystem");
When I run the code, i get a NullPointerException in the getParcelableExtra
Do you guys have an idea of what the problem should be?
Thanks
Could you please try this . You need to prefix your package name(com.example.whatever) with the name your passing in putExtra:
public void addArticle(View view) {
Intent intent = new Intent(this, AgregarArticulo.class);
intent.putExtra("YOURPACKAGENAME.objectSystem", s);
this.startActivityForResult(intent, 1);
}
and same in getParcelableExtra also:
Intent intento = getIntent();
s = intento.getParcelableExtra("YOURPACKAGENAME.objectSystem");