Android Room-汇总值并将其放入TextView [英] Android Room - Sum up values and put it in TextView

查看:63
本文介绍了Android Room-汇总值并将其放入TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是一个有点愚蠢的问题,但是我只是被这个困住了:

Maybe this is a tiny bit stupid to ask but I am just stuck with this:

我有我的会议室数据库+实体+道斯.

I have my Room Database + Entities + Daos.

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

The entities have a column called "costs" - so basically I wanna sum up all the costs (of course while tracking if there was a new input, so check if the sum has changed - if so, of course change the value/textview) and then simply put the value with .toString() into a TextView.

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

I already have a RecyclerView+CardView with an Adapter / ViewModel / Repository Class (they put only the Names of the Input into the CardView)

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

I just don't know where to put the costs stuff - I don't think I need a new ViewModel/Repository but where do I put the sumAllCost() method of my Dao

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的活动


The Activity with 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事情


I am really not sure about this whole SUM thing

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

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


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


Maybe someone can help - I feel rather stupid right now sorry! I already search several times for a fitting solution but I just cannot find something that helps me (or that I understand)

推荐答案

您应该为合成列提供别名(它甚至可以不使用别名,但总的来说,访问域聚合的列名会更容易函数结果如下):

you should provide an alias name to the synthetic column (it might even work without that, but in general, it is easier to access column-names of domain aggregate function results like that):

SELECT SUM(costs) AS value FROM subscriptions

,并且当期望查询返回 void (什么都不能)时,应该使用实际返回的数据类型;有关货币值和 LiveData 的信息,请尝试 LiveData< BigDecimal> .文档也有一个相关示例,可能会在更多示例中进行解释.细节.

and when a query is expect to return something void (nothing) cannot be used, that should be the actually returned data-type instead; for monetary values and LiveData, try LiveData<BigDecimal>. the documentation also has one relevant example, which might explain it in more detail.

这篇关于Android Room-汇总值并将其放入TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆