在Android Studio中从Firebase添加ListView中的项目

时间:2016-01-21 16:16:10

标签: android listview firebase firebase-realtime-database

在android中我只是在fireview中添加listview中的一些值,现在我的数据对就像:

enter image description here

我正在使用数组列表在ChildEventListener的帮助下在我的适配器上添加这些项目,这是我的班级:

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {


ListView listView;
ArrayList<String> arr = new ArrayList<>(10);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Firebase.setAndroidContext(this);


    final Firebase myFirebaseRef = new Firebase("https://f.......firebaseio.com/");

    myFirebaseRef.child("Donors").child("Baldia Town").child("Ittehad Town").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            System.out.println(dataSnapshot.getKey() + " was " + dataSnapshot.getValue());
            Log.d("obj", dataSnapshot.getKey() + " , " + dataSnapshot.getValue());

            arr.add(dataSnapshot.getKey());


        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

    listView = (ListView) findViewById(R.id.lvDonnors);
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,arr);
    arrayAdapter.notifyDataSetChanged();
    listView.setAdapter(arrayAdapter);
    listView.setOnItemClickListener(this);
}

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

    TextView text = (TextView) view;
    Toast.makeText(this, text.getText().toString(), Toast.LENGTH_SHORT).show();
}
}

注意:通过这个我只需获取日志:  法扎:2341  侯赛因:1345  Khezar:2233  Zeeshan:1122

但我只想在&#34; arr&#34;中添加名称。 (清单)。当用户按下&#34; zeeshan&#34;我只是在任何变量中得到它的id(datasnapshot.getValue),如1122。 任何人都可以帮我这么做吗?

0 个答案:

没有答案