Get Activity Context in an Adapter from a Fragment
Asked Answered
K

4

13

I have a Fragment with a ListView. In the adapter I want to create a dialog.

class ViewHolder {
...
  @Override
  public void onClick(View v) {
    ...
    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    ...
   }
...
}

mContext is the Context from my Fragment, which I save global when the adapter is created. I get the error.

unable to add window -- token null is not for an application

The method getActivity() is not available so how to get the Context of my Activity?

Kielce answered 3/6, 2013 at 13:26 Comment(2)
View.getContext() ? ... anyway: which i save global when the adapter is created do you store Context(which is not Application) somehere as global(FSM save us)?Sulphanilamide
i save the context in a global variabel in my adapter, should the fsm save me ? (is fsm the flying spagetthi monster ?)Kielce
T
17

When you are creating your adapter, what are you passing as a context? Try to pass this if you are not doing it. Some more code would be helpful too.

Tumer answered 3/6, 2013 at 13:30 Comment(1)
ah shit i didn't code this part, the fault was the context i get from the fragment is applicationContext and not activity context, sry didn't expect thisKielce
H
28

If you have a custom adapter, change the constructor to require Context as a parameter.

public CustomAdapter(Context context, List<Item> items) {   
}

Then, create an Instance variable to store the context via the constructor.

private Context mContext; //instance variable

public CustomAdapter(Context context, List<Item> items) {
    //some code
    this.mContext= context;
}

And now you can use the variable mContext from anywhere in your adapter.

To create the adapter, simply pass 'this' if created from an activity, or getActivity() if created from a fragment.

mAdapter = new CustomAdapter(this, mArrayItems);

Hope that helps.

Hospice answered 3/6, 2013 at 13:34 Comment(1)
That also right, but when you integrate the LeakCanary with your app, the LeakCanary will catch it and say it may cause Memory Leak, we should remove it. Instead of sent Context like that you can get context by: holder.mView.getContext()Guise
T
17

When you are creating your adapter, what are you passing as a context? Try to pass this if you are not doing it. Some more code would be helpful too.

Tumer answered 3/6, 2013 at 13:30 Comment(1)
ah shit i didn't code this part, the fault was the context i get from the fragment is applicationContext and not activity context, sry didn't expect thisKielce
I
0
class ViewHolder {
...
  @Override
  public void onClick(View v) {
    ...
    AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
    ...
   }
...
}
Iceboat answered 10/4, 2018 at 16:1 Comment(1)
A good answer should contain some explanations as to what you are doing and/ or what the OP was doing wrong.Vaulting
F
0

On my adapter listview

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.custom_list_view, null);
        holder = new ViewHolder();
        holder.lsNama = convertView.findViewById(R.id.lsNama);
        holder.lsUid = convertView.findViewById(R.id.idPartner);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.lsUid.setText(listData.get(position).getUid());
    holder.lsNama.setText(listData.get(position).getNama());

    holder.lsNama.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String uid = listData.get(position).getUid().toString();
            String nama =  listData.get(position).getUid().toString();
            Intent intent = new Intent(view.getContext(), MapsActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("bUid",uid);
            intent.putExtras(bundle);
            view.getContext().startActivity(intent);
        }
    });

    return convertView;
}
Finesse answered 30/4, 2018 at 23:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.