如何将数组从Firestore转换为字符串以在Viewholder TextView中显示

时间:2019-06-06 15:05:04

标签: android firebase android-recyclerview google-cloud-firestore

我对为什么这不起作用感到困惑。我在Firestore中的文档中有一个数组(daysOfWeek),我需要在recyclerview的视图持有器中显示该数组。我从文档中提取的字符串,整数,长整型等都很好,但是我无法弄清楚如何将arraylist转换为字符串。仅在某些情况下,该数组包含用户在一周中选择的日期,我需要相应的textview来显示一周中所有选择的日期。

这是我的适配器代码的一部分:

public class MealPlanDetailsAdapter extends RecyclerView.Adapter<MealPlanDetailsAdapter.MealPlanDetailsViewHolder> {

    private Context adapterContext;
    private ArrayList<MealplanDetails> mealplanDetailsArrayList;

    private final static String TAG = MealPlanDetailsAdapter.class.getSimpleName();

    public MealPlanDetailsAdapter(Context adapterContext, ArrayList<MealplanDetails> mealplanDetailsArrayList) {
        this.mealplanDetailsArrayList = mealplanDetailsArrayList;
        this.adapterContext = adapterContext;
    }

    @NonNull
    @Override
    public MealPlanDetailsAdapter.MealPlanDetailsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater layoutInflater = LayoutInflater.from(adapterContext);
        View view = layoutInflater.inflate(R.layout.mealplan_card, viewGroup, false);
        return new MealPlanDetailsViewHolder(view);
    }

@Override
    public void onBindViewHolder(@NonNull MealPlanDetailsAdapter.MealPlanDetailsViewHolder mpDetailsViewHolder, int position) {

        // Set days of week

        StringBuilder stringBuilder = new StringBuilder();
        for (Object day : mealplanDetailsArrayList.get(position).getDaysOfWeek())
        {
            stringBuilder.append(day.toString());
            stringBuilder.append(" ");
        }

        String finalDays = stringBuilder.toString();
        Log.d(TAG, "days of the week:" + finalDays);
        mpDetailsViewHolder.tvDaysOfWeek.setText(finalDays);

        // Set restaurant
        // Set menu items
    }

   class MealPlanDetailsViewHolder extends RecyclerView.ViewHolder {

        public tvDaysOfWeek

        public MealPlanDetailsViewHolder(@NonNull View itemView) {
            super(itemView);

            tvDaysOfWeek = itemView.findViewById(R.id.days_week);

        }
    }

我的回收站工作正常。适配器没有问题。但是它导致问题的这个数组。堆栈跟踪如下:

Process: com.healthplan, PID: 20051
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.healthplan.MealPlanDetailsAdapter.onBindViewHolder(MealPlanDetailsAdapter.java:65)

1 个答案:

答案 0 :(得分:0)

此问题与Firestore无关。 2019-06-06 16:02:22 ERROR o.s.boot.SpringApplication - Application startup failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: hello.model.League.teams, referenced property unknown: java.util.Set.league at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) at hello.Application.main(Application.java:47) Caused by: org.hibernate.AnnotationException: Unknown mappedBy in: hello.model.League.teams, referenced property unknown: java.util.Set.league at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:147) at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1655) at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1623) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874) at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ... 15 common frames omitted Process finished with exit code 1 为空。这意味着您试图找到TextView的代码行是错误的:

mpDetailsViewHolder.tvDaysOfWeek

您必须弄清楚TextView的位置并正确识别它。