2017-04-12 98 views
0

我使用自定義適配器來實現listview而不是默認的簡單listview。現在,我想添加Firebase功能以使用它的實時數據庫功能。如何爲Listview實現自定義FirebaseAdapter?

我知道實現默認FirebaseAdapter方法的步驟,但是如何使用Firebase實現自定義適配器以用於listview?

代碼簡單FirebaseAdapter:

mRef = new Firebase("https://<myURL>.."); 
    com.firebase.ui.FirebaseListAdapter<String> myAdapter; 
    myAdapter = new FirebaseListAdapter<String>(this,String.class,android.R.layout.simple_list_item_1,mRef) { 
     @Override 
     protected void populateView(View view, String s, int i) { 
      TextView text = (TextView) view.findViewById(android.R.id.text1); 
      text.setText(s); 
     } 
    }; 
    final ListView lv = (ListView) findViewById(R.id.listView); 
    lv.setAdapter(myAdapter); 

回答

0

可以做這樣的事情:

public class MainActivity extends AppCompatActivity { 

    Firebase mRef; 
    com.firebase.ui.FirebaseListAdapter<String> myAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mRef = new Firebase("https://<myURL>.."); 

     myAdapter = new FirebaseListAdapter<String>(this,String.class,android.R.layout.simple_list_item_1,mRef) { 
      @Override 
      protected void populateView(View view, String s, int i) { 
       TextView text = (TextView) view.findViewById(android.R.id.text1); 
       text.setText(s); 
      } 
     }; 
     final ListView lv = (ListView) findViewById(R.id.listView); 
     lv.setAdapter(myAdapter); 


    } 


}