Last active
March 7, 2016 22:08
-
-
Save asciimike/39a40c93da7ce4e6f38a to your computer and use it in GitHub Desktop.
Data Binding in 2015: Reactive Programming and Firebase Code Snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends Activity { | |
private ListView mListView; | |
private ArrayList<Message> mMessages; | |
private ArrayAdapter mAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Step -1: Initialize fields | |
mListView = (ListView)findViewById(R.id.listView); | |
mMessages = new ArrayList<Message>(); | |
Firebase.setAndroidContext(this); // Step 0: Set the Android context for Firebase | |
Firebase ref = new Firebase("https://droidcon2015.firebaseio.com"); // Step 1: Connect to a Firebase ref | |
ref.addChildEventListener(new ChildEventListener() { | |
@Override | |
public void onChildAdded(DataSnapshot dataSnapshot, String s) { | |
mMessages.add(dataSnapshot.getValue(Message.class)); // Step 2: Convert DataSnapshot to Message, add Message | |
mAdapter.notifyDataSetChanged(); | |
} | |
@Override | |
public void onChildChanged(DataSnapshot dataSnapshot, String s) { | |
} | |
@Override | |
public void onChildRemoved(DataSnapshot dataSnapshot) { | |
} | |
@Override | |
public void onChildMoved(DataSnapshot dataSnapshot, String s) { | |
} | |
@Override | |
public void onCancelled(FirebaseError firebaseError) { | |
} | |
}); | |
// Step 3: Add new ArrayAdapter that inflates our views | |
mAdapter = new ArrayAdapter<Message>(this, android.R.layout.simple_list_item_2, mMessages) { | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
View row = convertView; | |
if (row == null) { | |
LayoutInflater inflator = LayoutInflater.from(getContext()); | |
row = inflator.inflate(android.R.layout.simple_list_item_2, null); | |
} | |
TextView textView = (TextView) row.findViewById(android.R.id.text1); | |
TextView nameView = (TextView) row.findViewById(android.R.id.text2); | |
Message message = mMessages.get(position); | |
textView.setText(message.getText()); | |
nameView.setText(message.getName()); | |
return row; | |
} | |
}; | |
// Step 4: Connect ArrayAdapter to ListView | |
mListView.setAdapter(mAdapter); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
int id = item.getItemId(); | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivityFirebaseUI extends Activity { | |
private ListView mListView; | |
private FirebaseListAdapter<Message> mFirebaseAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Firebase.setAndroidContext(this); // Step 0: Set the Android context for Firebase | |
Firebase ref = new Firebase("https://droidcon2015.firebaseio.com"); // Step 1: Connect to a Firebase ref | |
// Step 2: Hook up FirebaseListAdapter | |
mFirebaseAdapter = new FirebaseListAdapter<Message>(this, Message.class, android.R.layout.simple_list_item_2, ref) { | |
@Override | |
protected void populateView(View view, Message message) { | |
// Step 3: Populate view with message | |
TextView textView = (TextView) view.findViewById(android.R.id.text1); | |
TextView nameView = (TextView) view.findViewById(android.R.id.text2); | |
textView.setText(message.getText()); | |
nameView.setText(message.getName()); | |
} | |
}; | |
// Step 4: Connect ArrayAdapter to ListView | |
mListView.setAdapter(mFirebaseAdapter); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
int id = item.getItemId(); | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Message { | |
private String name; | |
private String text; | |
public Message() { | |
/* Empty constructor */ | |
} | |
public Message(String name, String text) { | |
this.name = name; | |
this.text = text; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getText() { | |
return text; | |
} | |
public void setText(String text) { | |
this.text = text; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment