如何检索数据库中的所有行? [英] How to retrieve all the rows in my DB?

查看:64
本文介绍了如何检索数据库中的所有行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,很抱歉提出这些问题.希望有人能帮助我解决我的问题.也希望我的问题足够清楚,以至于有人不会对此投反对票.我只是这个机器人的新手.我只看并遵循一些教程来完成这些代码,并在事情变得难以理解时在stackoverflow中询问问题.

Good day sorry for asking these question. Hope somebody help me solve my problem. Also hoping that my question is clear enough so that somebody will not down vote it. I'm just new to this android thing. I only watch and followed some tutorials to get these code done and asking question here in stackoverflow when things gets hard for me to comprehend.

这是我当前的代码,它仅获取第一行,但未在CardView中显示它?

this my current code and it only fetches the 1st row but it didn't display it in my CardView?

我如何获取所有行然后显示所有行的列数据?

How could I fetch all the rows then display all rows column data?

这是用于在 SQLite DB 中插入数据的 log cat响应.

this is the log cat response for inserting the data in SQLite DB.

D/SQLiteHandler: Members's SL Details was successfully inserted: 1
D/SQLiteHandler: Successfully inserted SL Desc:             Test Account 1
D/SQLiteHandler: Successfully inserted Transaction Date:    2015-08-17
D/SQLiteHandler: Successfully inserted Actual Balance:      483.67
D/SQLiteHandler: Successfully inserted Available Balance:   483.67
D/SQLiteHandler: -------------------------------------------------------------------
D/SQLiteHandler: Members's SL Details was successfully inserted: 2
D/SQLiteHandler: Successfully inserted SL Desc:             Test Account 2
D/SQLiteHandler: Successfully inserted Transaction Date:    2015-08-28
D/SQLiteHandler: Successfully inserted Actual Balance:      10129.43
D/SQLiteHandler: Successfully inserted Available Balance:   10129.43
D/SQLiteHandler: --------------------------------------------------------------------

这是获取 SL详细信息

D/SQLiteHandler: List<Datas>) Getting members' SL Description:        Test Account 1
D/SQLiteHandler: List<Datas>) Getting members' SL TR Date:            2015-08-17
D/SQLiteHandler: List<Datas>) Getting members' SL Actual Balance:     483.67
D/SQLiteHandler: List<Datas>) Getting members' SL Available Balance:  483.67
D/SQLiteHandler: -------------------------------------------------------------------------
D/SQLiteHandler: List<Datas>) Members's SL Details data:              gsacmobileportal.activity.Datas@5357143c

它仅返回第一行.如何获取所有行然后显示所有行列数据?

It only returns the 1st row. How could I fetch all the rows then display all rows column data?

SQLiteHandler.java

/**
 * Getting user data from List
 * */
public List<Datas> getUserSLDetails() {

    String selectQuery = "SELECT * FROM " + TABLE_MEMBERS_SLDTL;
    List<Datas>  mMemberDetails =  new ArrayList<>();

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // Move to first row
    cursor.moveToFirst();
    if (cursor.getCount() > 0) {

        Datas pMemebr = new Datas();

        pMemebr.setSL_DESC(cursor.getString(0));
        pMemebr.setTR_DATE(cursor.getString(1));
        pMemebr.setACTUAL_BALANCE(cursor.getString(2));
        pMemebr.setAVAILABLE_BALANCE(cursor.getString(3));

        mMemberDetails.add(pMemebr);

        Log.d(TAG, "List<Datas>) Getting members' SL Description:        " + cursor.getString(0));
        Log.d(TAG, "List<Datas>) Getting members' SL TR Date:            " + cursor.getString(1));
        Log.d(TAG, "List<Datas>) Getting members' SL Actual Balance:     " + cursor.getString(2));
        Log.d(TAG, "List<Datas>) Getting members' SL Available Balance:  " + cursor.getString(3));
        Log.d(TAG, "-------------------------------------------------------------------------");

        Log.d(TAG, "List<Datas>) Members's SL Details data:              " + pMemebr.toString());

    }
    else{
        Log.d("getUserSLDetails()", "member's SL Details data is empty");
    }
    cursor.close();
    db.close();

    return mMemberDetails;

}

Datas.java

/**
 * Created by shifoodew on 7/28/2017.
 */

public class Datas {

    private String SL_DESC;
    private String TR_DATE;
    private String ACTUAL_BALANCE;
    private String AVAILABLE_BALANCE;

    public String getSL_DESC() {
        return SL_DESC;
    }

    public void setSL_DESC(String SL_DESC) {
        this.SL_DESC = SL_DESC;
    }

    public String getTR_DATE() {
        return TR_DATE;
    }

    public void setTR_DATE(String TR_DATE) {
        this.TR_DATE = TR_DATE;
    }

    public String getACTUAL_BALANCE() {
        return ACTUAL_BALANCE;
    }

    public void setACTUAL_BALANCE(String ACTUAL_BALANCE) {
        this.ACTUAL_BALANCE = ACTUAL_BALANCE;
    }

    public String getAVAILABLE_BALANCE() {
        return AVAILABLE_BALANCE;
    }

    public void setAVAILABLE_BALANCE(String AVAILABLE_BALANCE) {
        this.AVAILABLE_BALANCE = AVAILABLE_BALANCE;
    }
}

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private List<Datas> mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {

        public CardView mCardView;
        public TextView account_type;
        public TextView accnt_description;
        public TextView balance_label;
        public TextView account_balance;

        public MyViewHolder(View v) {
            super(v);

            mCardView = (CardView) v.findViewById(R.id.card_view);

            account_type = (TextView) v.findViewById(R.id.lblShareCapital);
            balance_label = (TextView) v.findViewById(R.id.lblAvailableBalance);

            accnt_description = (TextView) v.findViewById(R.id.sl_desc);
            account_balance = (TextView) v.findViewById(R.id.actual_balance);
        }
    }
    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(List<Datas> myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                     int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_item, parent, false);
        // set the view's size, margins, paddings and layout parameters
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }
    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        //holder.account_type.setText(mDataset[position]);
        Datas datas = mDataset.get(position);
        holder.account_balance.setText(datas.getSL_DESC());
        holder.mCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String sl_desc = mDataset.get(position).getSL_DESC();
                String actual_balance = mDataset.get(position).getACTUAL_BALANCE();

                Log.d("CardView", "CardView Clicked: " + sl_desc);
                Log.d("CardView", "CardView Clicked: " + actual_balance);
            }
        });
    }
    @Override
    public int getItemCount() {
        //return mDataset.length;
        return 0;
    }
}

AccountsFragment.java

public class AccountsFragment extends Fragment {
private SQLiteHandler db;
    public AccountsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_accounts, container, false);
        RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
        rv.setHasFixedSize(true);

        SQLiteHandler db = new SQLiteHandler(getActivity());
        MyAdapter adapter = new MyAdapter(db.getUserSLDetails());
        rv.setAdapter(adapter);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);

        return rootView;
    }
}

推荐答案

首先,您需要在 SQLiteHandler

if (cursor.moveToFirst()) {
    do {
          Datas pMemebr = new Datas();

          pMemebr.setSL_DESC(cursor.getString(0));
          pMemebr.setTR_DATE(cursor.getString(1));
          pMemebr.setACTUAL_BALANCE(cursor.getString(2));
          pMemebr.setAVAILABLE_BALANCE(cursor.getString(3));

          mMemberDetails.add(pMemebr);

          Log.d(TAG, "List<Datas>) Getting members' SL Description:        " + cursor.getString(0));
          Log.d(TAG, "List<Datas>) Getting members' SL TR Date:            " + cursor.getString(1));
          Log.d(TAG, "List<Datas>) Getting members' SL Actual Balance:     " + cursor.getString(2));
          Log.d(TAG, "List<Datas>) Getting members' SL Available Balance:  " + cursor.getString(3));
          Log.d(TAG, "-------------------------------------------------------------------------");

          Log.d(TAG, "List<Datas>) Members's SL Details data:              " + pMemebr.toString());
            } while (cursor.moveToNext());
        }
 cursor.close();
 return mMemberDetails;
}

然后在 MyAdapter 中,您需要更改以下方法-

Then in your MyAdapter you need to change the below method -

@Override
public int getItemCount() {
    //return mDataset.length;
    return mdataset.size();
}

请在确认数据正确无误后删除日志.

Please do remove the logs once you have verified that your data is coming correctly.

这篇关于如何检索数据库中的所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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