وهذا ملف ال Manifest
<?xml version="1.0" encoding="utf-8"?><!-- suppress ALL -->
<manifest xmlns:android="
"
package="com.esoodevelop.genedyonlin">
<uses-permission
android:name="android.permission.INTERNET">
</uses-permission>
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE">
</uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/icgenedy_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/icgenedy_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:fullBackupContent="@xml/backup_descriptor">
<activity android:name=".GenedyOnlin">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GenedyOnlin2" />
<activity android:name=".GenedyOnlin3" />
<activity android:name=".GenedyOnlin5" />
<activity android:name=".MainActivity" />
<activity android:name=".ChateRoom" />
</application>
وهذا ملف الجافا Mainactivty
package com.esoodevelop.genedyonlin;
import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.widget.EditText; import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog dialog = new AlertDialog.Builder(this).create();
final EditText editText = new EditText(this);
dialog.setTitle("ادخل اسمك");
dialog.setView(editText);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = editText.getText().toString();
if (!TextUtils.isEmpty(name)) {
Intent intent = new Intent(MainActivity.this,ChateRoom.class);
intent.putExtra("Name", name);
startActivity(intent);
}else{
Toast.makeText(MainActivity.this, "ادخل اسمك", Toast.LENGTH_SHORT).show();
}
}
});
dialog.show();
}
}
وهذا ملف الجافا الخاص بالدردشة package com.esoodevelop.genedyonlin;
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView;
import com.google.firebase.database.ChildEventListener; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map;
public class ChateRoom extends AppCompatActivity {
private DatabaseReference root;
private String temp_key;
private EditText input_msg;
private ListView listView_chat;
ArrayList<String> list_chat = new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
private String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chate_room);
root = FirebaseDatabase.getInstance().getReference().child("MainChatRoom");
listView_chat = (ListView) findViewById(R.id.listView_chat);
input_msg = (EditText) findViewById(R.id.input_msg);
Button btn_send_msg = (Button) findViewById(R.id.btn_send_msg);
name = getIntent().getExtras().getString("Name");
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list_chat);
listView_chat.setAdapter(arrayAdapter);
btn_send_msg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Map<String, Object> map = new HashMap<>();
temp_key = root.push().getKey();
root.updateChildren(map);
DatabaseReference message_root = root.child(temp_key);
Map<String, Object> map2 = new HashMap<>();
map2.put("name", name);
map2.put("msg", input_msg.getText().toString());
message_root.updateChildren(map2);
}
});
root.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Add_Chat(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Add_Chat(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void Add_Chat(DataSnapshot dataSnapshot) {
Iterator i = dataSnapshot.getChildren().iterator();
input_msg.setText("");
while (i.hasNext()) {
String chat_msg = (String) ((DataSnapshot) i.next()).getValue();
String chat_user_name = (String) ((DataSnapshot) i.next()).getValue();
list_chat.add(chat_user_name + " : " + chat_msg);
arrayAdapter.notifyDataSetChanged();
listView_chat.setSelection(list_chat.size());
}
}
}
الرجاء المساعدة وشكرا
التعليقات