0

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

这是我的主要活动代码

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

}
4

3 回答 3

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());
          }
    }
}
于 2013-10-19T15:27:09.393 回答
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());

                    }

                });}
于 2013-10-19T13:46:49.147 回答
0

您没有更新 playerID。

你只是得到一个新的ID。

你应该把

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

这样当你再次点击时,

playerID 得到更新。

于 2013-10-19T13:36:28.650 回答