ListView在项目上不显示文本

时间:2019-06-20 22:02:34

标签: java android xml firebase google-cloud-firestore

我正在尝试实现一个ListView,它将显示Cloud Firestore项目中的集合中存储的某些文档的名称。但是,它仅显示列表中项目数的划分。每个分区均可单击,但没有任何文本。

  1. 对象类:
public class stPopulation {

    public String subtopic;

    public stPopulation(){}

    public stPopulation(String subtopic){

        this.subtopic = subtopic;
    }

    public String getSubtopic(){

        return subtopic;

    }
}
  1. 列表适配器
public class ListAdapter extends ArrayAdapter<stPopulation> {

    public ListAdapter(Context context, ArrayList<stPopulation> object) {
        super(context, 0, object);
    }

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

        if (convertView == null){

            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_subtopic, parent, false);

        }

        stPopulation dir = getItem(position);

        TextView tvSubtopic = convertView.findViewById(R.id.tvSubtopic);

        tvSubtopic.setText(dir.getSubtopic());
        tvSubtopic.setTextColor(Color.RED);
        return convertView;

    }
}

  1. 活动

public class SubTopicsActivity extends AppCompatActivity {

    private ListView sListView;
    TextView tvSubTopic;

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

        final FirebaseFirestore subTopicsDatabase = FirebaseFirestore.getInstance();
        sListView = findViewById(R.id.list_view);

        subTopicsDatabase.getInstance().collection("examTopics").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {

                        ArrayList<stPopulation> subtopicsList = new ArrayList<>();

                        if (task.isSuccessful()) {

                            for (QueryDocumentSnapshot document : task.getResult()) {

                                stPopulation subtopic = document.toObject(stPopulation.class);
                                subtopicsList.add(subtopic);

                            }

                            ListAdapter adapter = new ListAdapter(SubTopicsActivity.this, subtopicsList);
                            sListView.setAdapter(adapter);
                            adapter.notifyDataSetChanged();

                        } else {

                        }
                    }
                });
    }
}

我没有收到任何错误消息,程序运行正常,只是文本(从firestore上的文档名称检索而来)不希望在列表视图中显示。

0 个答案:

没有答案