将Firebase子级添加到Android中的ListView

时间:2017-06-01 11:24:49

标签: android listview firebase firebase-realtime-database

我有一个ListView,目前只显示mapName,但我想将其他Firebase值发送到适配器。

其他值在这里:

String mapStartLat = person.getStartLat();
String mapStartLong = person.getStartLong();
String mapEndLat = person.getEndLat();
String mapEndLong = person.getEndLong();

我该怎么做?代码如下:

MainActivity

package uk.co.jaunt_app.jaunt;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.crashlytics.android.answers.Answers;
import com.crashlytics.android.answers.CustomEvent;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

import static uk.co.jaunt_app.jaunt.R.id.Posts;

public class MainActivity extends AppCompatActivity{

    private FloatingActionButton addmap;
    private Button settingbtn;
    private TextView mNameTextView;
    private TextView mEmailTextView;
    ListView mPosts;
    ArrayAdapter<String> adapter;
    private FirebaseAuth auth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setLogo(R.drawable.jauntlogodark);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        setContentView(R.layout.activity_main);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);

        Firebase.setAndroidContext(this);

        Firebase ref = new Firebase(Config.FIREBASE_URL);
        FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        String uid = currentFirebaseUser.getUid();
        Firebase usersRef = ref.child("Users").child(uid).child("name");
        Firebase emailRef = ref.child("Users").child(uid).child("email");
        Firebase postRef = ref.child("Users").child(uid).child("Maps");

        mPosts = (ListView) findViewById(Posts);
        mPosts.setAdapter(adapter);


        postRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
               for (final DataSnapshot postSnapshot : snapshot.getChildren()) {
                    //Getting the data from snapshot

                   Person person = postSnapshot.getValue(Person.class);
                   String mapName = person.getMapName();
                   String mapStartLat = person.getStartLat();
                   String mapStartLong = person.getStartLong();
                   String mapEndLat = person.getEndLat();
                   String mapEndLong = person.getEndLong();


                   adapter.add(mapName);

                   Intent intent = new Intent(MainActivity.this, ViewUserMapActivity.class);
                   intent.putExtra("mapName", mapName.toString());
                   startActivity(intent);
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                Toast.makeText(getApplicationContext(),
                        "Cancelled", Toast.LENGTH_LONG)
                        .show();
            }
        });

        mPosts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

            }
        });

        mNameTextView = (TextView) findViewById(R.id.NameTextView);

        usersRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                String name = dataSnapshot.getValue(String.class);
                mNameTextView.setText(name);

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

        mEmailTextView = (TextView) findViewById(R.id.EmailTextView);

        emailRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                String email = dataSnapshot.getValue(String.class);
                mEmailTextView.setText(email);

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });



        addmap = (FloatingActionButton) findViewById(R.id.add_map);
        addmap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Answers.getInstance().logCustom(new CustomEvent("Added Map"));
                Intent intent = new Intent(MainActivity.this, MapStartActivity.class);
                startActivity(intent);
            }
        });

        settingbtn = (Button) findViewById(R.id.settingbtn);
        settingbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
                startActivity(intent);
            }
        });

    }

}

1 个答案:

答案 0 :(得分:1)

使用自定义适配器。顺便说一句:用RecyclerView替换你的ListView以获得更好的性能。您可以在网上或此处找到教程:

http://www.androidhive.info/2016/01/android-working-with-recycler-view/