2013-06-23 5 views
2
listClass.add(new ListClass(json.getString("ProductId"), json 
          .getString("ProductPrice"))); 
      } 
     list.setAdapter(new MyListAdapter(Fragment1.this, listClass)); 

Mais en classe: MyListAdapter Je reçois une erreur:Fragment au contexte?

public View getView(int position, View convertView, ViewGroup parent) { 
    View rowView = LayoutInflater.from(fragment).inflate(
      R.layout.pricelist, parent, false); 

classe fragment1 étend fragment. Alors que dans rowView = layoutinflater.from (// je ne peux étendre le contexte ici et pas un fragment). Que puis-je faire ??

Mise à jour classe mylistadapter:

public class MyListAdapter extends BaseAdapter { 

    private Fragment fragment; 
    private ArrayList<ListClass> listClass = new ArrayList<ListClass>(); 

    public MyListAdapter(Fragment1 fragment1, ArrayList<ListClass> listClass1) { 
     this.fragment = fragment1; 
     this.listclass = listClass1; 
    } 

    @Override 
    public int getCount() { 
     return listClass.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return ListClass.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View rowView = LayoutInflater.from(fragment).inflate(
       R.layout.pricelist, parent, false); 

     TextView text1 = (TextView) rowView.findViewById(R.id.title); 
     TextView text2 = (TextView) rowView.findViewById(R.id.details); 
     text1.setText(BeanClass.get(position).phoneName); 
     text2.setText(BeanClass.get(position).phonePrice); 
     return rowView; 
    } 

} 
+0

Utilisez ce 'list.setAdapter (nouvelle MyListAdapter (getActivity(), listclass))' – Raghunandan

+0

Désolé i dactylographiées mauvais noms .. réparé (je reçois toujours une erreur). –

+0

Vérifiez les documents http://developer.android.com/reference/android/app/Fragment.html – Raghunandan

Répondre

0

Pour obtenir contexte d'une utilisation de l'activité getActvitiy().

http://developer.android.com/reference/android/app/Fragment.html#getActivity()

public final Activity getActivity() 

Retour l'activité ce fragment est actuellement associé.

list.setAdapter(new MyListAdapter(getActivity(), listClass)); 

Puis dans votre constructeur MyListAdapter

Context mContext; // you can remove this 
    LayoutInflater mInflater; 
    private ArrayList<ListClass> listClass = new ArrayList<ListClass>(); 
    public MyListAdapter(Context context, ArrayList<ListClass> listClass1) 
    { 
    mContext = context; // you can remove this 
    mInflater = LayoutInflater.from(mContext); // get rid of this statement 
    mInflater = LayoutInflater.from(context); // you can directly use context 
    this.listclass = listClass1; 
    } 

Puis, en getView

View rowView = mInflater.inflate(
     R.layout.pricelist, parent, false); 
+0

Merci beaucoup qui a résolu mon problème –

+0

content que cela a aidé. – Raghunandan

+0

@KingOmar pourquoi l'inacceptable? la réponse ne fonctionne pas? – Raghunandan

Questions connexes