我目前有片段活动,但我无法让我的适配器正常工作。 在我的片段活动中,我在setAdapter行上有错误。 这是我的片段:
package com.oneapp.tiradentes;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
public class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.activity_main4,container,false);
ArrayList<Carro> carros = new ArrayList<Carro>();
for(int i = 0; i < 5; i++){
Carro carro = new Carro();
carro.setModelo("Carro "+(i + 1));
carro.setMarca("Marca "+(i + 1));
carros.add(carro);
}
ListView lv = (ListView) getView().findViewById(R.id.lv);
lv.setAdapter(new CarroAdapter(this, carros));
return rootView;
}
}
在这一行显示错误:
lv.setAdapter(new CarroAdapter(this, carros));
请有人可以帮助我吗?
我的适配器: 包com.oneapp.tiradentes;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CarroAdapter extends BaseAdapter {
private Context context;
private ArrayList<Carro> lista;
public CarroAdapter(Context context, ArrayList<Carro> lista){
this.context = context;
this.lista = lista;
}
@Override
public int getCount() {
return lista.size();
}
@Override
public Object getItem(int position) {
return lista.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Carro carro = lista.get(position);
View layout;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.carros, null);
}
else{
layout = convertView;
}
TextView modelo = (TextView) layout.findViewById(R.id.t1);
modelo.setText(carro.getModelo());
TextView marca = (TextView) layout.findViewById(R.id.t2);
marca.setText(carro.getMarca());
ImageView iv = (ImageView) layout.findViewById(R.id.iv);
iv.setImageResource(carro.getCarroImagem(position));
if(position % 2 == 0){
layout.setBackgroundColor(Color.YELLOW);
}
return layout;
}
}
错误:
carroAdapter (android.content.Context, ArrayList<Carro>) in
CarroAdapter cannot be applied to (com.oneapp.tiradentes.FragmentA ,
ArrayList<Carro>)
答案 0 :(得分:0)
而不是:
ListView lv = (ListView) getView().findViewById(R.id.lv);
尝试使用:
ListView lv = (ListView) rootView.findViewById(R.id.lv);
因为getView()
将在方法onCreateView
答案 1 :(得分:0)
您的CarroAdapter
将上下文作为第一个参数,并且您正在从片段类发送this
,这意味着您正在发送FragmentA
实例。
将其更改为
lv.setAdapter(new CarroAdapter(getActivity(), carros));
同时尽量避免将Context
作为成员保留,只在
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
你可以用
代替 LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
答案 2 :(得分:0)
在这个地方修改代码:
Utils.LogDebug("Email File Thread took " + timer.ElapsedMilliseconds.ToString() + "(ms)");
这是因为片段不包含任何上下文;你必须在这里传递活动。