列表视图自动重新加载而其他一些在Firebase数据库表中添加数据

时间:2017-11-14 05:59:59

标签: android firebase firebase-realtime-database

我想创建一个可以进行在线对话的应用。我是通过使用def leave_review(request, pk): if request.method == 'POST': # Passing initial data to the form so our `agent` field will be populated data = {'agent': Agent.objects.get(pk=pk)} form = ReviewForm(request.POST, initial=data) if form.is_valid(): review.save() return redirect('review_success.html') else: form = ReviewForm() return render(request, 'leave_review.html', {'form': form}) 来实现的。我连接Firebase并且能够在多个设备的数据库表中添加数据。 插入问题数据仅在将其插入数据库表的设备中可见。其他设备无法自动查看。当该设备在数据库表中插入新数据时,它将可见。我在下面发送我的代码。请看看。

Activity_main

Firebase

MainActivity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.swatin.firebaselistviewsaveretriveshow.MainActivity">


<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="openAddArea"
    android:id="@+id/topbutton"
    android:onClick="openBellowtopbutton"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bellowtopbutton"
    android:orientation="horizontal"
    android:layout_below="@+id/topbutton"
    android:visibility="visible"
    >

    <EditText
        android:layout_width="0dp"
        android:layout_weight="5"
        android:layout_height="wrap_content"
        android:id="@+id/nameEditText"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="add"
        android:id="@+id/saveBtn"/>

</LinearLayout>



<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/bellowtopbutton"
    android:id="@+id/list"
    />

</RelativeLayout>

航天器

public class MainActivity extends AppCompatActivity {

ListView lv;
ArrayAdapter<String> adapter;
DatabaseReference db;
FirebaseHelper helper;
EditText nameEditTxt;
Button saveBtn;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv= (ListView) findViewById(R.id.list);
    db= FirebaseDatabase.getInstance().getReference();
    helper=new FirebaseHelper(db,this);
    nameEditTxt = (EditText) findViewById(R.id.nameEditText) ;
    saveBtn = (Button) findViewById(R.id.saveBtn);

    saveBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //GET DATA
            String name=nameEditTxt.getText().toString();
            //SET DATA
            Spacecraft s=new Spacecraft();
            s.setName(name);
            //VALIDATE
            if(name.length()>0 && name != null)
            {
                if(helper.save(s))
                {
                    nameEditTxt.setText("");
                    adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,helper.retrieve());
                    lv.setAdapter(adapter);
                }
            }else
            {
                Toast.makeText(MainActivity.this, "Name cannot be empty", Toast.LENGTH_SHORT).show();
            }
        }
    });

    adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,helper.retrieve());
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, helper.retrieve().get(position), Toast.LENGTH_SHORT).show();
        }
    });


}
public void openBellowtopbutton(View view)
{
    LinearLayout bellowtopbutton = (LinearLayout) findViewById(R.id.bellowtopbutton);
    if(bellowtopbutton.getVisibility() == View.VISIBLE)
    {
        bellowtopbutton.setVisibility(View.GONE);
    }
    else
    {
        bellowtopbutton.setVisibility(View.VISIBLE);
    }
 }
}

FirebaseHelper

public class Spacecraft {
String name;
public Spacecraft() {
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
 }
}

0 个答案:

没有答案