Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package local.example.guessthenumber;
- import android.content.Intent;
- import android.database.DataSetObserver;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.support.v7.widget.AppCompatButton;
- import android.text.Editable;
- import android.text.TextWatcher;
- import android.util.Log;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Adapter;
- import android.widget.ArrayAdapter;
- import android.widget.BaseAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- import org.w3c.dom.Text;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity
- implements View.OnClickListener {
- static class ViewHolder {
- TextView gameAttempts;
- TextView gameMessage;
- }
- class Message {
- public final String message;
- public final int attempts;
- public Message(String message, int attempts) {
- this.message = message;
- this.attempts = attempts;
- }
- }
- private final String TAG = "MAIN_ACTIVITY";
- Game game;
- Button btnAttempt;
- EditText etAttempt;
- TextView tvUserName;
- ListView lvResult;
- ArrayList<Message> messages;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- lvResult = (ListView) findViewById(R.id.lv_result);
- messages = new ArrayList<>();
- // ListAdapter adapt = new ArrayAdapter<String>(
- // this,
- // android.R.layout.simple_list_item_1,
- // messages);
- ListAdapter adapt =
- new ArrayAdapter<Message>(this, 0, messages) {
- @NonNull
- @Override
- public View getView(
- int position,
- @Nullable View convertView,
- @NonNull ViewGroup parent) {
- if (convertView == null) {
- convertView = getLayoutInflater()
- .inflate(R.layout.custom_message, null, false);
- TextView tvAttempt = (TextView) convertView.findViewById(R.id.tv_cm_attempt);
- TextView tvMessage = (TextView) convertView.findViewById(R.id.tv_cm_message);
- ViewHolder holder = new ViewHolder();
- holder.gameAttempts = tvAttempt;
- holder.gameMessage = tvMessage;
- convertView.setTag(holder);
- }
- ViewHolder holder = (ViewHolder)convertView.getTag();
- TextView tvAttempt = holder.gameAttempts;
- TextView tvMessage = holder.gameMessage;
- Message message = messages.get(position);
- tvAttempt.setText("" + message.attempts);
- tvMessage.setText(message.message);
- return convertView;
- }
- };
- lvResult.setAdapter(adapt);
- tvUserName = (TextView) findViewById(R.id.tv_user_name);
- Intent parent = getIntent();
- UserInfo user = (UserInfo) parent.getParcelableExtra("user");
- tvUserName.setText(user.userName);
- btnAttempt = (Button) findViewById(R.id.btn_attempt);
- btnAttempt.setOnClickListener(this);
- // btnAttempt.setOnClickListener(new View.OnClickListener() {
- // @Override
- // public void onClick(View view) {
- // Log.d(TAG, "onClickListener anonimo");
- // }
- // });
- etAttempt = (EditText) findViewById(R.id.et_attempt);
- etAttempt.addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- }
- @Override
- public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- btnAttempt.setEnabled(charSequence.length() > 0);
- }
- @Override
- public void afterTextChanged(Editable editable) {
- }
- });
- game = new Game();
- }
- protected void attempt(View view) {
- Log.d(TAG, view.toString());
- }
- @Override
- public void onClick(View view) {
- Log.d(TAG, "onClickListener");
- CharSequence s = etAttempt.getText();
- int attempt = Integer.parseInt(s.toString());
- int result = game.makeAttempt(attempt);
- switch (result) {
- case Game.WIN:
- addMessage("Hai vinto", false);
- endGame();
- break;
- case Game.LESS:
- addMessage(String.format("Il numero %d è troppo piccolo", attempt));
- break;
- case Game.GREATER:
- addMessage(String.format("Il numero %d è troppo grande", attempt));
- break;
- case Game.GAME_OVER:
- addMessage(String.format("Hai perso. Il numero da indovinare era %d", game.getTarget()), false);
- endGame();
- break;
- }
- etAttempt.selectAll();
- etAttempt.requestFocus();
- }
- private void endGame() {
- btnAttempt.setEnabled(false);
- etAttempt.setEnabled(false);
- }
- private void addMessage(String message) {
- addMessage(message, true);
- }
- private void addMessage(String message, boolean count) {
- // if (count)
- // message = String.format("%d) - %s\n\n", game.getAttempts() - 1, message);
- messages.add(0, new Message(message, game.getAttempts() - 1));
- lvResult.invalidateViews();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement