0

My goal is to create an app that in basics have two different activities, one in which the user creates a "protocol" (EditorActivity) and on in which the user can view them in a listview(CatalogActivity). If there is something wrong with a protocol, the user must be able to press one of the list view items in the listview, and from there go back in to the EditorActivity and edit the specific item.

My problem is that I have not figured out how to get the old data from the CatalogActivity in to the EditorActivity.

From firebase console: [Firebase structure][1]

CustomProtocol:

public class CustomProtocol {
    public String dateDrill;
    public String pileID;
    public boolean cleaned;
    public CustomProtocol() {
    }


    public CustomProtocol(String pileID,
                          String dateDrill,

                          boolean cleaned) {
        this.pileID = pileID;
        this.dateDrill = dateDrill;
        this.cleaned = cleaned;
    }

    public void setPileID(String pileID) {
        this.pileID = pileID;
    }

    public String getPileID() {
        return pileID;
    }

    public void setDateDrill(String dateDrill) {
        this.dateDrill = dateDrill;
    }

    public String getDateDrill() {
        return dateDrill;
    }
}

Snippet from CatalogActivity:

        final String projectNumber = projectPrefs.getString(getString(R.string.settings_project_number_key), getString(R.string.settings_project_number_by_default));

            mFirebaseDatabase = FirebaseDatabase.getInstance();
            mProtocolDatabaseReference = mFirebaseDatabase.getReference().child(projectNumber);


            List<CustomProtocol> protocols = new ArrayList<>();
            mProtocolAdapter = new ProtocolAdapter(this, R.layout.item_protocol, protocols);

            mProtocolListView.setAdapter(mProtocolAdapter);
            attachDatabaseReadListener();

            mProtocolListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

                  Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);

              intent.putExtra("Exiting protocol", EXISTING_PROTOCOL);
                   }
            });
        }

EditorActivty:

 public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editor);

 EXISTING_PROTOCOL = intent.getBooleanExtra("Exiting protocol", false);
     mEditorFirebaseDatabase = FirebaseDatabase.getInstance();
           mEditorProtocolDatabaseReference = 
 mEditorFirebaseDatabase.getReference().child(projectNumber);

if (EXISTING_PROTOCOL)

            mProtocolDatabaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                 //WHAT GOES HERE SO THAT I CAN POPULATE THE TEXTVIEWS IN THE ACTIVITY_EDITOR WITH THE EXISTING VALUES?
                }}

And after this I'm stuck. I guess that I must add something more to the databasereference in the EditorActivity, but I cannot figure out what? Since I don't know the pileID until after the listitem has been clicked? Is there an easier way to do this?

Thank you in advance!

1]: https://i.stack.imgur.com/Bt1mZ.png

KENdi
  • 7,576
  • 2
  • 16
  • 31
Rasmus
  • 21
  • 6

2 Answers2

1

You can pass List<CustomProtocol> through intent. Your CustomProtocol must be implement Parcelable.

Same question is here

Son Tieu
  • 517
  • 4
  • 15
0

1.Make CustomProtocol Parcelable, IDE can do it automatically

public class CustomProtocol implements Parcelable {
  public String dateDrill;
  public String pileID;
  public boolean cleaned;

  public CustomProtocol() {
  }


  public CustomProtocol(String pileID,
                        String dateDrill,

                        boolean cleaned) {
    this.pileID = pileID;
    this.dateDrill = dateDrill;
    this.cleaned = cleaned;
  }

  protected CustomProtocol(Parcel in) {
    dateDrill = in.readString();
    pileID = in.readString();
    cleaned = in.readByte() != 0;
  }

  public static final Creator<CustomProtocol> CREATOR = new Creator<CustomProtocol>() {
    @Override
    public CustomProtocol createFromParcel(Parcel in) {
      return new CustomProtocol(in);
    }

    @Override
    public CustomProtocol[] newArray(int size) {
      return new CustomProtocol[size];
    }
  };

  public void setPileID(String pileID) {
    this.pileID = pileID;
  }

  public String getPileID() {
    return pileID;
  }

  public void setDateDrill(String dateDrill) {
    this.dateDrill = dateDrill;
  }

  public String getDateDrill() {
    return dateDrill;
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(dateDrill);
    parcel.writeString(pileID);
    parcel.writeByte((byte) (cleaned ? 1 : 0));
  }
}

2.Pass clicked protocol from CatalogActivity to EditorActivity using intent,

    mProtocolListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

          Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);

      intent.putExtra("Exiting protocol", EXISTING_PROTOCOL);
      intent.putExtra("Clicked protocol object", protocols.get(position));
           }
    });

3.In editor activity read the clicked protocol though intent.

EXISTING_PROTOCOL = intent.getBooleanExtra("Exiting protocol", false);
protocol = intent.getParcelableExtra("Clicked protocol object");

4.

public void onDataChange(DataSnapshot dataSnapshot) {
                 //WHAT GOES HERE SO THAT I CAN POPULATE THE TEXTVIEWS IN THE ACTIVITY_EDITOR WITH THE EXISTING VALUES?
// Ans: Find text views by id and set corresponding text from protocol read above.
                }}
JTeam
  • 1,455
  • 1
  • 11
  • 16