从SQLite数据库中检索数据并显示到TextView

时间:2014-07-13 00:28:10

标签: android database sqlite

我需要在登录TextView后向Fragment向其他onClick显示数据,但我从NullPointerException数据库检索数据时获得SQLite

这是我的代码:

public Cursor getUserData() {
    String[] userData = new String[] { SqlDbHelper.COL_CODE,
            SqlDbHelper.COL_FNAME, SqlDbHelper.COL_LNAME,
            SqlDbHelper.COL_ADDRESS, SqlDbHelper.COL_LOCATION,
            SqlDbHelper.COL_CONTACT };
    Cursor c = database.query(SqlDbHelper.DATABASE_TABLE, userData, null,
            null, null, null, null);

    if (c != null) {
        c.moveToFirst();
         Bundle bundle = new Bundle();
         bundle.putString("Code",
         c.getString(c.getColumnIndex(SqlDbHelper.COL_CODE)));
         bundle.putString("Firstname",
         c.getString(c.getColumnIndex(SqlDbHelper.COL_FNAME)));
         bundle.putString("Lastname",
         c.getString(c.getColumnIndex(SqlDbHelper.COL_LNAME)));
         bundle.putString("Address",
         c.getString(c.getColumnIndex(SqlDbHelper.COL_ADDRESS)));
         bundle.putString("Location",
         c.getString(c.getColumnIndex(SqlDbHelper.COL_LOCATION)));
         bundle.putString("Contact",
         c.getString(c.getColumnIndex(SqlDbHelper.COL_CONTACT)));
    }
    return c;
}

onClick:

@Override
        public void onClick(View view) {
            // TODO Auto-generated method stub

            sharedPreferences();
            Toast.makeText(getActivity(), "Login Successful", 20).show();

            Log.d("DocLogin", user.getText().toString());
            Log.d("LoginDetails", "username:" + user.getText());
            Log.d("LoginDetails", "password:" + pass.getText());
            Cursor cur = dbcon.getuser_information(user.getText()
                    .toString(), pass.getText().toString());
            if (cur.getCount() != 0) {

                Cursor cursor = dbcon.getDocData();
                if(cursor.getCount() !=0) {
                    FragmentManager fm = getFragmentManager();
                    FragmentTransaction ft = fm.beginTransaction();
                    ft.replace(R.id.content_frame, new DocProfile());
                    ft.commit();
                }
            } else {
                AlertDialog alertDialog = new AlertDialog.Builder(
                        getActivity()).create();
                alertDialog.setTitle("Login Error");
                alertDialog
                        .setMessage("Doctor Code and Password does not match");
                alertDialog.setButton("OK",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                // dismiss dialog
                            }
                        });
                alertDialog.show();
            }

        }
    });

以及我希望使用TextView

显示数据的位置
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.user_profile, container, false);


    dbcon = new SQLController(getActivity());
    dbcon.open();

    TextView fname = (TextView) view.findViewById(R.id.doc_firstname);
    TextView lname = (TextView) view.findViewById(R.id.doc_lastname);
    TextView add = (TextView) view.findViewById(R.id.user_address);
    TextView loc = (TextView) view.findViewById(R.id.user_location);
    TextView con = (TextView) view.findViewById(R.id.user_contact);


    String firstname = "", lastname = "", address = "", location = "", contact = "";

    Bundle bundle = getArguments();

    firstname = bundle.getString("Firstname");
    lastname = bundle.getString("Lastname");
    address = bundle.getString("Address");
    location = bundle.getString("Location");
    contact = bundle.getString("Contact");

    fname.setText(firstname);
    lname.setText(lastname);
    add.setText(address);
    loc.setText(location);
    con.setText(contact);

    return view;
}

它说我在此代码中获得NullPointerException

    firstname = bundle.getString("Firstname");
    lastname = bundle.getString("Lastname");
    address = bundle.getString("Address");
    location = bundle.getString("Location");
    contact = bundle.getString("Contact");

0 个答案:

没有答案