我无法从firebase检索实时数据,我跟随了许多网站,但没有解决我的问题。他们主要是在回收者视图,但我想将我的数据检索到列表视图.. Ders没有错误但我的列表视图是空白当我在模拟器中运行它。此外,我不能使用firebase ui,因为它在我的项目中使用的卡视图依赖项中产生了一些问题..请帮忙 -
package com.serverphone.healthyfoods;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.ListView;
import com.firebase.client.ChildEventListener;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import java.util.ArrayList;
public class AlaCarte extends AppCompatActivity {
ListView lv;
String arr[]=new String[]{"Paneer Butter Masala","Chicken Butter Masala","Chicken Biriyani"};
ArrayList al=new ArrayList();
ArrayList food=new ArrayList();
Button btn;
Firebase mref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ala_carte);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Alacarte!!");
mref=new Firebase("https://healthy-foods-270de.firebaseio.com/Food items");
lv=(ListView)findViewById(R.id.listView);
btn=(Button)findViewById(R.id.button8);
final ArrayAdapter<String> aa=new ArrayAdapter<String>(AlaCarte.this,android.R.layout.simple_list_item_multiple_choice,food);
lv.setAdapter(aa);
mref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//String key=dataSnapshot.getKey();
//String value=dataSnapshot.getValue(String.class);
//String item=key+"-->"+value;
String value=dataSnapshot.getValue(String.class);
food.add(value);
aa.notifyDataSetChanged();
}
@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) {
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
CheckedTextView checkedTextView = ((CheckedTextView) view);
//checkedTextView.setChecked(!checkedTextView.isChecked());
if (checkedTextView.isChecked()) {
checkedTextView.setChecked(false);
al.remove(adapterView.getItemAtPosition(i));
} else {
checkedTextView.setChecked(true);
al.add(adapterView.getItemAtPosition(i));
}
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (al.isEmpty()) {
Snackbar.make(view, "Select check boxes to place your order", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
} else {
Intent i = new Intent(AlaCarte.this, ConfirmAlacarte.class);
i.putExtra("mylist",al);
startActivity(i);
}
}
});
/*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
}
}
答案 0 :(得分:0)
我去过那儿。我没有查看您的整个代码,但是如果您从firebase获取数据并将其添加到列表中,则需要“刷新”等内容。
我做的是什么;
我在页面底部添加了一个按钮,当我点击它时重启意图。
Pseudo就像;
public void Refresh(View view){ //refresh is onClick name given to the button
onRestart();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Intent i = new Intent("YourClass".this, "YourClass".class); //your class
startActivity(i);
finish();
}
这解决了我的问题,我获取了数据但没有显示在我的listView中。
如果你的“food”数组为null,你也可以用ToastMessage测试它。