Skip to content

Instantly share code, notes, and snippets.

@Ebaneck
Last active April 25, 2017 18:02
Show Gist options
  • Select an option

  • Save Ebaneck/bf8abb9f5081afec46f9404a5a9174d3 to your computer and use it in GitHub Desktop.

Select an option

Save Ebaneck/bf8abb9f5081afec46f9404a5a9174d3 to your computer and use it in GitHub Desktop.
package com.nkdroid.firstaid;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class ListviewCheckboxes extends Activity {
CheckboxesAdapter dataAdapter = null;
Integer globalPosition;
String txt;
String APP_NAME = "Pregnancy";
String[][] items;
TextView heading;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_checklists);
Intent i = getIntent();
// Get a single globalPosition
globalPosition = i.getExtras().getInt("globalPosition");
txt = i.getStringExtra("txt");
heading = (TextView) findViewById(R.id.heading);
heading.setText(txt);
getItems();
//Generate list View from ArrayList
displayListView();
//checkButtonClick();
}
private void displayListView() {
//Array list of countries
ArrayList<ChecklistItems> checklistItemsList = new ArrayList<ChecklistItems>();
ChecklistItems checklistItems;
for (int i = 0; i<items[globalPosition].length; i++){
boolean testChecked;
SharedPreferences preferences = getApplicationContext().getSharedPreferences(APP_NAME, Context.MODE_PRIVATE);
testChecked = preferences.getBoolean(String.valueOf(globalPosition) + String.valueOf(i), false);
checklistItems = new ChecklistItems(items[globalPosition][i], testChecked); // checked[globalPosition][i]);
checklistItemsList.add(checklistItems);
}
//create an ArrayAdaptar from the String Array
dataAdapter = new CheckboxesAdapter(this,
R.layout.checklist_view, checklistItemsList);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
/*
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
ChecklistItems checklistItems = (ChecklistItems) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),
"Clicked on Row: " + checklistItems.getName(),
Toast.LENGTH_LONG).show();
}
});
*/
}
private class CheckboxesAdapter extends ArrayAdapter<ChecklistItems> {
private ArrayList<ChecklistItems> checklistItemsList;
public CheckboxesAdapter(Context context, int textViewResourceId,
ArrayList<ChecklistItems> checklistItemsList) {
super(context, textViewResourceId, checklistItemsList);
this.checklistItemsList = new ArrayList<ChecklistItems>();
this.checklistItemsList.addAll(checklistItemsList);
}
private class ViewHolder {
CheckBox name;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.checklist_view, null);
holder = new ViewHolder();
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
ChecklistItems checklistItems = (ChecklistItems) cb.getTag();
/*
Toast.makeText(getApplicationContext(),
"Clicked on Checkbox: " + cb.getText() +
" is " + cb.isChecked(),
Toast.LENGTH_LONG).show();
*/
checklistItems.setSelected(cb.isChecked());
//Save state of checkbox
SharedPreferences preferences = getApplicationContext().getSharedPreferences("Pregnancy", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(String.valueOf(globalPosition) + String.valueOf((position)), cb.isChecked());
editor.apply();
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
ChecklistItems checklistItems = checklistItemsList.get(position);
holder.name.setText(checklistItems.getName());
holder.name.setChecked(checklistItems.isSelected());
holder.name.setTag(checklistItems);
return convertView;
}
}
private void getItems(){
items = new String[][]{
{
"Hair ties and comb/brush",
"Flip flobs for showering",
"Slippers",
"Socks",
"Physical Cash",
"Pads (bring a lot as many women can bleed after birth)",
"Snacks",
"Cell phone and/or camera",
"Lip balm",
"Tooth brush and tooth paste",
"Maternity bras/breast feeding bras x 3",
"Water bottle",
"Button up pyjamas (some that open in the front is good for breast feeding)",
"Clothing to wear home",
"Prenatal papers",
"Underwear (bring at least 5 pairs)",
"Shower supplies (soap, conditioner, shampoo)",
"Deoderant",
"Body lotion",
"Any medications you are currently taking",
"Razor, if you would like to shave",
},
{
"Car seat",
"Bottles, formula",
"Baby oil/petroleum jelly (Vaseline)",
"Baby pyjamas",
"Baby wipes",
"Receiving blankets x 2",
"Clothing for baby (socks, hat, etc)"
},
{
"Make your prenatal appointments",
"Try to eat well",
"Drink lots of water! 8-10 glasses/day",
"Know the warning signs of pregnancy",
"Try to quit smoking or using illegal drugs",
"Sleep on your left side",
"Spend time with your partner",
},
{
"Choose a birth partner",
"Find out the sex of your baby",
"Go to your prenatal appointments",
"Check your weight gain",
"Exercise",
"Drink lots of water!",
"Try to relax, take a warm bath",
"Try to get some maternity clothes",
"Visit the dentist! There is a dentist at the Clinic.",
"Try to sleep on your side with rolled blankets and pillows",
"Set up a safe place for your baby to sleep",
"Ask friends and family for gently used baby clothes",
"Stay safe!",
},
{
"Count your baby kicking! You should feel about 10 kicks per hour from your baby when you lay down on your left hand side",
"Go to your prenatal appointments",
"Know when you should go to the hospital, because the baby is coming!",
"Try to eat well",
"Stretch and exercise often. Walking is great!",
"Get your baby items together, like bottles, formula, a car seat and clothes",
"Learn the signs of labor",
"Pack your hospital bag using the checklist",
"Get lots of sleep",
"Ask for help from friends and family",
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment