Rasmus Rasmus - 3 months ago 13
Android Question

Firebase onClick list in android

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 im stuck. I guess that i must add something more to the databasereference in the EditorActivity, but i cannot figure out what? Since i dont 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

Answer Source

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.
                }}