如何实现prev和next按钮

时间:2013-10-19 13:32:54

标签: android

我想实现2个活动,一个是带有图像和文本的列表视图,现在效果很好。另一个是onClicklistener,当我点击任何ListItem时,它将启动另一个活动以显示更多信息,使用1个前一个按钮和下一个按钮查看Prev信息或下一个。但是按钮有问题。它只能看到前一个和下一个一次。这意味着您无法单击两次下一个按钮。任何建议将不胜感激。

这是我的主要活动代码

package com.example.manchesterunited;


import com.example.manchesterunited.R;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;

import android.view.View;

import android.widget.ListView;

import android.widget.Toast;

public class MainActivity extends ListActivity  {


static PlayerData data = new PlayerData();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


CustomAdapter adapter = new CustomAdapter(this);

setListAdapter(adapter);

 }



public void onListItemClick(ListView l,View v, int pos, long id){
    int playerId = (int)id;
    Toast.makeText(getApplicationContext(),"Selected       "+data.getName(pos),Toast.LENGTH_SHORT).show();

    Intent intent = new Intent(this, InfoActivity.class);
    intent.putExtra("playerId", playerId);
    startActivity(intent);

}
 }

这是第二项活动

package com.example.manchesterunited;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class InfoActivity extends Activity  {

TextView dobText;
TextView pobText;   
TextView internationalText;
Button prevButton;
Button nextButton;
PlayerData data = new PlayerData();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_info);

    dobText = (TextView)findViewById(R.id.textView1);
    pobText = (TextView)findViewById(R.id.textView2);
    internationalText = (TextView)findViewById(R.id.textView3);
    prevButton = (Button)findViewById(R.id.prev);
    nextButton =  (Button)findViewById(R.id.next);
    Intent intent = getIntent();
    final int playerId = intent.getExtras().getInt("playerId");  
     dobText.setText("Birthdate:"+String.valueOf(data.getPlayer(playerId).getDob()));
        pobText.setText("Birthplace:"+String.valueOf(data.getPlayer(playerId).getPob()));
        internationalText.setText("International:"+data.getPlayer(playerId).getInternational());


        prevButton.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        int newId = playerId-1;
                         dobText.setText("Birthdate:"+String.valueOf(data.getPlayer(newId).getDob()));
                            pobText.setText("Birthplace:"+String.valueOf(data.getPlayer(newId).getPob()));
                            internationalText.setText("International:"+data.getPlayer(newId).getInternational());

                    }

                });

                nextButton.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        int newId = playerId+1;
                         dobText.setText("Birthdate:"+String.valueOf(data.getPlayer(newId).getDob()));
                            pobText.setText("Birthplace:"+String.valueOf(data.getPlayer(newId).getPob()));
                            internationalText.setText("International:"+data.getPlayer(newId).getInternational());

                    }

                });}





@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_info, menu);
    return true;
}

}

3 个答案:

答案 0 :(得分:0)

您没有更新playerID。

您刚刚获得一个新ID。

你应该把

int newId = playerId+1;
playerID = newId; //insert this line

这样当你再次点击时,

playerID得到更新。

答案 1 :(得分:0)

您总是只向玩家ID添加1,现在指向下一个项目。但是一旦你重新点击按钮,它将具有通过意图传递的相同玩家ID。所以它再次添加相同的值。创建一个新变量,它将存储当前可见的玩家ID并继续增加它,然后您就可以在之前的情况下浏览下一个,反之亦然。

您无法增加玩家ID,因为它已被声明为final,因此您可以创建一个全局变量,也可以将一个变量置于intent中,并在按钮点击时增加它。

nextButton.setOnClickListener(new OnClickListener(){
                   @Override
                    public void onClick(View v) 
{
                        // TODO Auto-generated method stub
    //make newId global then it will work. And keep on updating it when the button is clicked accordingly.

                         newId = playerId+1;

                         dobText.setText("Birthdate:"+String.valueOf(data.getPlayer(newId).getDob()));
                            pobText.setText("Birthplace:"+String.valueOf(data.getPlayer(newId).getPob()));
                            internationalText.setText("International:"+data.getPlayer(newId).getInternational());

                    }

                });}

答案 2 :(得分:0)

在InfoActivity上试试这个,

private int playerId;
...
...onCreate(...){
    ...
    playerId = intent.getExtras().getInt("playerId");
    prevButton.setOnClickListener(new OnClickListener(){
          @Override
          public void onClick(View v) {
             ...
             playerId--;
             dobText.setText("Birthdate:"+String.valueOf(data.getPlayer(playerId).getDob()));
             pobText.setText("Birthplace:"+String.valueOf(data.getPlayer(playerId).getPob()));
             internationalText.setText("International:"+data.getPlayer(playerId).getInternational());
          }
    }
    nextButton.setOnClickListener(new OnClickListener(){
          @Override
          public void onClick(View v) {
             ...
             playerId++;
             dobText.setText("Birthdate:"+String.valueOf(data.getPlayer(playerId).getDob()));
             pobText.setText("Birthplace:"+String.valueOf(data.getPlayer(playerId).getPob()));
             internationalText.setText("International:"+data.getPlayer(playerId).getInternational());
          }
    }
}