单击每个按钮显示列表值

时间:2015-01-29 09:22:40

标签: java android list

如何在每次点击按钮时显示列表项。让我们说列表中有4个名字。当我按下它时会显示第一个名字。然后当您按下它时会显示第二个名称,依此类推。

我认为唯一的方法是使用list.get()方法。但我不知道如何使用该方法,以便它知道列表中有多少值,然后显示每个按钮命中。我想我需要使用方法,但我没有运气。

public class ZaidimasActivity extends ZaidejaiActivity {

    public TextView mPlayer;
    public TextView mKlausimas;
    public Button mNext;
    public Button mBack;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zaidimas);
         /** //get the player list from ZaidejaiActivity
        Bundle recdData = getIntent().getExtras();
        String myVal = recdData.getString("playerList"); */

        Intent zaidejuInfo = getIntent();
        Bundle extrasBundle = zaidejuInfo.getExtras();
        final ArrayList<String> players = extrasBundle.getStringArrayList("playerList");

        //show the first players name

        mPlayer = (TextView)findViewById(R.id.ZaidejoVardas);
        players.size();

        mPlayer.setText(players.get(0));




        mNext = (Button)findViewById(R.id.KitasBtn);
        mNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mPlayer.setText(players.get(1));


            }
        });


        mBack = (Button)findViewById(R.id.GryztiMeniuBtn);
        mBack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent gryztiMeniu = new Intent(ZaidimasActivity.this, ZaidejaiActivity.class);
                startActivity(gryztiMeniu);
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

在这里,维护一个用于存储全局数组索引的变量,并在每次单击该按钮时递增它。

private int count = 0; // Global array index. Make it as class field   

final ArrayList<String> players = extrasBundle.getStringArrayList("playerList");



mPlayer = (TextView)findViewById(R.id.ZaidejoVardas);
players.size();

mPlayer.setText(players.get(0));




mNext = (Button)findViewById(R.id.KitasBtn);
mNext.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        count++;
        mPlayer.setText(players.get((count)%players.size())); //Incrementing global count and making sure it never exceeds the players list size


    }
});