大家好,我是学生,我很高兴再次和你在一起,我正在学习android开发,现在我在处理服务器和数据库的这一章和我在这个应用程序(简单的聊天应用程序)中面临一种错误,让你在firebase数据库中共享文本java代码
package com.example.hfpro.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import java.util.ArrayList;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
Firebase database ;
ArrayList<String> arrayList ;
ListView listView ;
EditText editText ;
ArrayAdapter adapter ;
Firebase chat ;
Map<String , Object> mapRetrived ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Firebase.setAndroidContext(this);
database = new Firebase(MY FIREBASE URL);
chat = database.child("chat") ; // THE NAME OF THE CHILD
listView = (ListView) findViewById(R.id.listView) ;
arrayList = new ArrayList<>() ;
editText = (EditText) findViewById(R.id.textToSend) ;
adapter = new ArrayAdapter<String>(this , android.R.layout.simple_list_item_1 , arrayList);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
protected void onStart() {
super.onStart();
chat.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
arrayList.clear();
mapRetrived = (Map<String, Object>) dataSnapshot.getValue();
String[] strs = (String[]) mapRetrived.values().toArray();
for(String s : strs)
{
arrayList.add(s);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
public void send(View v)
{
String data = editText.getText().toString() ;
//arrayList.add(data);
editText.setText("");
chat.child("chat1").push().setValue(data);
adapter.notifyDataSetChanged();
}
}
并且xml文件非常简单
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hfpro.myapplication.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_above="@+id/textToSend" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textToSend"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/button"
android:layout_toStartOf="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="send"/>
</RelativeLayout>
感谢您阅读和感谢您的时间。