如何检索Firebase数据并创建Android列表视图

时间:2015-11-14 21:46:28

标签: java android listview firebase firebase-realtime-database

我正在尝试从firebase检索数据并从检索到的数据创建一个android列表视图。例如,我想从https://docs-examples.firebaseio.com/web/data检索所有消息,并为每条消息创建一个android列表视图。消息的子节点只是为每个单个列表视图显示的数据。

https://www.firebase.com/docs/android/guide/retrieving-data.html有助于解释受支持的Firebase数据类型,但仍然难以理解。非常感谢您的帮助。

谢谢!

Listview.java

public class ListingsActivity extends ListActivity {
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Firebase.setAndroidContext(this);

        ActionBar actionBar = getActionBar();
        actionBar.hide();

        Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/data/messages/");

        final Map < String, Object > data;

        final List <? > data2;

        // Attach an listener to read the data at our posts reference
        ref.addListenerForSingleValueEvent(new ValueEventListener() {@Override
            public void onDataChange(DataSnapshot snapshot) {
                System.out.println("Data: " + snapshot.getValue());
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("Something went wrong!");
            }
        });

        // Binding Array to ListAdapter
        // this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_listings, R.id.label, firebaseData));

        ListView lv = getListView();

        System.out.println("HERE!!!");

        // listening to single list item on click
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView <? > parent, View view,
            int position, long id) {

                // selected item
                String fproduct_label = ((TextView) view).getText().toString();

                // Launching new Activity on selecting single List Item
                Intent i = new Intent(getApplicationContext(), SingleListView.class);
                // sending data to new activity
                i.putExtra("fproduct_label", fproduct_label);
                startActivity(i);
            }
        });
    }

}

SingleView.java

public class SingleListView extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.single_list_view);

        TextView txtProduct = (TextView) findViewById(R.id.product_label);

        Intent i = getIntent();
        // getting attached intent data
        String product = i.getStringExtra("fproduct_label");
        // displaying selected product name
        txtProduct.setText(product);
    }
}

0 个答案:

没有答案
相关问题