Created
July 6, 2018 15:51
-
-
Save maxchehab/a58b7e1c44465f549ba40026de9f5341 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.kathrinegibson.watercare; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.support.design.widget.FloatingActionButton; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Spinner; | |
import android.widget.AdapterView.OnItemSelectedListener; | |
import com.google.gson.Gson; | |
import static android.content.ContentValues.TAG; | |
import static com.example.kathrinegibson.watercare.MainActivity.editor_string; | |
import static com.example.kathrinegibson.watercare.MainActivity.userAddedPlants; | |
public class AddActivity extends Activity implements OnItemSelectedListener { | |
PlantType newPlantType = PlantType.Default; | |
String plantName = null; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_add); | |
FloatingActionButton fab2 = findViewById(R.id.fab2); | |
fab2.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Intent returnIntent = new Intent(); | |
setResult(RESULT_CANCELED, returnIntent); | |
finish(); | |
} | |
}); | |
Button addButton = findViewById(R.id.add_plant_button); | |
addButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
EditText userInput = findViewById(R.id.editText1); | |
plantName = userInput.getText().toString(); | |
if(!plantName.isEmpty() && (newPlantType != PlantType.Default)){ | |
Plant newPlant = new Plant(newPlantType); | |
newPlant.changePlantName(plantName); | |
userAddedPlants.add(newPlant); | |
IOUtility.SaveData(AddActivity.this, userAddedPlants); | |
} | |
setResult(RESULT_OK); | |
finish(); | |
} | |
}); | |
Spinner spinner = findViewById(R.id.spinner1); | |
spinner.setOnItemSelectedListener(this); | |
// Creating adapter for spinner | |
ArrayAdapter<PlantType> dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, PlantType.values()); | |
// Drop down layout style - list view with radio button | |
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | |
// attaching data adapter to spinner | |
spinner.setAdapter(dataAdapter); | |
} | |
@Override | |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | |
newPlantType = PlantType.values()[position]; | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> parent) { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.kathrinegibson.watercare; | |
import android.app.Activity; | |
import android.content.SharedPreferences; | |
import com.google.gson.Gson; | |
import com.google.gson.reflect.TypeToken; | |
import java.lang.reflect.Type; | |
import java.util.ArrayList; | |
import static android.content.Context.MODE_PRIVATE; | |
import static com.example.kathrinegibson.watercare.MainActivity.editor_string; | |
import static com.example.kathrinegibson.watercare.MainActivity.userAddedPlants; | |
public class IOUtility { | |
static String KEY = "plant list"; | |
//load up list of plants when starting the app | |
public static ArrayList<Plant> LoadData(Activity activity){ | |
ArrayList<Plant> plants = new ArrayList<Plant>(); | |
SharedPreferences sharedPreferences = activity.getSharedPreferences("shared preferences", MODE_PRIVATE); | |
Gson gson = new Gson(); | |
String json = sharedPreferences.getString(KEY, null); | |
Type type = new TypeToken<ArrayList<Plant>>() {}.getType(); | |
plants = gson.fromJson(json, type); | |
if (plants == null){ | |
plants = new ArrayList<>(); | |
} | |
return plants; | |
} | |
//update the list of plants when adding a plant | |
public static void SaveData(Activity activity, ArrayList<Plant> plantList){ | |
SharedPreferences sharedPreferences = activity.getSharedPreferences("shared preferences", MODE_PRIVATE); | |
SharedPreferences.Editor editor = sharedPreferences.edit(); | |
Gson gson = new Gson(); | |
String json = gson.toJson(plants); | |
editor.putString(KEY, json); | |
editor.apply(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.kathrinegibson.watercare; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.os.Bundle; | |
import android.support.design.widget.FloatingActionButton; | |
import android.support.design.widget.Snackbar; | |
import android.support.design.widget.TabLayout; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.GridLayoutManager; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.support.v7.widget.Toolbar; | |
import android.util.Log; | |
import android.view.View; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.SimpleAdapter; | |
import com.google.gson.Gson; | |
import com.google.gson.reflect.TypeToken; | |
import java.lang.reflect.Type; | |
import java.util.ArrayList; | |
import java.util.List; | |
import static android.content.ContentValues.TAG; | |
public class MainActivity extends AppCompatActivity { | |
static ArrayList<Plant> userAddedPlants; | |
private LinearLayoutManager lLayout; | |
private RecyclerViewAdapter rcAdapter; | |
private RecyclerView rView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
userAddedPlants = loadData(this); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = findViewById(R.id.toolbar); | |
toolbar.setSubtitle("WaterCare"); | |
toolbar.inflateMenu(R.menu.menu_main); | |
lLayout = new GridLayoutManager(MainActivity.this,2); | |
rView= findViewById(R.id.recycler_view); | |
rView.setLayoutManager(lLayout); | |
rcAdapter = new RecyclerViewAdapter(MainActivity.this, userAddedPlants); | |
rView.setAdapter(rcAdapter); | |
FloatingActionButton fab = findViewById(R.id.fab); | |
final Intent addPlantIntent = new Intent(this, AddActivity.class); | |
fab.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
startActivity(addPlantIntent); | |
} | |
}); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_help) { | |
return true; | |
} | |
if (id == R.id.action_about) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.kathrinegibson.watercare; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.PopupMenu; | |
import android.widget.Toast; | |
import java.sql.SQLException; | |
import java.util.List; | |
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> { | |
private List<Plant> itemList; | |
private Context context; | |
public RecyclerViewAdapter(Context context, List<Plant> itemList) { | |
this.itemList = itemList; | |
this.context = context; | |
} | |
@Override | |
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) { | |
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, null); | |
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView); | |
return rcv; | |
} | |
@Override | |
public void onBindViewHolder(final RecyclerViewHolders holder, final int position) { | |
Activity myActivity = this; | |
holder.plantName.setText(itemList.get(position).getName()); | |
holder.plantType.setText(itemList.get(position).getPlantType()); | |
holder.plantPhoto.setImageResource(itemList.get(position).getImagePath()); | |
holder.buttonViewOption.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
PopupMenu popup = new PopupMenu(context, holder.buttonViewOption); | |
//inflating menu from xml resource | |
popup.inflate(R.menu.plant_menu); | |
//adding click listener | |
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { | |
@Override | |
public boolean onMenuItemClick(MenuItem item) { | |
switch (item.getItemId()) { | |
case R.id.menu1: | |
Toast.makeText(context, "menu 1 clicked", Toast.LENGTH_SHORT).show(); | |
break; | |
case R.id.menu2: | |
Toast.makeText(context, "menu 2 clicked", Toast.LENGTH_SHORT).show(); | |
break; | |
case R.id.menu3: | |
Toast.makeText(context, "menu 3 clicked", Toast.LENGTH_SHORT).show(); | |
break; | |
case R.id.menu4: | |
itemList.remove(position); | |
IOUtility.UpdateData((Activity) context, itemList); | |
//make a save function ->write to shared preferences, use the same key | |
notifyItemRemoved(position); | |
notifyItemRangeChanged(position,itemList.size()); | |
Toast.makeText(context,"Removed : " + position, Toast.LENGTH_SHORT).show(); | |
break; | |
} | |
return false; | |
} | |
}); | |
//displaying the popup | |
popup.show(); | |
} | |
}); | |
} | |
@Override | |
public int getItemCount() { | |
return this.itemList.size(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment