单击按钮以运行图像阵列列表

时间:2016-12-31 20:41:07

标签: java android arraylist

我的图像数组列表代码有问题,他压垮了,我不知道该怎么做。我需要你帮助我找到问题并解决它。我的问题:

@Override
public void onClick(View v) {
    String[] strArr = new String[array_image.size()];
    strArr = array_image.toArray(strArr);

    if (btnNext == v) {

        currentImage++;
        currentImage = currentImage % strArr .length;

        iv.setImageResource(strArr.length[currentImage]);

    } else if (btnBack == v) {
        currentImage--;
        currentImage = (currentImage + strArr .length) % strArr .length;

        iv.setImageResource(strArr[currentImage]);
    }
}
}

以下是所有java代码:

package com.example.hanansanag.mytourneyccreator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ssh on 25/12/2016.
 */

public class Players extends AppCompatActivity implements View.OnClickListener {
    protected Button btnNext, btnBack;
    protected ImageView iv;
    protected List<List<Integer>> array_image = new ArrayList<List<Integer>>();
    int currentImage = 0;



    // int[] pic = {R.drawable.bacelona, R.drawable.athlethiko, R.drawable.arsenak, R.drawable.chelsea,
    // R.drawable.dortmond, R.drawable.bayernunchen, R.drawable.intermilan, R.drawable.realmadrid,
    // R.drawable.ashkelon,R.drawable.city,R.drawable.juventus,R.drawable.psj,R.drawable.milan,R.drawable.leverpool,
    // R.drawable.macabiheifa, R.drawable.macabitelaviv, R.drawable.beitaryeroshlaim, R.drawable.apoelbersheva,};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.team_pic);
        ArrayList<Integer> array_image = new ArrayList<Integer>();
        array_image.add(R.drawable.bacelona);
        array_image.add(R.drawable.athlethiko);
        array_image.add(R.drawable.arsenak);
        array_image.add(R.drawable.chelsea);
        array_image.add(R.drawable.dortmond);
        array_image.add(R.drawable.city);
        array_image.add(R.drawable.bayernunchen);
        array_image.add(R.drawable.intermilan);
        array_image.add(R.drawable.psj);
        array_image.add(R.drawable.realmadrid);
        array_image.add(R.drawable.leverpool);
        array_image.add(R.drawable.milan);
        array_image.add(R.drawable.juventus);
        array_image.add(R.drawable.ashkelon);
        array_image.add(R.drawable.macabiheifa);
        array_image.add(R.drawable.macabitelaviv);
        array_image.add(R.drawable.beitaryeroshlaim);
        array_image.add(R.drawable.apoelbersheva);

        iv = (ImageView) findViewById(R.id.imageView);
        btnNext = (Button) findViewById(R.id.btnNextPic);
        btnBack = (Button) findViewById(R.id.btnBeckPic);
        btnBack.setOnClickListener(this);
        btnNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String[] strArr = new String[array_image.size()];
        strArr = array_image.toArray(strArr);

        if (btnNext == v) {

            currentImage++;
            currentImage = currentImage % strArr .length;

            iv.setImageResource(strArr.length[currentImage]);

        } else if (btnBack == v) {
            currentImage--;
            currentImage = (currentImage + strArr .length) % strArr .length;

            iv.setImageResource(strArr[currentImage]);
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

替换

currentImage = (currentImage + strArr .length) % strArr .length;
iv.setImageResource(strArr[currentImage]);

通过

iv.setImageResource(array_image.get(currentImage));

你可以看看这个 Changing Imageview on button dynamically