如何将数据库值解析为recyclerview?

时间:2018-09-28 06:58:27

标签: java android android-room

我已经将同等的数据库值输入到房间数据库中,现在我想访问所有对象并将其解析到recyclerview中,但是仅显示第一个对象的数组列表,并不是我犯错的所有对象都可以帮助我。 我的桌子。

@Entity(tableName = "idpDmo")
public class Example implements Serializable {
@PrimaryKey(autoGenerate = true)
private int uid;

@SerializedName("@odata.context")
@Expose
private String odataContext;
@SerializedName("value")
@Expose
@ColumnInfo(name = "value")
private List<Value> value = null;

public String getOdataContext() {
    return odataContext;
}

public void setOdataContext(String odataContext) {
    this.odataContext = odataContext;
}

@TypeConverters(Converters.class)
public List<Value> getValue() {
    return value;
}



public Example(List<Value> value) {
    this.value = value;
}

public int getUid() {
    return uid;
}

public void setUid(int uid) {
    this.uid = uid;
}

}

我的数据库Calass

@Database(entities = {Example.class}, version = 1, exportSchema = false)
@TypeConverters({Converters.class})
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract UserDao userDao();
public static AppDatabase getAppDataBase(Context context) {
    if (INSTANCE == null) {

        INSTANCE = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "user-database")
                .allowMainThreadQueries().build(); }
    return INSTANCE;
}
public static void destroyInstance() {
    INSTANCE = null;
}}

我的ViewModel类。

public class AddBorrowViewModel extends AndroidViewModel {
private final LiveData<List<Example>> itemAndPersonList;

private AppDatabase appDatabase;

public AddBorrowViewModel(Application application) {
    super(application);

    appDatabase = AppDatabase.getAppDataBase(getApplication());
    itemAndPersonList = appDatabase.userDao().getAll();
}


public void addBorrow(final List<Example> borrowModel) {
    new addAsyncTask(appDatabase).execute(borrowModel);

}

private static class addAsyncTask extends AsyncTask<List<Example>, Void, Void> {

    private AppDatabase db;

    addAsyncTask(AppDatabase appDatabase) {
        db = appDatabase;
    }


    @Override
    protected Void doInBackground(List<Example>... arrayLists) {
        db.userDao().saveAll(arrayLists[0]);
        return null;
    }
}

public LiveData<List<Example>> getItemAndPersonList() {

    return itemAndPersonList;
}
}

我的Adpater类。 公共类RecyclerViewAdapter扩展了RecyclerView.Adapter {

private List<Example> borrowModelList;
private Context mContextl;


public RecyclerViewAdapter(List<Example> borrowModelList, Context mContextl) {
    this.borrowModelList = borrowModelList;
    this.mContextl = mContextl;

}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new RecyclerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.idp_list, parent, false));
}

@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
    Example borrowModel = borrowModelList.get(position);
    holder.tvId.setText(borrowModel.getValue().get(position).getFields().getId());
    holder.tvTitle.setText(borrowModel.getValue().get(position).getFields().getId());
    holder.tvLookup.setText(borrowModel.getValue().get(position).getFields().getId());


}

@Override
public int getItemCount() {
    return borrowModelList.size();
}

static class RecyclerViewHolder extends RecyclerView.ViewHolder {

    private TextView tvId;
    private TextView tvTitle;
    private TextView tvLookup;

    public RecyclerViewHolder(View view) {
        super(view);

        tvId = itemView.findViewById(R.id.tvId);
        tvTitle = itemView.findViewById(R.id.tvTitle);
        tvLookup = itemView.findViewById(R.id.tvLookup);
    }
    }

    }

和我的Recyclerview。

     recyclerView = view.findViewById(R.id.recyclerView);
    //recyclerViewAdapter = new RecyclerViewAdapter(new ArrayList<Example> 
     ());
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    recyclerView.setAdapter(recyclerViewAdapter);

    addBorrowViewModel = ViewModelProviders.of(this).get(AddBorrowViewModel.class);

    addBorrowViewModel.getItemAndPersonList().observe(getActivity(), new Observer<List<Example>>() {
        @Override
        public void onChanged(@Nullable List<Example> borrowModelList)
        {

                recyclerViewAdapter = new 
         RecyclerViewAdapter(borrowModelList,getActivity());
                recyclerView.setAdapter(recyclerViewAdapter);

        }
        });

更新问题:主对象内部有内部对象,如何使用此数组列表迭代此内部对象?

0 个答案:

没有答案