From bafcb5dc14dfee5042e9b9412298cdd001985b23 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 9 Jan 2019 19:30:42 +0400 Subject: [PATCH] Obtaining intent extras is wrapped in a function that returns an associative array. --- .../vrcatalog/view/DetailActivity.java | 67 +++++++++++++++++-- 1 file changed, 60 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java index 5e95a3c..b478d3b 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java @@ -8,6 +8,9 @@ import android.view.MenuItem; import android.widget.EditText; import android.widget.TextView; +import java.util.HashMap; +import java.util.Map; + import ru.volgorobot.vrcatalog.R; public class DetailActivity extends AppCompatActivity { @@ -29,13 +32,15 @@ public class DetailActivity extends AppCompatActivity { Intent intent = getIntent(); - nameView.setText(intent.getStringExtra("detailName")); - quantityView.setText(intent.getStringExtra("detailQuantity").equals("0") ? "Нет на складе" : intent.getStringExtra("detailQuantity")); - priceView.setText(intent.getStringExtra("detailPrice")); - countryView.setText(intent.getStringExtra("detailCountry")); - analogueView.setText(intent.getStringExtra("detailAnalogue").equals("") ? "Нет аналогов" : intent.getStringExtra("detailAnalogue")); - datasheetView.setText(intent.getStringExtra("detailDatasheet").equals(" ") ? "Нет" : intent.getStringExtra("detailDatasheet")); - notesView.setText(intent.getStringExtra("detailNotes").equals("") ? "Нет описания" : intent.getStringExtra("detailNotes")); + HashMap extrasMap = getIntentExtras(intent); + + nameView.setText(extrasMap.get("detailName")); + quantityView.setText(extrasMap.get("detailQuantity")); + priceView.setText(extrasMap.get("detailPrice")); + countryView.setText(extrasMap.get("detailCountry")); + analogueView.setText(extrasMap.get("detailAnalogue")); + datasheetView.setText(extrasMap.get("detailDatasheet")); + notesView.setText(extrasMap.get("detailNotes")); } void initializeViews() { @@ -49,6 +54,54 @@ public class DetailActivity extends AppCompatActivity { datasheetView.setMovementMethod(LinkMovementMethod.getInstance()); } + HashMap getIntentExtras(Intent intent) { + HashMap hashMap = new HashMap<>(); + String tmp; + + tmp = intent.getStringExtra("detailName"); + hashMap.put("detailName", tmp); + + tmp = intent.getStringExtra("detailQuantity"); + if(tmp.equals("0")) { + tmp = "Под заказ"; + } + hashMap.put("detailQuantity", tmp); + + tmp = intent.getStringExtra("detailPrice"); + if(tmp.equals("0")) { + tmp = "Уточните у продавца"; + } + hashMap.put("detailPrice", tmp); + + tmp = intent.getStringExtra("detailCountry"); + if(tmp.equals("")) { + tmp = "Не указано"; + } + hashMap.put("detailCountry", tmp); + + tmp = intent.getStringExtra("detailAnalogue"); + if(tmp.equals("")) { + tmp = "Нет аналогов"; + } + hashMap.put("detailAnalogue", tmp); + + tmp = intent.getStringExtra("detailDatasheet"); + if(!tmp.matches("\\S")) { + tmp = "Не указано"; + } + hashMap.put("detailDatasheet", tmp); + + tmp = intent.getStringExtra("detailNotes"); + if(tmp != null && tmp.equals("")) { + tmp = "Нет описания"; + } else if (tmp == null){ + tmp = "Нет описания"; + } + hashMap.put("detailNotes", tmp); + + return hashMap; + } + @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) {