自定义适配器getView()方法不叫 [英] Custom Adapter getView() method is not called
问题描述
下面是在我设置一个自定义的适配器列表中的片段的code。有没有错误,但列表视图是空的。我已实施 getCount将()
返回我的ArrayList项目的权数。我坚持不好,请help.I没有看到(内部,GetView)在logcat中
片段
公共类ServiceCarListFragment扩展片段{
私人字符串URL;
私人的ArrayList< CarDetail> carDetailList =新的ArrayList< CarDetail>();
私人CarListAdapter适配器;
私人的ListView mList;
@覆盖
公共无效的onCreate(包savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
网址= getActivity()getIntent()getStringExtra(URL)。;
新DownloadCarDetail()执行(URL);
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
// TODO自动生成方法存根
视图V = inflater.inflate(R.layout.fragment_service_car_list,集装箱,假);
mList =(ListView控件)v.findViewById(R.id.list);
mList.setAdapter(适配器);
对于(CarDetail车:carDetailList){
//开始加载图像为每一个学生
car.loadImage(适配器);
}
返回伏;
}
类DownloadCarDetail扩展的AsyncTask<字符串,字符串,ArrayList的< CarDetail>> {
@覆盖
受保护的ArrayList< CarDetail> doInBackground(字符串... PARAMS){
// TODO自动生成方法存根
ArrayList的< CarDetail> carDetailList = JsonParser.parseJson(PARAMS [0]);
返回carDetailList;
}
@覆盖
保护无效onPostExecute(ArrayList中< CarDetail> carDetailList){
// TODO自动生成方法存根
ServiceCarListFragment.this.carDetailList = carDetailList;
Log.d(DCCS,将String.valueOf(ServiceCarListFragment.this.carDetailList.size()));
适配器=新CarListAdapter(getActivity(),ServiceCarListFragment.this.carDetailList);
Log.d(DCCS,将String.valueOf((adapter.getCount())));
}
}
CustomAdapter
公共类CarListAdapter扩展了BaseAdapter {
私人的ArrayList< CarDetail>项目=新的ArrayList< CarDetail>();
私人上下文的背景下;
公共CarListAdapter(上下文的背景下,ArrayList的< CarDetail>项目){
this.context =背景;
this.items =项目;
}
@覆盖
公众诠释getCount将(){
// TODO自动生成方法存根
返回items.size();
}
@覆盖
公共对象的getItem(INT位置){
// TODO自动生成方法存根
返回items.get(位置);
}
@覆盖
众长getItemId(INT位置){
// TODO自动生成方法存根
返回的位置;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
// TODO自动生成方法存根
Log.d(内部,GetView);
LayoutInflater mInflater =(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
ViewHolder支架=无效;
CarDetail车= items.get(位置);
如果(convertView == NULL){
convertView = mInflater.inflate(R.layout.car_list_row,NULL);
持有人=新ViewHolder();
holder.tvCarName =(TextView中)convertView.findViewById(R.id.tvCarName);
holder.tvDailyPriceValue =(TextView中)convertView.findViewById(R.id.tvWeeklyPriceValue);
holder.tvWeeklyPriceValue =(TextView中)convertView.findViewById(R.id.tvWeeklyPriceValue);
holder.imgCar =(ImageView的)convertView.findViewById(R.id.imgCar);
convertView.setTag(保持器);
}
其他 {
支架=(ViewHolder)convertView.getTag();
}
holder.tvCarName.setText(car.getCarName());
如果(car.getImage()!= NULL){
holder.imgCar.setImageBitmap(car.getImage());
} 其他 {
//我的默认图片
holder.imgCar.setImageResource(R.drawable.ic_action_call);
}
返回convertView;
}
静态类ViewHolder {
TextView的tvCarName;
TextView的tvDailyPriceValue;
TextView的tvWeeklyPriceValue;
ImageView的imgCar;
}
}
的唯一原因 getView
不叫有:
-
getCount将
返回0 - 您忘了叫
setListAdapter
在的ListView
。 - 如果在
的ListView
的可见性(或它的容器的可见性)是GONE
。感谢@TaynãBonaldo的宝贵意见
在 onPostExcute
,之后创建 CarListAdapter
的新实例,我会建议您更新了新的实例你的的ListView
。事实上,你需要再次调用
mList.setAdapter(适配器);
Here is the code of the fragment in which I am setting a custom adapter to the list. There no errors but the list view is empty. I have implemented getCount()
which returns right number of items in my arraylist. I am stuck badly please help.I dont see ("Inside", "GetView") in the logcat
Fragment
public class ServiceCarListFragment extends Fragment {
private String url;
private ArrayList<CarDetail> carDetailList = new ArrayList<CarDetail>();
private CarListAdapter adapter;
private ListView mList ;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
url = getActivity().getIntent().getStringExtra("url");
new DownloadCarDetail().execute(url);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment_service_car_list, container,false);
mList = (ListView)v.findViewById(R.id.list);
mList.setAdapter(adapter);
for (CarDetail car : carDetailList) {
// START LOADING IMAGES FOR EACH STUDENT
car.loadImage(adapter);
}
return v;
}
class DownloadCarDetail extends AsyncTask<String, String, ArrayList<CarDetail>>{
@Override
protected ArrayList<CarDetail> doInBackground(String... params) {
// TODO Auto-generated method stub
ArrayList<CarDetail> carDetailList = JsonParser.parseJson(params[0]);
return carDetailList;
}
@Override
protected void onPostExecute(ArrayList<CarDetail> carDetailList) {
// TODO Auto-generated method stub
ServiceCarListFragment.this.carDetailList = carDetailList;
Log.d("dccs", String.valueOf(ServiceCarListFragment.this.carDetailList.size()));
adapter = new CarListAdapter(getActivity(),ServiceCarListFragment.this.carDetailList);
Log.d("dccs", String.valueOf((adapter.getCount())));
}
}
CustomAdapter
public class CarListAdapter extends BaseAdapter {
private ArrayList<CarDetail> items = new ArrayList<CarDetail>();
private Context context;
public CarListAdapter(Context context , ArrayList<CarDetail> items) {
this.context = context;
this.items = items;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.d("Inside", "GetView");
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
ViewHolder holder = null;
CarDetail car = items.get(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.car_list_row, null);
holder = new ViewHolder();
holder.tvCarName = (TextView) convertView.findViewById(R.id.tvCarName);
holder.tvDailyPriceValue = (TextView) convertView.findViewById(R.id.tvWeeklyPriceValue);
holder.tvWeeklyPriceValue = (TextView) convertView.findViewById(R.id.tvWeeklyPriceValue);
holder.imgCar = (ImageView) convertView.findViewById(R.id.imgCar);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvCarName.setText(car.getCarName());
if (car.getImage() != null) {
holder.imgCar.setImageBitmap(car.getImage());
} else {
// MY DEFAULT IMAGE
holder.imgCar.setImageResource(R.drawable.ic_action_call);
}
return convertView;
}
static class ViewHolder {
TextView tvCarName;
TextView tvDailyPriceValue;
TextView tvWeeklyPriceValue;
ImageView imgCar;
}
}
the only reasons getView
is not called are:
getCount
returns 0- you forget to call
setListAdapter
on theListView
. - If the
ListView
's visibility (or its container's visibility) isGONE
. Thanks to @TaynãBonaldo for the valuable input
In the onPostExcute
, after you create a new instance of CarListAdapter
I will suggest you to update the new instance to your ListView
. Indeed you need to call again
mList.setAdapter(adapter);
这篇关于自定义适配器getView()方法不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!