Reputation: 3
I am trying to set reminders. There is a spinner with 5 values (1,2,3,4,5). Let's say if user selects 3 from the spinner there should be 3 "Add Time" buttons displayed. Similarly if user selects 5, five "Add Time" button should be displayed.
User presses "Add Time" button and time picker will be displayed to select time for the reminders. I've created spinner through xml from res/string folder and set an array of 5 item (1,2,3,4,5). I don't know to display these "Add Time" buttons as per the selection of value from the spinner.
Would greatly appreciate any suggestions/guidance.
Thanks a lot...
package com.example.medicationreminder;
import jim.h.common.android.lib.zxing.config.ZXingLibConfig;
import jim.h.common.android.lib.zxing.integrator.IntentIntegrator;
import jim.h.common.android.lib.zxing.integrator.IntentResult;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
import com.example.medicationreminder.InteractiveArrayAdapter.ViewHolder;
import android.app.ListActivity;
import android.widget.ArrayAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
public class MedInfo extends Activity implements OnClickListener{
private Button daily, weekly, mScanBarcode;
private Handler handler = new Handler();
private ZXingLibConfig zxingLibConfig;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_med_info);
mScanBarcode = (Button) findViewById(R.id.Barcodescanner);
mScanBarcode.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.med_info, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == mScanBarcode)//When scan barcode button is clicked
{
IntentIntegrator.initiateScan(MedInfo.this, zxingLibConfig);//Intent that opens camera for scanning through zxing Library
}
/* else if(v == weekly)
{
Intent weekly = new Intent(MedInfo.this,Weekly.class);
startActivity(weekly);
} */
else if(v == daily)
{
Intent daily = new Intent(MedInfo.this, DailyMed.class);
startActivity(daily);
}
}
//The method that cathes the result after scanning is done
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case IntentIntegrator.REQUEST_CODE:
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode,
resultCode, data);
if (scanResult == null)
{
return;
}
final String result = scanResult.getContents();//scanned result
if (result != null)
{
handler.post(new Runnable()
{
@Override
public void run()
{
showScannedResult(result);//opening pop up showing scanned result
}
});
}
break;
default:
}
}
//Pop up dialog showing scanned result
private void showScannedResult(String result)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scanned Result")
.setMessage(result)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
//Array list for selection of Days
public class Weekly extends ListActivity {
public void onCreate1(Bundle icicle) {
super.onCreate(icicle);
// Create an array of Strings, that will be put to our ListActivity
ArrayAdapter<Model> adapter = new InteractiveArrayAdapter(this,
getModel());
setListAdapter(adapter);
}
private List<Model> getModel() {
List<Model> list = new ArrayList<Model>();
list.add(get("Monday"));
list.add(get("Tuesday"));
list.add(get("Wednesday"));
list.add(get("Thursday"));
list.add(get("Friday"));
list.add(get("Saturday"));
list.add(get("Sunday"));
// Initially select one of the items
list.get(1).setSelected(true);
return list;
}
private Model get(String s) {
return new Model(s);
}
// Spinner item selection and add button
public class spinning extends Activity implements AdapterView.OnItemSelectedListener{
LinearLayout list;
Spinner spinner;
@Override
public void onCreate(Bundle savedInstanceSate) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceSate);
setContentView(R.layout.activity_med_info);
spinner = (Spinner)findViewById(R.id.spinner1);
list = (LinearLayout)findViewById(R.id.list);
ArrayAdapter<CharSequence> adapterSpinner = ArrayAdapter.createFromResource(this,
R.array.dose_arrays, android.R.layout.simple_spinner_item);
adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterSpinner);
spinner.setOnItemSelectedListener(this);
}
public void addButton(int number){
list.removeAllViews();
for(int i = 0; i<number;i++){
Button button = new Button(getApplicationContext());
button.setText("Add Time");
list.addView(button);
}
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i,
long l) {
// TODO Auto-generated method stub
switch(i){
case 0:
addButton(1);
break;
case 1:
addButton(2);
break;
case 2:
addButton(3);
break;
case 3:
addButton(4);
break;
case 4:
addButton(5);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
}
}
Medinfo.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@drawable/android"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MedInfo" >
<EditText
android:id="@+id/MedName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup1"
android:layout_alignTop="@+id/radioGroup1"
android:ems="10"
android:hint="Name of Medicine" >
<requestFocus />
</EditText>
<Button
android:id="@+id/Barcodescanner"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/MedName"
android:layout_below="@+id/MedName"
android:layout_marginTop="14dp"
android:text="Scan Barcode" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Barcodescanner"
android:layout_below="@+id/Barcodescanner"
android:layout_marginTop="44dp"
android:text="Dose per day"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView1"
android:entries="@array/dose_arrays"
android:prompt="@string/dose_prompt" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_alignParentLeft="true"
android:layout_below="@+id/spinner" android:id="@+id/list">
</LinearLayout>
<CheckBox
android:id="@+id/checkMon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="114dp"
android:text="Mon" />
<CheckBox
android:id="@+id/checkTue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkMon"
android:layout_alignBottom="@+id/checkMon"
android:layout_toRightOf="@+id/AddTime"
android:text="Tue" />
<CheckBox
android:id="@+id/checkWed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkTue"
android:layout_alignBottom="@+id/checkTue"
android:layout_centerHorizontal="true"
android:text="Wed" />
<CheckBox
android:id="@+id/checkThur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/checkWed"
android:layout_marginLeft="25dp"
android:layout_toRightOf="@+id/checkWed"
android:text="Thur" />
<CheckBox
android:id="@+id/checkFri"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkTue"
android:layout_marginTop="27dp"
android:layout_toRightOf="@+id/radioGroup1"
android:text="Fri" />
<CheckBox
android:id="@+id/checkSat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkFri"
android:layout_alignBottom="@+id/checkFri"
android:layout_alignLeft="@+id/checkTue"
android:text="Sat" />
<CheckBox
android:id="@+id/checkSun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkSat"
android:layout_alignBottom="@+id/checkSat"
android:layout_alignLeft="@+id/checkWed"
android:text="Sun" />
<!--
<Button
android:id="@+id/AddTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:layout_marginTop="49dp"
android:text="Add Time" />
-->
</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Medication Reminder</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_med_info">MedInfo</string>
<string name="dose_prompt">Choose a Dose</string>
<string-array name="dose_arrays">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
</string-array>
<string name="check_Monday">Monday</string>
<string name="check_Tuesday">Tuesday</string>
<string name="check_Wednesday">Wednesday</string>
<string name="check_Thursday">Thursday</string>
<string name="check_Friday">Friday</string>
<string name="check_Saturday">Saturday</string>
<string name="check_Sunday">Sunday</string>
<string name="title_activity_daily_med">DailyMed</string>
<string name="title_activity_weekly">Weekly</string>
<string name="title_activity_daily__addtime">Daily_Addtime</string>
<string name="title_activity_interactive_adapter">InteractiveAdapter</string>
<string name="title_activity_interactive_array_adapter">InteractiveArrayAdapter</string>
<string name="title_activity_barcode_scanner">Barcode_scanner</string>
</resources>
Upvotes: 0
Views: 4438
Reputation: 819
try this code.
ActivityMain.class
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener{
LinearLayout list;
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ativity_main);
spinner = (Spinner) findViewById(R.id.spinner);
list = (LinearLayout)findViewById(R.id.list);
ArrayAdapter<CharSequence> adapterSpinner = ArrayAdapter.createFromResource(this,
R.array.number, android.R.layout.simple_spinner_item);
adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterSpinner);
spinner.setOnItemSelectedListener(this);
}
public void addButton(int number){
list.removeAllViews();
for(int i = 0; i<number; i++){
Button button = new Button(getApplicationContext());
button.setText("Add Time");
list.addView(button);
}
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i){
case 0:
addButton(1);
break;
case 1:
addButton(2);
break;
case 2:
addButton(3);
break;
case 3:
addButton(4);
break;
case 4:
addButton(5);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_centerHorizontal="true" android:layout_alignParentTop="true"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_alignParentLeft="true"
android:layout_below="@+id/spinner" android:id="@+id/list">
</LinearLayout>
strings.xml
<string-array name="number">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
</string-array>
Hope, it will be helpful :)
Upvotes: 1