也许这是一个愚蠢的问题,但我只是坚持这一点:

我有房间数据库+实体+道斯。

实体有一个称为“成本”的列-所以基本上我想总结所有成本(当然是在跟踪是否有新的输入时,因此请检查总和是否已更改-如果是的话,当然要更改值/文本视图)然后只需将.toString()的值放入TextView中即可。

我已经有一个带有Adapter / ViewModel / Repository类的RecyclerView + CardView(它们仅将输入名称放到CardView中)

我只是不知道将费用放在哪里-我认为我不需要新的ViewModel / Repository,但是我应该在哪里放置Dao的sumAllCost()方法

public class SubRepository {

private SubDAO mSubDao;
private LiveData<List<Sub>> mAllSubs;

SubRepository(Application application){
    SubDatabase db = SubDatabase.getInstance(application);
    mSubDao = db.getsubDAO();
    mAllSubs = mSubDao.getAllSubs();
}

public LiveData<List<Sub>> getAllSubs(){
    return mAllSubs;
}

public void insert (Sub sub){
    new insertAsyncTask(mSubDao).execute(sub);
}

private static class insertAsyncTask extends AsyncTask<Sub, Void, Void> {
    private SubDAO mAsyncTaskDao;

    insertAsyncTask (SubDAO dao){
        mAsyncTaskDao = dao;
    }

    @Override
    protected Void doInBackground(final Sub... params){
        mAsyncTaskDao.addSub(params[0]);
        return null;
    }
}
}




public class SubViewModel extends AndroidViewModel {

private SubRepository mRepository;

private LiveData<List<Sub>> mallSubs;

public SubViewModel (Application application) {
    super (application);
    mRepository = new SubRepository(application);
    mallSubs = mRepository.getAllSubs();
}

public LiveData<List<Sub>> getAllSubs(){
    return mallSubs;
}

public void insert (Sub sub){
    mRepository.insert(sub);
}}




class HomeSubAdapter extends RecyclerView.Adapter<HomeSubAdapter.ViewHolder> {

List<Sub> subList;
Context context;
private final LayoutInflater mInflater;

public static FragmentManager fragmentManager;

HomeSubAdapter(Context context) {
    mInflater = LayoutInflater.from(context);
}


//Just wanna show the name so only one tv
public class ViewHolder extends  RecyclerView.ViewHolder {
    public TextView main_cv_dummy;
    public ViewHolder(View itemView) {
        super(itemView);
        main_cv_dummy = itemView.findViewById(R.id.main_cv_dummy);


    }
}





@Override
public HomeSubAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View subview = mInflater.inflate(R.layout.snippet_main_cardview, parent, false);
    return new ViewHolder(subview);
}

@Override
public void onBindViewHolder (HomeSubAdapter.ViewHolder holder, final int position) {

    if (subList!= null){
        final Sub current = subList.get(position);
        holder.main_cv_dummy.setText(current.getSubName());

        holder.main_cv_dummy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Fragment currentFrag = ShowInfoFragment.newInstance(current);
                //fragmentManager.beginTransaction().replace(R.id.compLayout, currentFrag).addToBackStack(null).commit();
                Log.d("Clicking: ", current.getSubName() + " was clicked - Listener worked");
            }
        });
    }
    else {
        holder.main_cv_dummy.setText("Create a new Sub!");
    }

}

public void setSubs(List<Sub> subs){
    this.subList = subs;
    notifyDataSetChanged();
}






@Override
public int getItemCount(){
    if(subList!=null){
        return subList.size();
    } else
    {
        return 0;
    }
}




onCreate活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Log.d(TAG, "onCreate: starting");
    middle_vf = (ViewFlipper)findViewById(R.id.middle_vf);

    tv_sum = (TextView) findViewById(R.id.tv_sumCosts);




    mSubViewModel = ViewModelProviders.of(this).get(SubViewModel.class);

    mSubViewModel.getAllSubs().observe(this, new Observer<List<Sub>>() {
        @Override
        public void onChanged(@Nullable List<Sub> subs) {
            mAdapter.setSubs(subs);

        }
    });


    fragmentManager = getSupportFragmentManager();

    //Whenever screen is called - call for recycler view
    mRecyclerView = (RecyclerView) findViewById(R.id.main_rv);
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);




    //give dummy-subs to Adapter to Recycler
    mAdapter = new HomeSubAdapter(this);
    mRecyclerView.setAdapter(mAdapter);


    setupBottomNavigationView();
    setupFAB();

}




我真的不确定这整个SUM的事情

//Get all costs
@Query("SELECT SUM(costs) FROM subscriptions")
void getAllCosts();

//Get all subs
@Query("SELECT * FROM subscriptions")
LiveData<List<Sub>> getAllSubs();




也许有人可以帮忙-我现在很傻,很抱歉!我已经搜索了数次以寻找合适的解决方案,但是我找不到能帮助我(或我理解)的东西

最佳答案

您应该为合成列提供一个别名(它甚至可以在没有别名的情况下工作,但是一般来说,访问域聚合函数结果的列名比较容易):

SELECT SUM(costs) AS value FROM subscriptions


当期望查询返回的内容不能使用void(什么都没有)时,应改为实际返回的数据类型;有关货币值和LiveData,请尝试LiveData<BigDecimal>documentation也有一个相关的示例,可能会更详细地说明。

关于java - Android Room-汇总值并将其放入TextView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54514205/

10-08 23:35