-1

I have an application where I used sqlite as my database in which it has a login and registration. And also a user can view all the accounts. In my view class, I used a fragment but I receive an error that says, "Error:(58, 55) error: incompatible types: _6_ViewAll cannot be converted to Context", in this line

databaseHelper = new DatabaseHelper(_6_ViewAll.this);

How can I resolve this?

Here is my source code:

_6_ViewAll.java

public class _6_ViewAll extends Fragment {

private AppCompatTextView textViewName;
private RecyclerView recyclerViewUsers;
private List<UserInfo> listUsers;
private UsersRecyclerAdapter usersRecyclerAdapter;
private DatabaseHelper databaseHelper;

View myView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

    myView = inflater.inflate(R.layout.activity__6__view_all, container, false);

    textViewName = (AppCompatTextView) getView().findViewById(R.id.textViewName);
    recyclerViewUsers = (RecyclerView) getView().findViewById(R.id.recycleViewUsers);

    initObjects();

    return myView;
}

private void initObjects() {
    listUsers = new ArrayList<>();
    usersRecyclerAdapter = new UsersRecyclerAdapter(listUsers);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    recyclerViewUsers.setLayoutManager(mLayoutManager);
    recyclerViewUsers.setItemAnimator(new DefaultItemAnimator());
    recyclerViewUsers.setHasFixedSize(true);
    recyclerViewUsers.setAdapter(usersRecyclerAdapter);
    databaseHelper = new DatabaseHelper(_6_ViewAll.this);

    String emailFromIntent = getActivity().getIntent().getStringExtra("EMAIL");
    textViewName.setText(emailFromIntent);

    getDataFromSQLite();
}

private void getDataFromSQLite() {
    // AsyncTask is used that SQLite operation not blocks the UI Thread.
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            listUsers.clear();
            listUsers.addAll(databaseHelper.getAllUser());

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            usersRecyclerAdapter.notifyDataSetChanged();
        }
    }.execute();
}
 }

Thank you!

Pate Ortega
  • 83
  • 1
  • 11
  • 1
    databaseHelper = new DatabaseHelper( getContext() ); should work. – Angel Koh Sep 01 '17 at 15:48
  • 1
    `Fragment` class doesn't extend `Context` it's the reason why you cannot convert it but `Activity` does, use `getActivity()` method to get Context. – Vasyl Glodan Sep 01 '17 at 15:49
  • 1
    As @VasylGlodan has stated, a Fragment does not extend the Context class, but a Fragment is displayed by a class (directly or indirectly) that extends or implements a Context object so you can use the getContext() method or you can always get the Activity object that is responsible for displaying the Fragment object with the getActivity() method and have access to all the public features of the Activity. – Clement Osei Tano Sep 01 '17 at 15:54
  • @VasylGlodan: I already use the getActivity() but my view class got my app into a force close – Pate Ortega Sep 01 '17 at 15:57

2 Answers2

1

A Fragment is not a Context

For API level 23 and higher you can use Fragment.getContext():

Otherwise use Fragment.getActivity(). Since you are creating a database class associated with SQLite I recommend using Fragment.getActivity().getApplicationContext().

Example inside your Fragment code:

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (activity != null && databaseHelper != null) {
        databaseHelper = new DatabaseHelper(activity.getApplicationContext());
    }
}

If you want to prepare for deprecation of onAttach(activity) also implement, but do not rely on:

@TargetApi(Build.VERSION_CODES.M)
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    // Calling super.onAttach(context) will also call super.Attach(activty)
    if (context != null) {
        databaseHelper = new DatabaseHelper(context.getApplicationContext());
    }
}

Be fair warned this calls both onAttach methods currently.

Dave Thomas
  • 3,667
  • 2
  • 33
  • 41
0
public class _6_ViewAll extends Fragment {

private AppCompatTextView textViewName;
private RecyclerView recyclerViewUsers;
private List<UserInfo> listUsers;
private UsersRecyclerAdapter usersRecyclerAdapter;
private DatabaseHelper databaseHelper;

View myView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

    myView = inflater.inflate(R.layout.activity__6__view_all, container, false);

    textViewName = (AppCompatTextView) getView().findViewById(R.id.textViewName);
    recyclerViewUsers = (RecyclerView) getView().findViewById(R.id.recycleViewUsers);

    initObjects();

    return myView;
}

private void initObjects() {
    listUsers = new ArrayList<>();
    usersRecyclerAdapter = new UsersRecyclerAdapter(listUsers);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    recyclerViewUsers.setLayoutManager(mLayoutManager);
    recyclerViewUsers.setItemAnimator(new DefaultItemAnimator());
    recyclerViewUsers.setHasFixedSize(true);
    recyclerViewUsers.setAdapter(usersRecyclerAdapter);
    databaseHelper = new DatabaseHelper(getContext()/*or use getActivity()*/);

    String emailFromIntent = getActivity().getIntent().getStringExtra("EMAIL");
    textViewName.setText(emailFromIntent);

    getDataFromSQLite();
}

private void getDataFromSQLite() {
    // AsyncTask is used that SQLite operation not blocks the UI Thread.
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            listUsers.clear();
            listUsers.addAll(databaseHelper.getAllUser());

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            usersRecyclerAdapter.notifyDataSetChanged();
        }
    }.execute();
}
 }

I have edited your class to use the right context. Note that you can however get the Application level context too if you wish to, this comes in handy when you are doing stuff that are not directly related to an activity.