如何检索我的数据库中的所有行?

时间:2017-08-01 07:00:05

标签: android android-fragments android-recyclerview android-sqlite

美好的一天抱歉提出这些问题。希望有人帮我解决我的问题。也希望我的问题足够清楚,以便有人不会投票。我刚接触这个安卓机器。我只是观察并遵循一些教程来完成这些代码并在stackoverflow中询问问题,这时我很难理解。

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

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

这是 log cat响应,用于在 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: --------------------------------------------------------------------

log cat response 用于获取 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

它只返回第1行。我怎样才能获取所有行然后显示所有行列数据?

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;
    }
}

4 个答案:

答案 0 :(得分:0)

使用do while循环

 if (cursor.moveToFirst()) {
                do {
                    Datas data = new DiaryData(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3)
                            , cursor.getString(4));
                    // Adding data to list
                    mMemberDetails.add(data);
                } while (cursor.moveToNext());
            }
     cursor.close();
    return mMemberDetails;

答案 1 :(得分:0)

你传递0因此没有可用的项目所以使用

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

答案 2 :(得分:0)

现在你只是恢复光标的第一个结果。检查你写的这段代码:

if (cursor.getCount() > 0) {
    ...
    mMemberDetails.add(pMemebr);
    ...
}

您只获得第一个且唯一的第一个项目。你需要遍历游标。而不是那样if (cursor.getCount() > 0)考虑改为while (cursor.moveToNext())

您可以在this question

中找到有关游标的更多信息

答案 3 :(得分:0)

首先,您需要在 SQLiteHandler

中迭代 getUserSLDetails 方法
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 中,您需要更改以下方法 -

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

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