Fragment context error

0

I have a problem, I found a tutorial to create a ListView but it is implemented with extends Activity and I am with extend Fragment and so I have an error in the place marked with an arrow:

    package com.sapires.Joes;

import android.support.v4.app.Fragment;

import com.google.android.gms.internal.im;
import com.sapires.Pizza.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Ementa extends Fragment {

    Context cont;
    String[] titulos;
    String[] descricoes;
    int[] images = { R.drawable.um, R.drawable.dois, R.drawable.tres,
            R.drawable.quatro,R.drawable.cinco,R.drawable.seis };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View android = inflater.inflate(R.layout.ementa_2, container, false);

        Resources res = getResources();
        titulos = res.getStringArray(R.array.titulos);
        descricoes = res.getStringArray(R.array.descricoes);

        ListView list = (ListView) android.findViewById(R.id.listView1);
   -->  VivzAdapter adapter = new VivzAdapter(this, titulos, images, descricoes);
        list.setAdapter(adapter);
        return android;
    }
}

class VivzAdapter extends ArrayAdapter<String> {

    Context context;
    int[] images;
    String[] titleArray;
    String[] descriptionArray;

    VivzAdapter(Context c,String[] titulos, int imgs[], String[] desc) {
        super(c, R.layout.ementa_2, R.id.textView1, titulos);
        this.context = c;
        this.images = imgs;
        this.titleArray = titulos;
        this.descriptionArray = desc;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.ementa_2, parent, false);
        ImageView myImage = (ImageView) row.findViewById(R.id.imageView);
        TextView myTitle = (TextView) row.findViewById(R.id.textView1);
        TextView myDescription = (TextView) row.findViewById(R.id.textView2);

        myImage.setImageResource(images[position]);
        myTitle.setText(titleArray[position]);
        myDescription.setText(descriptionArray[position]);

        return row;
    }
}

The error is: The constructor VivzAdapter(Ementa, String[], int[], String[]) is undefined But changing to extend Activity works, can they help?

    
asked by anonymous 05.08.2014 / 00:22

1 answer

1

The constructor of VivzAdapter asks for a Context .

When you create the instance this way:

VivzAdapter adapter = new VivzAdapter(this, titulos, images, descricoes);  

Activity / strong>, because Activity extends of Context . When Fragment extends of Fragment Fragment > Context

You can get a Context in a Fragment using the getActivity() method. However there are times when getActivity() can return null .

The best way to have a valid Context is to get it in the% method of Fragment :

//Declara uma variável para guardar o context.
private Context context;

@Override
public void onAttach(Activity activity) {
    context = activity;
    super.onAttach(activity);
}

Use the onAttach variable when you need a Context :

VivzAdapter adapter = new VivzAdapter(context, titulos, images, descricoes);
    
05.08.2014 / 00:57