gpt4 book ai didi

android - fragment 中的 ListView 使用 ArrayAdapter 未设置

转载 作者:行者123 更新时间:2023-11-29 17:26:10 24 4
gpt4 key购买 nike

我想在 Fragment 中使用 ArrayAdapter 设置 Listview。这是我的所有代码。

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;
}
}

数组适配器::

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 没有设置其显示空白屏幕。我的代码中有任何遗漏吗?帮助在 fragment 中设置 ListView

谢谢

最佳答案

根据您的用例,我建议您使用 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;
}
}

关于android - fragment 中的 ListView 使用 ArrayAdapter 未设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34553947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com