I am working on a shopping car app and need to have the product list and every product stock. for this what I am trying to do is implement a global list variable. I have read about global variables and I could implement it, but can't figure out how a list would be. any suggestions or help on how to implement it would be great, thanks.
public class Application extends android.app.Application {
private int data;
public int getData(){
return data;
}
public void setData(int d){
this.data=d;
}
}
((Application) this.getApplication()).setData(0);
x1=((Application) this.getApplication()).getData();
I figure it out following your suggestions. first on the applicacion class
public class Application extends android.app.Application {
public ArrayList myGlobalArray = null;
public Application() {
myGlobalArray = new ArrayList();
}
}
then for the porpouse of my app I add the respective values on their respectives indexes
for (int i=0;i<productsList.size();i++){
if (productsList.get(i).idProduct==idProduct){
values.add(i,Integer.parseInt(txtQuantity.getText().toString()));
((Application)getApplicationContext()).myGlobalArray = (ArrayList) values;
}
}
and when I need to get the values
ArrayList<Integer> test = new ArrayList<>();
for (int d=0;d< ((Application) getApplicationContext()).myGlobalArray.size();d++) {
test.add((Integer) ((Application)getApplicationContext()).myGlobalArray.get(d));
}
Thank you!
Related
What I want to do is passing DataModel array between Activity by Intent.
DataModel class has Bitmap object and FirebaseVisionLabel object. I found many sites to implement this.
Many people said that DataModel class should implements Serializable or Parceable interface to pass DataModel[] or ArrayList<DataModel>.
So I tried, but the real problem was FirebaseVisionLabel class cannot be serializable. Also, I cannot modify that class because it is firebase library.
How can I pass DataModel array by intent??
Point
Want to pass array or arraylist of my own class by intent.
that class has unserializable object and I cannot modify.
how can I pass or deal with it?
Use Parceable. It works perfect
public class Test implements Parcelable
{
FirebaseVisionLabel firebaseVisionLabel;
String testString;
protected Test(Parcel in) {
testString = in.readString();
}
public static final Creator<Test> CREATOR = new Creator<Test>() {
#Override
public Test createFromParcel(Parcel in) {
return new Test(in);
}
#Override
public Test[] newArray(int size) {
return new Test[size];
}
};
public FirebaseVisionLabel getFirebaseVisionLabel() {
return firebaseVisionLabel;
}
public void setFirebaseVisionLabel(FirebaseVisionLabel firebaseVisionLabel) {
this.firebaseVisionLabel = firebaseVisionLabel;
}
public String getTestString() {
return testString;
}
public void setTestString(String testString) {
this.testString = testString;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(testString);
}
}
After that for passing data through intent
Test test = new Test();
test.setTestString("test");
test.setFirebaseVisionLabel(yourObject);
Intent intent = new Intent(this, BaseActivity.class);
intent.putExtra("key", test);
startActivity(intent);
Use the below code to get ArrayList data without Serialized or Parcelable:
Consider,
Intent intent = new Intent(this, your_second_class.class);
intent.putStringArrayListExtra("<your_name_here>", your_list_here);
startActivity(intent);
Then in your second class use:
Intent i = getIntent();
new_list = i.getStringArrayListExtra("<your_name_here>");
Hope it will work fine.
You may use Application class, which can be used in all the screen, activities.
So store array in Application class and used anywhere in app.
FirebaseVisionLabel doesn't have too many properties. You will need to serialize Label / Confidence /... (anything you care) yourself by creating your own VisionLabelParcelable class.
So far, there are not enough use cases to make ML Kit return a Parcelable FirebaseVisionLabel. Most apps should extract the info they are interested in and pass around if they want.
I've been trying to add Parse to my android app. Everything is fine setting it up. Adding certain data to the cloud and users etc. I'm trying to add data from an classes ArrayList that sends out params. The Class Collection from the arraylist is fine which is called Tasks. It has the correct information. I set up a new class which extends the ParseObject, which is below, that should fill in for the Tasks class to enter the data to the Parse Cloud.
package beans;
import com.parse.ParseClassName;
import com.parse.ParseObject;
import com.parse.ParseUser;
import java.util.ArrayList;
/**
* Created by KieranMcc on 11/01/2016.
*/
#ParseClassName("Tasks")
public class ParseTasks extends ParseObject {
private int id; //_id
private int task_id; //task_id
private String task; //task_name
private boolean completed; //_isCompleted
public ParseTasks(){
super();
}
public ParseTasks(Tasks tasks){
super();
}
public int getId() {
return getInt("_id");
}
public void setId(int id) {
put("_id", id);
}
public int getTask_id() {
return getInt("task_id");
}
public void setTask_id(int task_id) {
put("task_id", task_id);
}
public String getTask() {
return getString("task_name");
}
public void setTask(String task) {
put("task_name", task);
}
public boolean isCompleted() {
return getBoolean("_isCompleted");
}
public void setCompleted(boolean completed) {
put("_isCompleted", completed);
}
public void setUser(ParseUser user){
put("tasks_user", user);
}
public ParseUser getUser(){
return getParseUser("task_user");
}
}
What I'm trying to do is loop through the arraylist with a collection of the class Tasks. ArrayList
This is my code but it isn't saving to Parse
protected void uploadToCloud(Task task){
ParseTask taskParse = new ParseTask();
taskParse.setUser(ParseUser.getCurrentUser());
taskParse.setId(task.getId());
taskParse.setName(task.getName());
taskParse.setNumOfTasks(task.getNumOfTasks());
taskParse.setNumOfTasksCompleted(task.getNumOfTasksCompleted());
taskParse.saveInBackground();
// add task to cloud
// loop through tasks and add one by one to cloud
ParseTasks tasksParse= new ParseTasks();
for(int i = 0; i < task.getTasks().size(); i++){
tasksParse.setId(task.getTasks().get(i).getId());
tasksParse.setTask(task.getTasks().get(i).getTask());
tasksParse.setTask_id(task.getTasks().get(i).getTaskId());
tasksParse.setCompleted(false);
tasksParse.setUser(ParseUser.getCurrentUser());
tasksParse.saveInBackground();
}
}
Not quite sure what i'm doing wrong as I don't get an error or anything. And the ParseTask about goes through fine? Can someone tell me why it wont go through please. Thank you very much for reading over the long post and for any help :)
I managed to find out what the problem was. So thought I'd share what I learned for anyone having the issue in the future :)
With parse you should add more then one piece of data in a series yourself. Like I tried at the top! Using the method saveAllInBackground() is what you should use.
What you need to have is a List of ParseObjects and pass them into saveAllInBackground(list, callbackmethod) like so. I then added a constructor with params and used that to save to the cloud by using the constructor as a new instance.
Hope it might help some people in the future :)
I have my own class Order with many fields in it. I put LinkedList of them to the ListView with the custom ArrayAdapter. But I need to bind each order object with corresponding View of listView. I tried to implement getChildView() method of the listView but it doesn't work properly for me. Tell me please how can I implement robust and fast solution for this? SHould I use LinkedList for this? Or should I create my own data container? Besides, adding one more View field to the Order object seems to me like perfomance decreasing.
I solved this issue by HashMap using. I use singletone pattern for hashmap creating and using:
public class OrdersViewMapSingletone {
Map<Order, View> arrayList;
private static OrdersViewMapSingletone instance;
private OrdersViewMapSingletone()
{
arrayList = new HashMap<Order,View>();
}
public static OrdersViewMapSingletone getInstance(){
return instance;
}
public static void initInstance()
{
if (instance == null)
{
instance = new OrdersViewMapSingletone();
}
}
public Map<Order, View> getOrderViewMap()
{
return arrayList;
}
#Override
public String toString()
{
return getOrderViewMap().toString();
}
}
and in my OrderAdapter:
Map<Order, View> orderViewMap = OrdersViewMapSingletone.getInstance().getOrderViewMap();
add items in the getView() method:
orderViewMap.put(order,row);
and this method for getting items in the activity:
public View getMapedView(Order order)
{
return this.orderViewMap.get(order);
}
worked good for me. Perfomance is much better than ArrayList using.
How do I create an array of different types? If each class is an extension of the _object class, can I just make an _object array and add the extensions to it?
Example:
class _object {
int type = 1;
public _object() {
type = 2;
}
public doSomething() {
}
}
class tree extends _object {
public tree() {
}
}
class apple extends _object {
public apple() {
}
}
public tree aTree = new tree();
public apple anApple = new apple();
public _object[] objects = new _object[] { aTree, anApple };
The example in your question works. This is known as polymorphism.
http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html
http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
After setting up the above code in android as a project and debugging it, it in fact does work, so long as you only call methods of the base object.
What I need is simple:
If oncreate I have an arraylist created with some items.
But, how can I edit that arraylist(add items,remove) from another function(method)?
Just use a object field:
public class X extends ThatAndroidAppClassWhatsNameIForgot {
private List<ItemType> list = new ArrayList<ItemType>();
public void onCreate() {
list.add(...);
}
private void otherMethod(...) {
list.remove(...);
}
}
create that array list as a private field of your class. That way it will be visible to oncreate method or any other