I want to display data from my DB into a EditText in my fragment.
Here is my coding:
DBhelper
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table Tenant_login(name text, email text primary key, password text)" );
}
Retrive Data
public Cursor alldata() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor= db.rawQuery("Select * from Tenant_login",null);
return cursor;
}
Fragment class
public class profileFragement extends Fragment {
DatabaseHelper db;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.profile_fragment, container, false);
EditText et = (EditText) view.findViewById(R.id.text);
et.setVisibility(view.VISIBLE);
db = new DatabaseHelper(getActivity());
Cursor cursor = db.alldata();
et.setText(""+ cursor.getString(0));
return view;
}
}
This coding is giving me no results. Please Help me out.
You must move the cursor's index to the 1st row (if it exists):
if (cursor.moveToFirst()) {
et.setText("" + cursor.getString(0));
}