firebase返回空项

时间:2018-03-30 11:30:20

标签: java android firebase

欢迎使用firebase为valeter制作应用程序的人。我目前正在尝试进行一项名为添加工作的活动。活动的第一部分旨在找到您正在进行的护理。我希望通过搜索汽车的标志来做到这一点..

所以我创建了一个名为add_car的活动,其中我将汽车的详细信息添加到我的数据库中。这一切都有效,它使用注册作为主键然后有其他孩子,有助于区分车辆。因此,一旦添加了汽车,这就是我的数据库的外观

汽车 ----->

Kj65YHT(以制造注册为例)  --->

制造商福特

模特嘉年华

门2

enginesize 1600

reg Kj65YHT

数据添加和删除我的数据库罚款,但当我尝试搜索reg后,列表返回约7个空项目。这是我的代码

Add_Job活动

public class Add_Job extends AppCompatActivity implements View.OnClickListener {
private Button btnsubmit;
private EditText etreg;
private FirebaseDatabase db;
private DatabaseReference dbref;
private ListView mlv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add__job);


    mlv = (ListView)findViewById(R.id.mlv);
    btnsubmit = (Button)findViewById(R.id.btnsubmit);
    btnsubmit.setOnClickListener(this);
    etreg = (EditText)findViewById(R.id.etreg);
    db = FirebaseDatabase.getInstance();


}

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.btnsubmit:
locateRegistration();
            break;
    }
}

 private void locateRegistration(){
    String reg = etreg.getText().toString().trim();
    String Cars = "Cars";
    dbref = db.getReference().child(Cars).child(reg);
   HarropChildListener listner = new 
HarropChildListener(mlv,Add_Job.this,dbref);
    dbref.addChildEventListener(listner);
}
}

ChildEventListener类

public class HarropChildListener implements ChildEventListener {
private ListView mlv;

private static ArrayList<String> CAR_COLOUR;
private static ArrayList<String> AMOUNT_DOORS;
private static ArrayList<String> ENGINE_SIZE;
private static ArrayList<String> FUEL_TYPE;
private static ArrayList<String> CAR_MANUFACTURER;
private static ArrayList<String> CAR_MODEL;
private static ArrayList<String> CAR_REGISTRATION;
private Context mContext;
private dataListAdapter apkData;
private DatabaseReference ref;


public HarropChildListener(){

}

public HarropChildListener(ListView mlv,Context context, DatabaseReference dbref){
    this.mlv = mlv;
    this.mContext = context;
    this.ref = dbref;
    Log.i("Event","Started");

    CAR_COLOUR = new ArrayList<>();
    AMOUNT_DOORS = new ArrayList<>();
    ENGINE_SIZE = new ArrayList<>();
    FUEL_TYPE = new ArrayList<>();
    CAR_MANUFACTURER = new ArrayList<>();
    CAR_MODEL = new ArrayList<>();
    CAR_REGISTRATION = new ArrayList<>();

}
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    if (dataSnapshot.exists()) {
        Log.i("Event", "Added");
        String a,b,c,d,e,f,g;
        a = dataSnapshot.child("colour").getValue(String.class);
        b = dataSnapshot.child("doors").getValue(String.class);
        c = dataSnapshot.child("enginesize").getValue(String.class);
        d = dataSnapshot.child("fuel").getValue(String.class);
        e = dataSnapshot.child("manufacturer").getValue(String.class);
        f = dataSnapshot.child("model").getValue(String.class);
        g = dataSnapshot.child("reg").getValue(String.class);

        CAR_COLOUR.add(a);
        AMOUNT_DOORS.add(b);
        ENGINE_SIZE.add(c);
        FUEL_TYPE.add(d);
        CAR_MANUFACTURER.add(e);
        CAR_MODEL.add(f);
        CAR_REGISTRATION.add(g);

        final String[] colours = CAR_COLOUR.toArray(new String[CAR_COLOUR.size()]);
        String[] doors = AMOUNT_DOORS.toArray(new String[AMOUNT_DOORS.size()]);
        String[] engine = ENGINE_SIZE.toArray(new String[ENGINE_SIZE.size()]);
        final String[] man = CAR_MANUFACTURER.toArray(new String[CAR_MANUFACTURER.size()]);
        final String[] model = CAR_MODEL.toArray(new String[CAR_MODEL.size()]);
        final String[] fuel = FUEL_TYPE.toArray(new String[FUEL_TYPE.size()]);
        final String[] reg = CAR_REGISTRATION.toArray(new String[CAR_REGISTRATION.size()]);
        apkData = new dataListAdapter(mContext, colours,fuel,man,model,reg,engine,doors);


        mlv.setAdapter(apkData);
        apkData.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(DatabaseError databaseError) {

}

}

DatalistAdapter类

public class dataListAdapter extends BaseAdapter {
String[] Colour, FuelType;
String[] Manufacturer,Model,Reg,EngineSize ,Doors;
Context mContext;
TextView tv1,tv2,tv3,tv4,tv5,tv6,tv7;

public dataListAdapter() {
    Colour = null;
    FuelType = null;
    Manufacturer = null;
    Model = null;
    Reg = null;
    EngineSize = null;
    Doors = null;
}

public dataListAdapter(Context c, String[] colour, String[] fuel, String[] manufacturer,String[]model,String[]reg,String[]engineSize, String[] doors) {
    Colour = colour;
    FuelType = fuel;
    Manufacturer = manufacturer;
    Model = model;
    Reg = reg;
    EngineSize = engineSize;
    Doors = doors;

    this.mContext = c;




}

public int getCount() {
    // TODO Auto-generated method stub
    return Reg.length;
}

public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {


    final View row;


    if (convertView == null) {

            row = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom, parent, false);

    } else {
        row = convertView;
    }


    tv1 =  row.findViewById(R.id.tvcolour);
    tv2 = row.findViewById(R.id.tvfuel);
    tv3 = row.findViewById(R.id.tvmanufacture);
    tv4 = row.findViewById(R.id.tvmodel);
    tv5 = row.findViewById(R.id.tvreg);
    tv6  = row.findViewById(R.id.tvengine);
    tv7 = row.findViewById(R.id.tvdoors);


    tv1.setText(Colour[position]);
    tv2.setText(FuelType[position]);
    tv3.setText(Manufacturer[position]);
    tv4.setText(Model[position]);
    tv5.setText(Reg[position]);
    tv6.setText(EngineSize[position]);
    tv7.setText(Doors[position]);
    return (row);
}

****** ****编辑 好的家伙经过大量的测试和搞乱这是我在... 如果我从我的数据库参考结束

中删除行reg子项
dbref = db.getReference().child(Cars).child(reg);

喜欢这个

dbref = db.getReference().child(Cars);

它使用我的“汽车”部分中的所有数据填充列表。我只是想要汽车我正在寻找。只是不明白为什么当我搜索孩子(reg)它返回空白

****另一个编辑***** 进一步tersting香港专业教育学院发现,如果添加汽车数据,如果添加额外的孩子为reg进入它工作。所以现在数据库看起来像这样

汽车----&GT;

Kj65YHT(以制造注册为例)  ---&GT; Kj65YHT(这是额外的孩子,我不得不补充说这是错的吗?)  ---&GT;

制造商福特

模特嘉年华

门2

enginesize 1600

reg Kj65YHT

0 个答案:

没有答案