Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MainActivity.java
- package com.joythis.android.motivator;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.SeekBar;
- import android.widget.TextView;
- import android.widget.Button;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- //data members
- Context mContext;
- // for correspondence with whatever relevant in the layout
- TextView mTvAbout, mTvMsgs;
- SeekBar mSbProb; // the user picked probability (of a positive message)
- Button mBtnGetMsg;
- // trio for respecting MVC for ListView
- ListView mLvMsgs;
- //the data
- ArrayList<String> mAlMsgs;
- //the broker
- ArrayAdapter<String> mAd;
- String[] maPositive, maNegative;
- //behavior (click) handler
- View.OnClickListener mClickHandler =
- new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.idBtnGetMsg:
- //displayMsgInTextView();
- displayMessageInListView();
- break;
- }//switch
- }//onClick
- };//mClickHandler
- void displayMessageInListView(){
- int iUserPickedProbabilityOfPositiveMsg =
- mSbProb.getProgress(); //0..100
- int iRandom = AmUtil.randomInt(
- 0,
- mSbProb.getMax()
- );
- boolean bDecidePositive =
- iRandom<iUserPickedProbabilityOfPositiveMsg;
- String strMsg = "";
- if (bDecidePositive) {
- strMsg = maPositive[
- AmUtil.randomInt(
- 0,
- maPositive.length - 1
- )
- ];
- }
- else
- {
- strMsg = maNegative[
- AmUtil.randomInt(
- 0,
- maNegative.length-1
- )
- ];
- }
- //???? add strMsg to the ListView
- //mAlMsgs.add(strMsg); // inserts at the end
- mAlMsgs.add(0, strMsg);
- mAd.notifyDataSetChanged();
- }//displayMessageInListView
- void displayMsgInTextView(){
- //String strP1 = maPositive[0];
- //String strN3 = maNegative[2];
- int iUserPickedProbabilityOfPositiveMsg =
- mSbProb.getProgress(); //0..100
- int iRandom = AmUtil.randomInt(
- 0,
- mSbProb.getMax()
- );
- boolean bDecidePositive =
- iRandom<iUserPickedProbabilityOfPositiveMsg;
- String strMsg = "";
- if (bDecidePositive) {
- strMsg = maPositive[
- AmUtil.randomInt(
- 0,
- maPositive.length - 1
- )
- ];
- }
- else
- {
- strMsg = maNegative[
- AmUtil.randomInt(
- 0,
- maNegative.length-1
- )
- ];
- }
- String strPast = mTvMsgs.getText().toString();
- mTvMsgs.setText(strMsg+"\n"+strPast);
- }//displayMsg
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //setContentView(R.layout.activity_main);
- //setContentView(R.layout.rl_motivator_v1);
- //setContentView(R.layout.cl_motivator_v1);
- setContentView(R.layout.rl_motivator_v2);
- init();
- }//onCreate
- void init(){
- //inits
- mContext = this;
- mTvAbout = findViewById(R.id.idTvAbout);
- mSbProb = findViewById(R.id.idSbProb);
- mBtnGetMsg = findViewById(R.id.idBtnGetMsg);
- //caution!
- mTvMsgs = findViewById(R.id.idTvMsgs);
- mLvMsgs = findViewById(R.id.idLvMsgs);
- mAlMsgs = new ArrayList<>();
- mAd = new ArrayAdapter<>(
- mContext,
- android.R.layout.simple_list_item_1,
- mAlMsgs
- );
- mLvMsgs.setAdapter(mAd);
- maPositive = this.getResources().
- getStringArray(R.array.saPositiveMsgs);
- maNegative = this.getResources().
- getStringArray(R.array.saNegativeMsgs);
- //quality control
- //set behavior
- //associate mBtnGetMsg to some behavior handler
- mBtnGetMsg.setOnClickListener(
- mClickHandler
- );
- /*
- testing randomInt
- */
- /*
- for(int idx=0; idx<100; idx+=1){
- int iRandom = AmUtil.randomInt(555, 777);
- mTvMsgs.setText(
- String.valueOf(iRandom)
- +"\n"+
- mTvMsgs.getText().toString()
- );
- }
- */
- }//init
- }//class MainActivity
Advertisement
Add Comment
Please, Sign In to add comment