|
| 1 | +package org.openimis.imisclaims; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | +import android.database.Cursor; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.text.Editable; |
| 7 | +import android.text.TextWatcher; |
| 8 | +import android.view.View; |
| 9 | +import android.widget.AutoCompleteTextView; |
| 10 | +import android.widget.Button; |
| 11 | +import android.widget.EditText; |
| 12 | +import android.widget.ListView; |
| 13 | +import android.widget.SimpleAdapter; |
| 14 | + |
| 15 | +import java.util.HashMap; |
| 16 | + |
| 17 | +import org.openimis.imisclaims.tools.Log; |
| 18 | + |
| 19 | +public class AddItemsPreAuth extends ImisActivity { |
| 20 | + ListView lvItems; |
| 21 | + EditText etQuantity, etAmount; |
| 22 | + Button btnAdd; |
| 23 | + AutoCompleteTextView etItems; |
| 24 | + |
| 25 | + int Pos; |
| 26 | + |
| 27 | + HashMap<String, String> oItem; |
| 28 | + SimpleAdapter alAdapter; |
| 29 | + |
| 30 | + @Override |
| 31 | + public void onCreate(Bundle savedInstanceState) { |
| 32 | + super.onCreate(savedInstanceState); |
| 33 | + setContentView(R.layout.additems); |
| 34 | + |
| 35 | + if (actionBar != null) { |
| 36 | + actionBar.setTitle(getResources().getString(R.string.app_name_claim)); |
| 37 | + } |
| 38 | + |
| 39 | + lvItems = findViewById(R.id.lvItems); |
| 40 | + etQuantity = findViewById(R.id.etQuantity); |
| 41 | + etAmount = findViewById(R.id.etAmount); |
| 42 | + etItems = findViewById(R.id.etItems); |
| 43 | + btnAdd = findViewById(R.id.btnAdd); |
| 44 | + |
| 45 | + alAdapter = new SimpleAdapter(AddItemsPreAuth.this, PreAuthorizationActivity.lvItemList, R.layout.lvitem, |
| 46 | + new String[]{"Code", "Name", "Price", "Quantity"}, |
| 47 | + new int[]{R.id.tvLvCode, R.id.tvLvName, R.id.tvLvPrice, R.id.tvLvQuantity}); |
| 48 | + lvItems.setAdapter(alAdapter); |
| 49 | + |
| 50 | + if (isIntentReadonly()) { |
| 51 | + disableView(etQuantity); |
| 52 | + disableView(etAmount); |
| 53 | + disableView(etItems); |
| 54 | + disableView(btnAdd); |
| 55 | + } else { |
| 56 | + ItemAdapter itemAdapter = new ItemAdapter(this, sqlHandler); |
| 57 | + etItems.setAdapter(itemAdapter); |
| 58 | + etItems.setThreshold(1); |
| 59 | + |
| 60 | + etItems.setOnItemClickListener((parent, view, position, l) -> { |
| 61 | + if (position >= 0) { |
| 62 | + |
| 63 | + Cursor cursor = (Cursor) parent.getItemAtPosition(position); |
| 64 | + final int itemColumnIndex = cursor.getColumnIndexOrThrow("Code"); |
| 65 | + final int descColumnIndex = cursor.getColumnIndexOrThrow("Name"); |
| 66 | + String Code = cursor.getString(itemColumnIndex); |
| 67 | + String Name = cursor.getString(descColumnIndex); |
| 68 | + |
| 69 | + oItem = new HashMap<>(); |
| 70 | + oItem.put("Code", Code); |
| 71 | + oItem.put("Name", Name); |
| 72 | + |
| 73 | + etQuantity.setText("1"); |
| 74 | + etAmount.setText(sqlHandler.getItemPrice(Code)); |
| 75 | + } |
| 76 | + |
| 77 | + }); |
| 78 | + |
| 79 | + etItems.addTextChangedListener(new TextWatcher() { |
| 80 | + @Override |
| 81 | + public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 82 | + btnAdd.setEnabled(s != null && s.toString().trim().length() != 0 |
| 83 | + && etQuantity.getText().toString().trim().length() != 0 |
| 84 | + && etAmount.getText().toString().trim().length() != 0); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void afterTextChanged(Editable s) { |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + etQuantity.addTextChangedListener(new TextWatcher() { |
| 97 | + @Override |
| 98 | + public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 99 | + btnAdd.setEnabled(s != null && s.toString().trim().length() != 0 |
| 100 | + && etItems.getText().toString().trim().length() != 0 |
| 101 | + && etAmount.getText().toString().trim().length() != 0); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void afterTextChanged(Editable s) { |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + etAmount.addTextChangedListener(new TextWatcher() { |
| 114 | + @Override |
| 115 | + public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 116 | + btnAdd.setEnabled(s != null && s.toString().trim().length() != 0 |
| 117 | + && etQuantity.getText().toString().trim().length() != 0 |
| 118 | + && etItems.getText().toString().trim().length() != 0); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void afterTextChanged(Editable s) { |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + lvItems.setAdapter(alAdapter); |
| 131 | + |
| 132 | + btnAdd.setEnabled(false); |
| 133 | + btnAdd.setOnClickListener(v -> { |
| 134 | + try { |
| 135 | + if (oItem == null) return; |
| 136 | + |
| 137 | + String Amount, Quantity; |
| 138 | + |
| 139 | + HashMap<String, String> lvItem = new HashMap<>(); |
| 140 | + lvItem.put("Code", oItem.get("Code")); |
| 141 | + lvItem.put("Name", oItem.get("Name")); |
| 142 | + Amount = etAmount.getText().toString(); |
| 143 | + lvItem.put("Price", Amount); |
| 144 | + if (etQuantity.getText().toString().length() == 0) Quantity = "1"; |
| 145 | + else Quantity = etQuantity.getText().toString(); |
| 146 | + lvItem.put("Quantity", Quantity); |
| 147 | + PreAuthorizationActivity.lvItemList.add(lvItem); |
| 148 | + |
| 149 | + alAdapter.notifyDataSetChanged(); |
| 150 | + |
| 151 | + etItems.setText(""); |
| 152 | + etAmount.setText(""); |
| 153 | + etQuantity.setText(""); |
| 154 | + |
| 155 | + } catch (Exception e) { |
| 156 | + Log.d("AddLvError", e.getMessage()); |
| 157 | + } |
| 158 | + }); |
| 159 | + |
| 160 | + lvItems.setOnItemLongClickListener((parent, view, position, id) -> { |
| 161 | + try { |
| 162 | + |
| 163 | + Pos = position; |
| 164 | + HideAllDeleteButtons(); |
| 165 | + |
| 166 | + Button d = view.findViewById(R.id.btnDelete); |
| 167 | + d.setVisibility(View.VISIBLE); |
| 168 | + |
| 169 | + d.setOnClickListener(v -> { |
| 170 | + PreAuthorizationActivity.lvItemList.remove(Pos); |
| 171 | + HideAllDeleteButtons(); |
| 172 | +// alAdapter.notifyDataSetChanged(); |
| 173 | + }); |
| 174 | + |
| 175 | + |
| 176 | + } catch (Exception e) { |
| 177 | + Log.d("ErrorOnLongClick", e.getMessage()); |
| 178 | + } |
| 179 | + return true; |
| 180 | + }); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private boolean isIntentReadonly() { |
| 185 | + Intent intent = getIntent(); |
| 186 | + return intent.getBooleanExtra(PreAuthorizationActivity.EXTRA_READONLY, false); |
| 187 | + } |
| 188 | + |
| 189 | + private void HideAllDeleteButtons() { |
| 190 | + for (int i = 0; i <= lvItems.getLastVisiblePosition(); i++) { |
| 191 | + Button Delete = lvItems.getChildAt(i).findViewById(R.id.btnDelete); |
| 192 | + Delete.setVisibility(View.GONE); |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments