我该如何解决这个 String 和 int 问题?
How can I solve this String and int problem?
你看我有一个问题:是否有可能在不完全更改代码的情况下以某种方式将该路径更改为 int?
我正在使用 android studio 并且目前正在开发应用程序,但我正在为一件事而苦苦挣扎。
我有一个图像的字符串路径,它需要是 int 才能工作。
String path1 = Environment.getDataDirectory().getAbsolutePath() + "/data/sk.lotario.bg/app_imgBuns/0.png";
String path2 = Environment.getDataDirectory().getAbsolutePath() + "/data/sk.lotario.bg/app_imgBuns/1.png";
List<SliderBN> sliderBNS = new ArrayList<>();
for(int i = 0; i < Recipe.bnPos; i++){
sliderBNS.add(new SliderBN(path1));
sliderBNS.add(new SliderBN(path2));
在 class SliderBN 我有这个:
public class SliderBN {
private int image;
SliderBN(int image){
this.image = image;
}
public int getImage() {
return image;
}
问题是我不想在此处将其更改为字符串,因为那样我将不得不更改大量代码。
这是 class 的适配器:
private List<SliderBN> sliderBNS;
private ViewPager2 vpBN;
private static final String TAG = "SliderBNAdapter";
public static int vpBNPosition = 0;
public SliderBNAdapter(List<SliderBN> sliderBNS, ViewPager2 vpBN) {
this.sliderBNS = sliderBNS;
this.vpBN = vpBN;
}
public SliderBNAdapter() {
}
@NonNull
@Override
public SliderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new SliderViewHolder(
LayoutInflater.from(parent.getContext()).inflate(
R.layout.slider_item_container,
parent,
false
)
);
}
@Override
public void onBindViewHolder(@NonNull SliderViewHolder holder, int position) {
holder.setImage(sliderBNS.get(position));
if (position == sliderBNS.size()) {
vpBN.post(runnable);
}
}
@Override
public int getItemCount() {
vpBNPosition = vpBN.getCurrentItem();
Log.i(TAG,"position "+vpBNPosition);
return sliderBNS.size();
}
class SliderViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
SliderViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageSlide);
}
void setImage(SliderBN sliderBN){
imageView.setImageResource(sliderBun.getImage());
}
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
sliderBNS.addAll(sliderBNS);
notifyDataSetChanged();
}
};
您可以向SliderBN 添加两个属性。一个是 String path
,另一个是 int index
。当您创建 SliderBN
时,您可以设置您想要的值。然后在你的代码中你可以使用你需要的 属性 。
public class SliderBN {
private int index;
private String path;
SliderBN(String path, int index){
this.index = index;
this.path = path;
}
public int getIndex() {
return index;
}
public String getPath() {
return path;
}
}
一开始创建镜像时可以做:
String pathToImages = Environment.getDataDirectory().getAbsolutePath() + "/data/sk.lotario.bg/app_imgBuns/";
List<SliderBN> sliderBNS = new ArrayList<>();
// Here you need to be sure that you always have the images else you will
// receive exceptions! If this images will be dynamically added/removed then
// you need another way for retrieving the names and loading them in the list.
for(int i = 0; i < Recipe.bnPos; i++){
sliderBNS.add(new SliderBN(pathToImages + i + ".png", i));
}
例如你需要设置图片路径你可以直接调用getPath
方法:
class SliderViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
SliderViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageSlide);
}
void setImage(SliderBN sliderBN){
// Here you can set the path to the image
// I saw that you setImageResource(int resId)
// But for this to work you need the Image ids
// In the current case we can use setImageURI(Uri uri)
// With Uri you need to provide the path to the image not the id
File imgFile = new File(sliderBun.getPath());
if(imgFile.exists())
{
Uri uri = Uri.fromFile(imgFile);
imageView.setImageURI(uri);
}
else
{
// Log here message that the file does not exist
// and also log the path so you can see what is going wrong
}
}
}
注意: 由于您的代码现在是您不需要 index (int) in class SliderBN
因为您在开始添加所有图像的列表中有此信息。我仍然不知道您的所有用例,这就是我离开它的原因。请参阅使用列表索引获取该位置上的对象的示例:
@Override
public void onBindViewHolder(@NonNull SliderViewHolder holder, int position)
{
// Here we get SliderBN object from the list by list position and not by
// internal properties
holder.setImage(sliderBNS.get(position));
if (position == sliderBNS.size()) {
vpBN.post(runnable);
}
}
如果您有 List 未排序 SliderBN
对象并且您需要轻松地对它们进行排序,那么您需要索引的一个地方。然后您可以使用此 属性 index
对该列表中的对象进行排序。但是在您的情况下,您是根据 for loop
内不断增长的索引创建图像的,因此您的列表实际上是经过排序的。
祝大家好运!
你看我有一个问题:是否有可能在不完全更改代码的情况下以某种方式将该路径更改为 int?
我正在使用 android studio 并且目前正在开发应用程序,但我正在为一件事而苦苦挣扎。
我有一个图像的字符串路径,它需要是 int 才能工作。
String path1 = Environment.getDataDirectory().getAbsolutePath() + "/data/sk.lotario.bg/app_imgBuns/0.png";
String path2 = Environment.getDataDirectory().getAbsolutePath() + "/data/sk.lotario.bg/app_imgBuns/1.png";
List<SliderBN> sliderBNS = new ArrayList<>();
for(int i = 0; i < Recipe.bnPos; i++){
sliderBNS.add(new SliderBN(path1));
sliderBNS.add(new SliderBN(path2));
在 class SliderBN 我有这个:
public class SliderBN {
private int image;
SliderBN(int image){
this.image = image;
}
public int getImage() {
return image;
}
问题是我不想在此处将其更改为字符串,因为那样我将不得不更改大量代码。
这是 class 的适配器:
private List<SliderBN> sliderBNS;
private ViewPager2 vpBN;
private static final String TAG = "SliderBNAdapter";
public static int vpBNPosition = 0;
public SliderBNAdapter(List<SliderBN> sliderBNS, ViewPager2 vpBN) {
this.sliderBNS = sliderBNS;
this.vpBN = vpBN;
}
public SliderBNAdapter() {
}
@NonNull
@Override
public SliderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new SliderViewHolder(
LayoutInflater.from(parent.getContext()).inflate(
R.layout.slider_item_container,
parent,
false
)
);
}
@Override
public void onBindViewHolder(@NonNull SliderViewHolder holder, int position) {
holder.setImage(sliderBNS.get(position));
if (position == sliderBNS.size()) {
vpBN.post(runnable);
}
}
@Override
public int getItemCount() {
vpBNPosition = vpBN.getCurrentItem();
Log.i(TAG,"position "+vpBNPosition);
return sliderBNS.size();
}
class SliderViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
SliderViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageSlide);
}
void setImage(SliderBN sliderBN){
imageView.setImageResource(sliderBun.getImage());
}
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
sliderBNS.addAll(sliderBNS);
notifyDataSetChanged();
}
};
您可以向SliderBN 添加两个属性。一个是 String path
,另一个是 int index
。当您创建 SliderBN
时,您可以设置您想要的值。然后在你的代码中你可以使用你需要的 属性 。
public class SliderBN {
private int index;
private String path;
SliderBN(String path, int index){
this.index = index;
this.path = path;
}
public int getIndex() {
return index;
}
public String getPath() {
return path;
}
}
一开始创建镜像时可以做:
String pathToImages = Environment.getDataDirectory().getAbsolutePath() + "/data/sk.lotario.bg/app_imgBuns/";
List<SliderBN> sliderBNS = new ArrayList<>();
// Here you need to be sure that you always have the images else you will
// receive exceptions! If this images will be dynamically added/removed then
// you need another way for retrieving the names and loading them in the list.
for(int i = 0; i < Recipe.bnPos; i++){
sliderBNS.add(new SliderBN(pathToImages + i + ".png", i));
}
例如你需要设置图片路径你可以直接调用getPath
方法:
class SliderViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
SliderViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageSlide);
}
void setImage(SliderBN sliderBN){
// Here you can set the path to the image
// I saw that you setImageResource(int resId)
// But for this to work you need the Image ids
// In the current case we can use setImageURI(Uri uri)
// With Uri you need to provide the path to the image not the id
File imgFile = new File(sliderBun.getPath());
if(imgFile.exists())
{
Uri uri = Uri.fromFile(imgFile);
imageView.setImageURI(uri);
}
else
{
// Log here message that the file does not exist
// and also log the path so you can see what is going wrong
}
}
}
注意: 由于您的代码现在是您不需要 index (int) in class SliderBN
因为您在开始添加所有图像的列表中有此信息。我仍然不知道您的所有用例,这就是我离开它的原因。请参阅使用列表索引获取该位置上的对象的示例:
@Override
public void onBindViewHolder(@NonNull SliderViewHolder holder, int position)
{
// Here we get SliderBN object from the list by list position and not by
// internal properties
holder.setImage(sliderBNS.get(position));
if (position == sliderBNS.size()) {
vpBN.post(runnable);
}
}
如果您有 List 未排序 SliderBN
对象并且您需要轻松地对它们进行排序,那么您需要索引的一个地方。然后您可以使用此 属性 index
对该列表中的对象进行排序。但是在您的情况下,您是根据 for loop
内不断增长的索引创建图像的,因此您的列表实际上是经过排序的。
祝大家好运!