am_dot_com

DDM 20211027

Oct 27th, 2021 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.72 KB | None | 0 0
  1. package com.joythis.android.motivator;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Context;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.AdapterView;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.Button;
  12. import android.widget.ListView;
  13. import android.widget.SeekBar;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import java.util.ArrayList;
  18.  
  19. public class MainActivity extends AppCompatActivity {
  20.     public final static String STAMP = "@MainActivity";
  21.     /*
  22.     discussion about String[] (static arrays) vs. ArrayList<String>
  23.     discussion strings in XML (better) vs. strings in Java (recompilation req)
  24.      */
  25.     //ArrayList<String> mAlPositiveMsgs; //e.g. if the app was to support user defined messages
  26.     //ArrayList<String> mAlNegativeMsgs;
  27.     String[] mAlPositiveMsgs;
  28.     String[] mAlNegativeMsgs;
  29.  
  30.     Context mContext; //permanent access to the Activity's context
  31.     SeekBar mSbProbPositiveMsgs;
  32.     Button mBtnGetMsg;
  33.     TextView mTvMsgs;
  34.     AmUtil mUtil; //to bridge to the tools @AmUtil
  35.  
  36.     //to have a working ListView we'll also need a "data source" and an "adapter"
  37.     ListView mLvMsgs; //null - initial value set @init (called on onCreate)
  38.     //data src - these will be the messages presented to the user
  39.     ArrayList<String> mAlMsgs;
  40.     ArrayAdapter<String> mAd;//Adapter
  41.  
  42.     void displayMessage (String pStr){
  43.         if (mTvMsgs!=null){
  44.             //v1 - a TextView is used to display the message
  45.             String strAll = mTvMsgs.getText().toString();
  46.             mTvMsgs.setText(pStr+"\n"+strAll);
  47.         }//if
  48.         if (mLvMsgs!=null){
  49.             //v2 - a ListView is used to display the message
  50.             //TODO: display the new message in the ListView
  51.             mAlMsgs.add(0, pStr); //stack push - insert @head - @top
  52.             //mAlMsgs.add(pStr); //message appears at the end
  53.             mAd.notifyDataSetChanged();
  54.         }//if
  55.     }//displayMessage
  56.  
  57.     AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
  58.         @Override
  59.         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  60.             //this (beter)
  61.             String strInItem = parent.getItemAtPosition(position).toString();
  62.             //or this
  63.             TextView tv = (TextView)view;
  64.             strInItem = tv.getText().toString();
  65.            
  66.             String strReady =
  67.                     String.format("@pos %d : %s", position, strInItem);
  68.  
  69.             mUtil.fb(strReady);
  70.         }//onItemClick
  71.     };//mItemClickListener
  72.  
  73.     View.OnClickListener mBtnClickHandler = new View.OnClickListener() {
  74.         @Override
  75.         public void onClick(View v) {
  76.             /*
  77.             pay attention to the user's probability of positive msg
  78.              */
  79.             int iUserProbability = mSbProbPositiveMsgs.getProgress(); //0-100
  80.             int iProb = AmUtil.randomInt(0, 100);
  81.             boolean bPositiveMsg = iProb <= iUserProbability;
  82.             boolean bCaution = mAlPositiveMsgs!=null && mAlNegativeMsgs!=null
  83.                     && mAlNegativeMsgs.length>0
  84.                     && mAlPositiveMsgs.length>0;
  85.  
  86.             if (bCaution){
  87.                 if (bPositiveMsg){
  88.                     //?
  89.                     int idxRandom = AmUtil.randomInt(
  90.                             0
  91.                             ,
  92.                             mAlPositiveMsgs.length-1
  93.                     );
  94.                     String strPositive = mAlPositiveMsgs[idxRandom];
  95.                     displayMessage(strPositive);
  96.                     /*
  97.                     //display!
  98.                     String strAll =
  99.                         strPositive + "\n" + mTvMsgs.getText().toString();
  100.                     mTvMsgs.setText(strAll);
  101.                      */
  102.                 }//if
  103.                 else{
  104.                     //?
  105.                     int idxRandom = AmUtil.randomInt(
  106.                             0
  107.                             ,
  108.                             mAlNegativeMsgs.length-1
  109.                     );
  110.                     String strNegative = mAlNegativeMsgs[idxRandom];
  111.                     displayMessage(strNegative);
  112.                     /*
  113.                     //display!
  114.                     String strAll = strNegative + "\n" +
  115.                         mTvMsgs.getText().toString();
  116.                     mTvMsgs.setText(strAll);
  117.                      */
  118.                 }//else
  119.             }//if bCaution
  120.         }//onClick
  121.     };//mBtnClickHandler
  122.  
  123.     @Override
  124.     protected void onCreate(Bundle savedInstanceState) {
  125.         super.onCreate(savedInstanceState);
  126.         //setContentView(R.layout.activity_main);
  127.         //setContentView(R.layout.rl_motivator_v1);
  128.         setContentView(R.layout.rl_motivator_v2);
  129.  
  130.         init();
  131.     }//onCreate
  132.  
  133.     void init(){
  134.         //1 - bindings
  135.         mContext = this;
  136.         mUtil = new AmUtil(this);
  137.         mAlPositiveMsgs = this.getResources().getStringArray(R.array.saPositiveMsgs);
  138.         /*
  139.         for (String s: fromXML){
  140.             mAlPositiveMsgs.add(s);
  141.         }//for
  142.          */
  143.         mAlNegativeMsgs = getResources().getStringArray(R.array.saNegativeMsgs);
  144.  
  145.         mSbProbPositiveMsgs = findViewById(R.id.idSbProbPositiveMsgs);
  146.         mBtnGetMsg = findViewById(R.id.idBtnGetMsg);
  147.         mTvMsgs = findViewById(R.id.idTvMsgs); //null @ v2 ; ok @ v1
  148.         mLvMsgs = findViewById(R.id.idLvMsgs); //ok @ v2 ; null @ v1
  149.         if (mLvMsgs!=null){
  150.             mAlMsgs = new ArrayList<String>(); //no strings, empty ArrayList
  151.             mAd = new ArrayAdapter<String>(
  152.                 mContext,
  153.                 android.R.layout.simple_list_item_1,//each String/Message will appear inside this layout
  154.                 mAlMsgs//data source
  155.             );
  156.             mLvMsgs.setAdapter(mAd); //the ListView gets bound to the mAd
  157.         }//if
  158.  
  159.  
  160.         View[] mAllRelevantObjects =
  161.                 {mSbProbPositiveMsgs, mBtnGetMsg /*, mTvMsgs*/};
  162.         //static method
  163.         boolean bQualityControlResult = AmUtil.qualityControl(
  164.             mAllRelevantObjects
  165.         );
  166.         //if (!bQualityControlResult){
  167.         if (bQualityControlResult==false){
  168.             //will happen @v2
  169.             Log.e(STAMP, getResources().getString(R.string.strQCFail));
  170.             finish();
  171.         }//if
  172.  
  173.         mUtil.fb("Hello!");
  174.  
  175.         //2 - behaviors
  176.         //without this the Button would "do nothing"
  177.         mBtnGetMsg.setOnClickListener(mBtnClickHandler);
  178.         //without the following code, list items would "do nothing"
  179.         mLvMsgs.setOnItemClickListener(mItemClickListener);
  180.     }//init
  181. }//MainActivity
Add Comment
Please, Sign In to add comment