我在ListView
中有一个NestedScrollView
。当我将Firebase中的项目加载到列表中时,屏幕上不会显示任何内容。我认为它不会调用getview方法,否则它至少会显示我设置为自定义布局的默认图像。
当我打印结果时,查询似乎工作正常。
任何帮助?
public class ListAnimalsByHabitatActivity extends AppCompatActivity {
private ListView listView;
private ArrayList<Animal> list = new ArrayList<Animal>();
private String habitat;
private ImageView cover;
private TextView Habitat_name;
FirebaseFirestore db = FirebaseFirestore.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_animals_by_habitat);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listView = (ListView) findViewById(R.id.animal_list_view);
cover = (ImageView) findViewById(R.id.coverimage);
Habitat_name = (TextView) findViewById(R.id.habitatname);
Intent i = getIntent();
habitat = i.getStringExtra("habitat");
String s = i.getStringExtra("cover");
String habitat_name = i.getStringExtra("habitat_name");
Habitat_name.setText(habitat_name);
Glide.with(this).load(s).into(cover);
MyTasks mt = new MyTasks();
mt.execute();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), AnimalViewerActivity.class);
i.putExtra("type", list.get(position).getType());
startActivity(i);
}
});
}
class MyTasks extends AsyncTask{
@Override
protected Object doInBackground(Object[] objects) {
db.collection("Animal")
.whereEqualTo("Habitat", habitat)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
String pic = document.getData().get("Picture").toString();
String name = document.getData().get("Name").toString();
String type = document.getData().get("Type").toString();
String desc = document.getData().get("Description").toString();
list.add(new Animal(habitat, type, name, pic, type));
}
} else {
}
}
});
return null;
}
@Override
protected void onPostExecute(Object o) {
MyNewCustomAdapter ca = new MyNewCustomAdapter(getApplicationContext(), list);
listView.setAdapter(ca);
}
}
class MyNewCustomAdapter extends ArrayAdapter<Animal>{
private Context mContext;
private ArrayList<Animal> array;
public MyNewCustomAdapter(Context context, ArrayList<Animal> array) {
super(context, -1, array);
}
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowview = layoutInflater.inflate(R.layout.petite_list_animal_layout, parent, false);
ImageView im = rowview.findViewById(R.id.IMAGE);
Glide.with(getApplicationContext()).load(list.get(position).getPicture()).into(im);
return rowview;
}
}
@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
}
这是xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="match_parent"
android:background="@android:color/background_light"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/main.appbar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|snap">
<ImageView
android:id="@+id/coverimage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher_background"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/main.toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|snap"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<TextView
android:id="@+id/habitatname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:paddingLeft="20dp"
android:text="Habitat Name"
android:textColor="@android:color/white"
android:textSize="22dp"
android:textStyle="bold" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<ListView
android:id="@+id/animal_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp">
</ListView>
</android.support.v4.widget.NestedScrollView>
答案 0 :(得分:0)
Firebase数据库客户端已在后台线程中运行所有网络操作。这意味着所有操作都可以在不阻塞主线程的情况下进行。将它放在AsyncTask
中并没有带来任何额外的好处。
要解决此问题,只需在onCreate()
方法中使用以下代码行:
db.collection("Animal")
.whereEqualTo("Habitat", habitat)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
String pic = document.getData().get("Picture").toString();
String name = document.getData().get("Name").toString();
String type = document.getData().get("Type").toString();
String desc = document.getData().get("Description").toString();
list.add(new Animal(habitat, type, name, pic, type));
}
} else {
}
}
});
请记住,由于其异步行为,仅在list
方法内定义和使用onComplete()
。如果您想使用list
onComplete()
外ttl
方法,我建议您从 post 中查看我的anwser的最后一部分,并查看此内容的 video 强>