am_dot_com

DDM 2022-10-26

Oct 26th, 2022 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. // MainActivity.java
  2. package com.joythis.android.motivator;
  3.  
  4. import androidx.appcompat.app.AppCompatActivity;
  5.  
  6. import android.content.Context;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.ArrayAdapter;
  10. import android.widget.ListView;
  11. import android.widget.SeekBar;
  12. import android.widget.TextView;
  13. import android.widget.Button;
  14.  
  15. import java.util.ArrayList;
  16.  
  17. public class MainActivity extends AppCompatActivity {
  18. //data members
  19. Context mContext;
  20.  
  21. // for correspondence with whatever relevant in the layout
  22. TextView mTvAbout, mTvMsgs;
  23. SeekBar mSbProb; // the user picked probability (of a positive message)
  24. Button mBtnGetMsg;
  25.  
  26. // trio for respecting MVC for ListView
  27. ListView mLvMsgs;
  28. //the data
  29. ArrayList<String> mAlMsgs;
  30. //the broker
  31. ArrayAdapter<String> mAd;
  32.  
  33. String[] maPositive, maNegative;
  34.  
  35. //behavior (click) handler
  36. View.OnClickListener mClickHandler =
  37. new View.OnClickListener() {
  38. @Override
  39. public void onClick(View v) {
  40. switch(v.getId()){
  41. case R.id.idBtnGetMsg:
  42. //displayMsgInTextView();
  43. displayMessageInListView();
  44. break;
  45. }//switch
  46. }//onClick
  47. };//mClickHandler
  48.  
  49. void displayMessageInListView(){
  50. int iUserPickedProbabilityOfPositiveMsg =
  51. mSbProb.getProgress(); //0..100
  52.  
  53. int iRandom = AmUtil.randomInt(
  54. 0,
  55. mSbProb.getMax()
  56. );
  57.  
  58. boolean bDecidePositive =
  59. iRandom<iUserPickedProbabilityOfPositiveMsg;
  60.  
  61. String strMsg = "";
  62. if (bDecidePositive) {
  63. strMsg = maPositive[
  64. AmUtil.randomInt(
  65. 0,
  66. maPositive.length - 1
  67. )
  68. ];
  69. }
  70. else
  71. {
  72. strMsg = maNegative[
  73. AmUtil.randomInt(
  74. 0,
  75. maNegative.length-1
  76. )
  77. ];
  78. }
  79.  
  80. //???? add strMsg to the ListView
  81. //mAlMsgs.add(strMsg); // inserts at the end
  82. mAlMsgs.add(0, strMsg);
  83. mAd.notifyDataSetChanged();
  84. }//displayMessageInListView
  85.  
  86. void displayMsgInTextView(){
  87. //String strP1 = maPositive[0];
  88. //String strN3 = maNegative[2];
  89. int iUserPickedProbabilityOfPositiveMsg =
  90. mSbProb.getProgress(); //0..100
  91.  
  92. int iRandom = AmUtil.randomInt(
  93. 0,
  94. mSbProb.getMax()
  95. );
  96.  
  97. boolean bDecidePositive =
  98. iRandom<iUserPickedProbabilityOfPositiveMsg;
  99.  
  100. String strMsg = "";
  101. if (bDecidePositive) {
  102. strMsg = maPositive[
  103. AmUtil.randomInt(
  104. 0,
  105. maPositive.length - 1
  106. )
  107. ];
  108. }
  109. else
  110. {
  111. strMsg = maNegative[
  112. AmUtil.randomInt(
  113. 0,
  114. maNegative.length-1
  115. )
  116. ];
  117. }
  118.  
  119. String strPast = mTvMsgs.getText().toString();
  120. mTvMsgs.setText(strMsg+"\n"+strPast);
  121. }//displayMsg
  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.cl_motivator_v1);
  129. setContentView(R.layout.rl_motivator_v2);
  130.  
  131. init();
  132. }//onCreate
  133.  
  134. void init(){
  135. //inits
  136. mContext = this;
  137. mTvAbout = findViewById(R.id.idTvAbout);
  138. mSbProb = findViewById(R.id.idSbProb);
  139. mBtnGetMsg = findViewById(R.id.idBtnGetMsg);
  140.  
  141. //caution!
  142. mTvMsgs = findViewById(R.id.idTvMsgs);
  143.  
  144. mLvMsgs = findViewById(R.id.idLvMsgs);
  145. mAlMsgs = new ArrayList<>();
  146. mAd = new ArrayAdapter<>(
  147. mContext,
  148. android.R.layout.simple_list_item_1,
  149. mAlMsgs
  150. );
  151. mLvMsgs.setAdapter(mAd);
  152.  
  153. maPositive = this.getResources().
  154. getStringArray(R.array.saPositiveMsgs);
  155. maNegative = this.getResources().
  156. getStringArray(R.array.saNegativeMsgs);
  157.  
  158. //quality control
  159.  
  160. //set behavior
  161. //associate mBtnGetMsg to some behavior handler
  162. mBtnGetMsg.setOnClickListener(
  163. mClickHandler
  164. );
  165.  
  166. /*
  167. testing randomInt
  168. */
  169. /*
  170. for(int idx=0; idx<100; idx+=1){
  171. int iRandom = AmUtil.randomInt(555, 777);
  172. mTvMsgs.setText(
  173. String.valueOf(iRandom)
  174. +"\n"+
  175. mTvMsgs.getText().toString()
  176. );
  177. }
  178. */
  179. }//init
  180. }//class MainActivity
Advertisement
Add Comment
Please, Sign In to add comment