我有一个ListView,我想用它从Firebase检索数据(2个textviews:沙拉名称和沙拉价格每一行)。不幸的是,行总是空的。数据似乎是核心进行 - 行数总是与沙拉节点中的项目数相同。
这是我的activity_adapter_listing.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@android:color/holo_blue_dark"
>
<TextView
android:id="@+id/saladName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_alignParentLeft="true"
android:textColor="@android:color/white"
/>
<TextView
android:id="@+id/saladPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_alignParentRight="true"
android:textColor="@android:color/white"
/>
activity_menu.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/allsalads"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_marginBottom="50dp"
>
</ListView>
ListingAdapter.java:
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class ListingAdapter extends BaseAdapter {
Context context;
LayoutInflater layoutInflater;
ArrayList<Salad> salad;
public ListingAdapter(Context con, ArrayList<Salad> salad)
{
context=con;
layoutInflater = LayoutInflater.from(context);
this.salad=salad;
}
// return number of salads in firebase
@Override
public int getCount() {
return salad.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.activity_adapter_listing, null, false);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.saladName);
holder.price = (TextView) convertView.findViewById(R.id.saladPrice);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Salad salads=salad.get(position);
holder.name.setText(salads.getName());
holder.price.setText(salads.getPrice());
return convertView;
}
public class ViewHolder {
TextView name, price;
}
@Override
public Object getItem(int position) {
return salad.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
我的MenuActivity.java:
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class MenuActivity extends AppCompatActivity {
ListView allsalads;
ProgressDialog mProgressDialog;
DatabaseReference databaseReference= FirebaseDatabase.getInstance().getReference();
ListingAdapter adapter;
ArrayList<Salad> salads=new ArrayList<>();
Button saladBtn, pizzaBtn, drinkBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
allsalads=(ListView)findViewById(R.id.allsalads);
adapter=new ListingAdapter(MenuActivity.this,salads);
allsalads.setAdapter(adapter);
getDataFromServer();
}
public void getDataFromServer()
{
showProgressDialog();
databaseReference.child("Sałatki").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
{
for(DataSnapshot postSnapShot:dataSnapshot.getChildren())
{
Salad salad=postSnapShot.getValue(Salad.class);
salads.add(salad);
adapter.notifyDataSetChanged();
}
}
hideProgressDialog();
}
@Override
public void onCancelled(DatabaseError databaseError) {
hideProgressDialog();
}
});
}
private void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(MenuActivity.this);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.show();
}
private void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
private class ListingAdapter extends BaseAdapter {
Context context;
LayoutInflater layoutInflater;
ArrayList<Salad> salads;
public ListingAdapter(Context con,ArrayList<Salad> salads)
{
context=con;
layoutInflater = LayoutInflater.from(context);
this.salads=salads;
}
@Override
public int getCount() {
return salads.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.activity_adapter_listing, null, false);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.saladName);
holder.price = (TextView) convertView.findViewById(R.id.saladPrice);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Salad salad=salads.get(position);
holder.name.setText(salad.getName());
holder.price.setText(salad.getPrice());
return convertView;
}
public class ViewHolder {
TextView name, price;
}
@Override
public Object getItem(int position) {
return salads.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}}
和Salad.java:
public class Salad {
private String name, price;
Salad(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}}
我不知道我在哪里弄错了。