android更新自定义视图

时间:2015-11-15 21:11:46

标签: android view

你好,我是新程序员,我在我的应用程序中有一个自定义视图,我尝试重新绘制自己后,我尝试使用invalidate我尝试使用新数据,但它似乎不起作用我不确定是否问题是我更新数据的方式或我尝试重新绘制视图的方式。任何帮助将不胜感激。

视图:

public class BlackJackView extends View {
private PlayerView playerView, computerView;
private BlackJackGame game;
private Context contex;


public void setPlayer(BlackJackPlayer player) {
    playerView = new PlayerView(contex, player);
}

public void setComputer(BlackJackPlayer computer) {
    computerView = new PlayerView(contex, computer);
}

public BlackJackView(Context context) {
    super(context);
    init(context);
}

public BlackJackView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public BlackJackView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}


private void init(Context context) {
    this.contex = context;
    game = new BlackJackGame();
    playerView = new PlayerView(contex, game.getPlayer());
    computerView = new PlayerView(contex, game.getComputer());

}

public void init(BlackJackGame game) {
    this.game = new BlackJackGame(game.getPlayer().getName(),game.getPlayer().getMoney());
    playerView = new PlayerView(contex, game.getPlayer());
    computerView = new PlayerView(contex, game.getComputer());
    this.invalidate();
}



@Override
public void onDraw(Canvas canvas) {
        playerView.onDraw(canvas);
        computerView.onDraw(canvas);
}

}

活性:

public class BlackJackActivity extends Activity {
private UsersDataSource dataSource;
private List<User> values;
private BlackJackView view;
private BlackJackGame game;
private int userID;
private int playerMoney;
private String playerName;


@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    game = new BlackJackGame();
    view = new BlackJackView(this);
    view.setPlayer(game.getPlayer());


    setContentView(R.layout.black_jack_activity);
    GetId(savedInstanceState);
    GetPlayerData();
    view = (BlackJackView) findViewById(R.id.blackJackView);
    view.init(game);

}

private void GetId(Bundle savedInstanceState) {
    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if (extras == null) {
            userID = -1;
        } else {
            userID = extras.getInt("ID");
        }
    } else {
        userID = (int) savedInstanceState.getSerializable("ID");
    }
}

private void GetPlayerData() {

    //data base open
    dataSource = new UsersDataSource(this);
    dataSource.open();
    //get list of database
    values = dataSource.getAllUsers();
    playerName = values.get(userID).getUsername();
    playerMoney = (int) values.get(userID).getTokens();
}
@Override
protected void onResume() {
    dataSource.open();
    super.onResume();
}

@Override
protected void onPause() {
    dataSource.close();
    super.onPause();
}

}

0 个答案:

没有答案