Advertisement
NelloRizzo

[GTN} Main Activity

Jan 11th, 2018
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.07 KB | None | 0 0
  1. package local.example.guessthenumber;
  2.  
  3. import android.content.Intent;
  4. import android.database.DataSetObserver;
  5. import android.support.annotation.NonNull;
  6. import android.support.annotation.Nullable;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.support.v7.widget.AppCompatButton;
  10. import android.text.Editable;
  11. import android.text.TextWatcher;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.widget.Adapter;
  16. import android.widget.ArrayAdapter;
  17. import android.widget.BaseAdapter;
  18. import android.widget.Button;
  19. import android.widget.EditText;
  20. import android.widget.ListAdapter;
  21. import android.widget.ListView;
  22. import android.widget.TextView;
  23.  
  24. import org.w3c.dom.Text;
  25.  
  26. import java.util.ArrayList;
  27.  
  28. public class MainActivity extends AppCompatActivity
  29.         implements View.OnClickListener {
  30.  
  31.     static class ViewHolder {
  32.         TextView gameAttempts;
  33.         TextView gameMessage;
  34.     }
  35.  
  36.     class Message {
  37.         public final String message;
  38.         public final int attempts;
  39.  
  40.         public Message(String message, int attempts) {
  41.             this.message = message;
  42.             this.attempts = attempts;
  43.         }
  44.     }
  45.  
  46.     private final String TAG = "MAIN_ACTIVITY";
  47.     Game game;
  48.  
  49.     Button btnAttempt;
  50.     EditText etAttempt;
  51.     TextView tvUserName;
  52.     ListView lvResult;
  53.     ArrayList<Message> messages;
  54.  
  55.     @Override
  56.     protected void onCreate(Bundle savedInstanceState) {
  57.         super.onCreate(savedInstanceState);
  58.         setContentView(R.layout.activity_main);
  59.         lvResult = (ListView) findViewById(R.id.lv_result);
  60.         messages = new ArrayList<>();
  61. //        ListAdapter adapt = new ArrayAdapter<String>(
  62. //                this,
  63. //                android.R.layout.simple_list_item_1,
  64. //                messages);
  65.         ListAdapter adapt =
  66.                 new ArrayAdapter<Message>(this, 0, messages) {
  67.  
  68.  
  69.                     @NonNull
  70.                     @Override
  71.                     public View getView(
  72.                             int position,
  73.                             @Nullable View convertView,
  74.                             @NonNull ViewGroup parent) {
  75.                         if (convertView == null) {
  76.                             convertView = getLayoutInflater()
  77.                                     .inflate(R.layout.custom_message, null, false);
  78.                             TextView tvAttempt = (TextView) convertView.findViewById(R.id.tv_cm_attempt);
  79.                             TextView tvMessage = (TextView) convertView.findViewById(R.id.tv_cm_message);
  80.                             ViewHolder holder = new ViewHolder();
  81.                             holder.gameAttempts = tvAttempt;
  82.                             holder.gameMessage = tvMessage;
  83.                             convertView.setTag(holder);
  84.                         }
  85.                         ViewHolder holder = (ViewHolder)convertView.getTag();
  86.                         TextView tvAttempt = holder.gameAttempts;
  87.                         TextView tvMessage = holder.gameMessage;
  88.                         Message message = messages.get(position);
  89.                         tvAttempt.setText("" + message.attempts);
  90.                         tvMessage.setText(message.message);
  91.  
  92.                         return convertView;
  93.                     }
  94.                 };
  95.         lvResult.setAdapter(adapt);
  96.         tvUserName = (TextView) findViewById(R.id.tv_user_name);
  97.  
  98.         Intent parent = getIntent();
  99.         UserInfo user = (UserInfo) parent.getParcelableExtra("user");
  100.         tvUserName.setText(user.userName);
  101.  
  102.         btnAttempt = (Button) findViewById(R.id.btn_attempt);
  103.         btnAttempt.setOnClickListener(this);
  104. //        btnAttempt.setOnClickListener(new View.OnClickListener() {
  105. //            @Override
  106. //            public void onClick(View view) {
  107. //                Log.d(TAG, "onClickListener anonimo");
  108. //            }
  109. //        });
  110.         etAttempt = (EditText) findViewById(R.id.et_attempt);
  111.         etAttempt.addTextChangedListener(new TextWatcher() {
  112.             @Override
  113.             public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  114.             }
  115.  
  116.             @Override
  117.             public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  118.                 btnAttempt.setEnabled(charSequence.length() > 0);
  119.             }
  120.  
  121.             @Override
  122.             public void afterTextChanged(Editable editable) {
  123.  
  124.             }
  125.         });
  126.  
  127.         game = new Game();
  128.     }
  129.  
  130.     protected void attempt(View view) {
  131.         Log.d(TAG, view.toString());
  132.     }
  133.  
  134.     @Override
  135.     public void onClick(View view) {
  136.         Log.d(TAG, "onClickListener");
  137.         CharSequence s = etAttempt.getText();
  138.         int attempt = Integer.parseInt(s.toString());
  139.         int result = game.makeAttempt(attempt);
  140.  
  141.         switch (result) {
  142.             case Game.WIN:
  143.                 addMessage("Hai vinto", false);
  144.                 endGame();
  145.                 break;
  146.             case Game.LESS:
  147.                 addMessage(String.format("Il numero %d è troppo piccolo", attempt));
  148.                 break;
  149.             case Game.GREATER:
  150.                 addMessage(String.format("Il numero %d è troppo grande", attempt));
  151.                 break;
  152.             case Game.GAME_OVER:
  153.                 addMessage(String.format("Hai perso. Il numero da indovinare era %d", game.getTarget()), false);
  154.                 endGame();
  155.                 break;
  156.         }
  157.         etAttempt.selectAll();
  158.         etAttempt.requestFocus();
  159.     }
  160.  
  161.     private void endGame() {
  162.         btnAttempt.setEnabled(false);
  163.         etAttempt.setEnabled(false);
  164.     }
  165.  
  166.     private void addMessage(String message) {
  167.         addMessage(message, true);
  168.     }
  169.  
  170.     private void addMessage(String message, boolean count) {
  171. //        if (count)
  172. //            message = String.format("%d) - %s\n\n", game.getAttempts() - 1, message);
  173.         messages.add(0, new Message(message, game.getAttempts() - 1));
  174.         lvResult.invalidateViews();
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement