我想从List
中的Firebase检索数据。为此,我创建了一个用于检索的活动和一个Retrieving
类。从Firebase获取数据时,当我通过按下按钮打开Retrieve
活动时,就会出现问题。它会打开一会儿,移回到上一个活动...
我在另一个项目中使用了相同的代码,因此没有任何问题。我被困住了...
这是我的Retrieve
活动
public class RetrieveTourPlan extends AppCompatActivity {
ListView mylistView;
DatabaseReference db;
EditText search_edit_text;
List<ClassTourPlan> TourPlanList, totalreqSale, TouredArea, TouredCity;
String searchtext;
FirebaseUser firebaseUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrieve_tour_plan);
mylistView = findViewById(R.id.mylisttourplan);
search_edit_text= findViewById(R.id.etSearch);
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
TourPlanList = new ArrayList<>();
totalreqSale = new ArrayList<>();
TouredArea = new ArrayList<>();
TouredCity = new ArrayList<>();
db = FirebaseDatabase.getInstance().getReference("DbTourPlan");
}
@Override
protected void onStart() {
super.onStart();
db.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
TourPlanList.clear();
for (DataSnapshot snapshot:dataSnapshot.getChildren()){
ClassTourPlan classTourPlan=snapshot.getValue(ClassTourPlan.class);
TourPlanList.add(classTourPlan);
}
TourPlanListClass adapter = new TourPlanListClass(RetrieveTourPlan.this, TourPlanList);
mylistView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
与此类
public class TourPlanListClass extends ArrayAdapter<ClassTourPlan> {
private Activity context;
int position;
List<ClassTourPlan> tourPlanList;
public TourPlanListClass(Activity context,List<ClassTourPlan> tourPlanList){
super(context,R.layout.mylistlayout,tourPlanList);
this.context=context;
this.tourPlanList=tourPlanList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater= context.getLayoutInflater();
View listViewItems= inflater.inflate(R.layout.mylistlayout,null,true);
TextView textTourId=listViewItems.findViewById(R.id.tvtourid);
TextView textViewTotalReqSale=listViewItems.findViewById(R.id.tvtotalreqsale);
TextView textViewTouredArea=listViewItems.findViewById(R.id.tvtouredarea);
TextView textViewTouredCity=listViewItems.findViewById(R.id.tvtouredcity);
ClassTourPlan strtingEndingPoint= tourPlanList.get(position);
textTourId.setText(strtingEndingPoint.getClsTourId());
textViewTotalReqSale.setText(strtingEndingPoint.getClstotalreqsale());
textViewTouredArea.setText(strtingEndingPoint.getClstourarea());
textViewTouredCity.setText(strtingEndingPoint.getClstourcity());
return listViewItems;
}
}
我已经从Retrieve
活动中删除了此代码,所以没有问题
for (DataSnapshot snapshot:dataSnapshot.getChildren()){
ClassTourPlan classTourPlan=snapshot.getValue(ClassTourPlan.class);
TourPlanList.add(classTourPlan);
}
这是规则
{
"rules": {
".read": true,
".write": true
}
}
Logcat只显示
11-27 23:03:35.523 30191-30191 / com.example.medicalrepresentative.application E / HAL:负载: id = gralloc!= hmi-> id = gralloc 11-27 23:03:35.583 30191-30264 / com.example.medicalrepresentative.application E / HAL:负载: id = gralloc!= hmi-> id = gralloc 11-27 23:03:35.588 30191-30264 / com.example.medicalrepresentative.application E / HAL:负载: id = gralloc!= hmi-> id = gralloc