[英]Unable to retrieve data from firebase into list view
I am unable to retrieve real time data from firebase, i followed many sites but nothing solves my problem. 我无法从Firebase检索实时数据,我关注了许多站点,但没有任何解决方案可以解决我的问题。 They were mostly on recycler view, But i want to retrieve my data onto a list view.. Ders no error but my list view is blank when i am running it in emulator. 他们大多在回收站视图上,但是我想将数据检索到列表视图上。没有错误,但是当我在模拟器中运行它时,列表视图为空。 Moreover i cannot use firebase ui because it creates some problems in card view dependency used in my project.. Please help-- 此外,我不能使用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();
}
});*/
}
}
I've been there. 我去过那儿。 I did not go through your whole code, but if you get the data from firebase and add it to your list, you need something like "refresh". 我没有遍历整个代码,但是如果您从firebase中获取数据并将其添加到列表中,则需要“刷新”之类的东西。
What i've done was; 我所做的是;
I added a button on the bottom of my page and when i clicked it restart the intent. 我在页面底部添加了一个按钮,当我单击它时,重新启动了意图。
Pseudo will be like ; 伪会像;
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();
}
This solved my issue, where i got the data but did not display in my listView. 这解决了我的问题,我得到了数据但没有显示在listView中。
You could also test it with ToastMessage if your "food" array is null or not. 如果您的“食物”数组是否为空,也可以使用ToastMessage对其进行测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.