我想在Fragment中使用ArrayAdapter设置Listview.Here是我的所有代码。
Fragment :::
public class HairstylesFragment extends Fragment {
ListView HairStylelz;
Integer[] imageId = {
R.drawable.man_menu_buzz,
R.drawable.man_menu_buzz,
R.drawable.man_menu_buzz,
R.drawable.man_menu_buzz,
R.drawable.man_menu_buzz,
R.drawable.man_menu_buzz,
R.drawable.man_menu_buzz
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_hairstyles, container, false);
HairStylelz=(ListView)view.findViewById(R.id.Listview_HairStyle);
HairStylez_Adapter adapter = new HairStylez_Adapter(view.getContext(),imageId);
HairStylelz.setAdapter(adapter);
return view;
}
}
ArrayAdapter ::
public class HairStylez_Adapter extends ArrayAdapter<String> {
Integer[] imageId;
Context context;
public HairStylez_Adapter(Context context, Integer[] imageId) {
super(context, R.layout.listview_adapter_hairstyle);
this.context=context;
this.imageId=imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView= inflater.inflate(R.layout.listview_adapter_hairstyle, null, true);
ImageView imageView = (ImageView) rowView.findViewById(R.id.Hairstyle_imageview);
imageView.setImageResource(imageId[position]);
Log.d("settingup","");
return rowView;
}
}
但Listview没有设置其显示空白屏幕。 我的代码中有没有丢失? 帮助在片段中设置Listview
由于
答案 0 :(得分:1)
getView()方法内部:
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.listview_adapter_hairstyle, parent, true);
}
ImageView imageView = (ImageView) view.findViewById(R.id.Hairstyle_imageview);
imageView.setImageResource(imageId[position]);
Log.d("settingup","");
return view; //You are initialing view and returning rowView which is wrong.
如果视图为null,则应仅对视图进行一次充气。并且在充气时传递父项。
答案 1 :(得分:1)
您需要通过调用add(someValue)
将String数据添加到ArrayAdapter答案 2 :(得分:1)
根据您的使用案例,我建议您使用BaseAdapter并覆盖getCount()
,getItemId(int position)
和getItem(int position)
方法并返回imageId.length,0和imageId [position]。以下是BaseAdapter的示例实现。
public class CustomBaseAdapter extends BaseAdapter {
Integer[] imageId;
Context context;
public CustomBaseAdapter(Context context, Integer[] imageId) {
this.context=context;
this.imageId=imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView= inflater.inflate(R.layout.listview_adapter_hairstyle, null, true);
ImageView imageView = (ImageView) rowView.findViewById(R.id.Hairstyle_imageview);
imageView.setImageResource(getItem(position));
Log.d("settingup","");
return rowView;
}
@Override
public int getCount() {
return imageId.length;
}
@Override
public Integer getItem(int position) {
return imageId[position];
}
@Override
public long getItemId(int position) {
return 0;
}
}