Code examples for week 4

Code for the activity layout file

<TextView

android:id="@+id/tStatus"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:hint="No entries found for January"/>

<LinearLayout

android:id="@+id/lStatus"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/tStatus"

android:layout_marginTop="@dimen/activity_vertical_margin"

android:orientation="horizontal"

android:weightSum="1">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="0.15"

android:text="Income:"/>

<EditText

android:id="@+id/eIncome"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="0.35"

android:hint="1500"

android:inputType="number"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="0.15"

android:text="Expenses:"/>

<EditText

android:id="@+id/eExpenses"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="0.35"

android:hint="1200"

android:inputType="number"/>

</LinearLayout>

<Button

android:id="@+id/bUpdate"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/lStatus"

android:layout_marginTop="@dimen/activity_vertical_margin"

android:onClick="clicked"

android:text="Update"/>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:weightSum="1">

<EditText

android:id="@+id/eSearch"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="0.6"

android:hint="January"/>

<Button

android:id="@+id/bSearch"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="0.4"

android:onClick="clicked"

android:text="Search"/>

</LinearLayout>

Code for creating a new database listener

private void createNewDBListener() {

// remove previous databaseListener

if (databaseReference != null && currentMonth != null && databaseListener != null)

databaseReference.child("calendar").child(currentMonth).removeEventListener(databaseListener);

databaseListener = new ValueEventListener() {

@Override

public void onDataChange(DataSnapshot dataSnapshot) {

// This method is called once with the initial value and again

// whenever data at this location is updated.

MonthlyExpenses monthlyExpense = dataSnapshot.getValue(MonthlyExpenses.class);

// explicit mapping of month name from entry key

monthlyExpense.month = dataSnapshot.getKey();

eIncome.setText(String.valueOf(monthlyExpense.getIncome()));

eExpenses.setText(String.valueOf(monthlyExpense.getExpenses()));

tStatus.setText("Found entry for " + currentMonth);

}

@Override

public void onCancelled(DatabaseError error) {

}

};

// set new databaseListener

databaseReference.child("calendar").child(currentMonth).addValueEventListener(databaseListener);

}