Search in sources :

Example 1 with Message

use of com.teamplantpower.team_plant_power.Message in project Team-Plant-Power by Alexander1994.

the class MessageBoardActivity method onCreate.

/**
 * Called on the creation of the Message Board activity, iniates login listener and display listener
 * @param savedInstanceState
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_message_board);
    enterInput = (EditText) findViewById(R.id.EnterName);
    nameText = (TextView) findViewById(R.id.Name);
    submit = (Button) findViewById(R.id.Submit);
    // Load User if signed up
    userDbRef.orderByKey().equalTo(User.getId(getApplicationContext())).addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot != null && dataSnapshot.getValue() != null) {
                String name = dataSnapshot.child(User.getId(getApplicationContext())).getValue(String.class);
                u = new User(name);
                nameText.setText(u.getName());
                enterInput.setText("Enter Message");
                loggedIn = true;
            } else {
                u = null;
                nameText.setText(" ");
                enterInput.setText("Enter Name");
                loggedIn = false;
            }
        }

        public void onCancelled(DatabaseError err) {
        }
    });
    // Populate message list
    messageListView = (ListView) findViewById(R.id.List);
    // Set up the List View
    firebaseAdapter = new FirebaseListAdapter<Message>(this, Message.class, android.R.layout.simple_list_item_1, messageDbRef) {

        @Override
        protected void populateView(View v, Message model, int position) {
            TextView contactName = (TextView) v.findViewById(android.R.id.text1);
            String dateStr = firebaseAdapter.getRef(position).getKey();
            contactName.setText(model.getFullDisplayMessage(dateStr));
        }
    };
    messageListView.setAdapter(firebaseAdapter);
}
Also used : User(com.teamplantpower.team_plant_power.User) DatabaseError(com.google.firebase.database.DatabaseError) Message(com.teamplantpower.team_plant_power.Message) TextView(android.widget.TextView) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot) TextView(android.widget.TextView) View(android.view.View) ListView(android.widget.ListView)

Example 2 with Message

use of com.teamplantpower.team_plant_power.Message in project Team-Plant-Power by Alexander1994.

the class MessageBoardActivity method setInput.

/**
 * Sets name for login or message to send depending on whether the user has set a name or not, View param required for generating key attached to name in DB
 * @param v
 */
public void setInput(View v) {
    String input = enterInput.getText().toString();
    Date date = new Date();
    if (loggedIn) {
        Message m = new Message(u.getName(), input);
        messageDbRef.child(Message.createMsgKey()).setValue(m.toMap());
    } else {
        // add user to DB
        userDbRef.child(User.getId(getApplicationContext())).setValue(input);
    }
}
Also used : Message(com.teamplantpower.team_plant_power.Message) Date(java.util.Date)

Aggregations

Message (com.teamplantpower.team_plant_power.Message)2 View (android.view.View)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 DataSnapshot (com.google.firebase.database.DataSnapshot)1 DatabaseError (com.google.firebase.database.DatabaseError)1 ValueEventListener (com.google.firebase.database.ValueEventListener)1 User (com.teamplantpower.team_plant_power.User)1 Date (java.util.Date)1