Android - How To retrieve EditText value from RecyclerView in MainActivity - android

How can I retrieve the value from all EditTexts created by the RecyclerView in MainActivity?
In my RecyclerView Adapter I'm extending my inner class:
public class MyPersonalAdapter extends RecyclerView.Adapter<MyPersonalAdapter.MyPersonalViewHolder>
I'm getting a reference to the EditText in that inner class:
class MyPersonalViewHolder extends RecyclerView.ViewHolder {
TextView numberTextView;
EditText nameEditText;
public MyPersonalViewHolder(View itemView) {
super(itemView);
numberTextView = (TextView) itemView.findViewById(R.id.tv_number);
nameEditText = (EditText) itemView.findViewById(R.id.et_name);
}
}
and in my MainActivity I want to use:
for (int i = 0; i < count; i++) {
String name = "Somehow get that name";
cv.put(MyContract.MyEntry.COLUMN_NAME, "name");
}

Got it working, here is the edited code:
mAdapter = new MyClassAdapter(this, mDataset.size);
mRecyclerView.setAdapter(mAdapter);
mRecyclerview.setItemViewCacheSize(mDataset.size());
List<ContentValues> list = new ArrayList<>();
for (int i = 0; i < mDataset.size(); i++) {
View view = recyclerView.getChildAt(i);
EditText nameEditText = (EditText) view.findViewById(R.id.et_name);
String name = nameEditText.getText().toString();
ContentValues cv = new ContentValues();
cv.put(MyContract.MyEntry.COLUMN_NAME, name);
list.add(cv)
}
// I encapsulated this in a try catch
for (ContentValues c:list) {
mDb.insert(MyClassContract.MyClassEntry.TABLE_NAME, null, c);
}

try this:
for(int i=0;i<adapter.getItemCount();i++){
MyPersonalViewHolder viewHolder= (MyPersonalViewHolder )
mRecyclerView.findViewHolderForAdapterPosition(i);
EditText editText=viewHolder.nameEditText;
}

Implement a addTextChangedListener inside bindview method in the recyclerview adapter.
everytime the edittext text is modified in any cell modify the arraylist string at that position.
And later when you need the whole arraylist, just send it back from the adapter class via any public getmethod.
This should be enough.

I created a getData function inside my Adapter class.
public String getData()
{
String s;
s=txt1.getText().toString();
return s;
}
Then in my MainActivity
public void onSave(View view) {
String[] s=new String[length];
for(int i=0;i<ad.getItemCount();i++)
{
s[i]=ad.getData().toString();
}
}
By this, you can save edit text entries in the array.

//So the other day I spend full day to get data(list of edittext) from recyclerview to activity when i press
button in activity
//perform onclick of button
Here is the code in adapter,Did't work with textchange listener..So i had to used textchange listener and setOnfoucusChange(100% working)
holder.mComment.setOnFocusChangeListener(new
View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
String data=holder.mComment.getText().toString();
commentList[position]=data;
}
if(position==mList.size()-1){
holder.mComment.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence s, int i, int i1, int i2) {
commentList[position]=s.toString();
}
#Override
public void afterTextChanged(Editable editable) {
}
});
}
}
});
Intent intent = new Intent("mrn_intent");
Bundle args = new Bundle();
args.putSerializable("comment_list",(Serializable)commentList);
args.putSerializable("rating_list", (Serializable) mRatingList);
intent.putExtra("BUNDLE_COMMENT",args);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
And in activity write the following code
Bundle args2 = intent.getBundleExtra("BUNDLE_COMMENT");
if(args2!=null){
list = (String[]) args2.getSerializable("comment_list");
Log.d(TAG, "onReceive: list+++=>>>>>>"+list);
}

This worked for me:
mySetEnabled is a method I implemented within my viewHolder.
if(mRecView!=null) {
int size=mRecView.getChildCount();
for (int i = 0; i < size; i++) {
myAdapter.myViewHolder wordView = (myAdapter.myViewHolder)mRecView.findViewHolderForLayoutPosition(i);
if(wordView!=null)
wordView.mySetEnabled(state);
}
}

Try this way,
class MyPersonalViewHolder extends RecyclerView.ViewHolder {
TextView numberTextView;
EditText nameEditText;
public MyPersonalViewHolder(View itemView) {
super(itemView);
numberTextView = (TextView) itemView.findViewById(R.id.tv_number);
nameEditText = (EditText) itemView.findViewById(R.id.et_name);
nameEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
values.add(getAdapterPosition(),s.toString());
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
}
Also, define a function
public String getValue(int position){
return values.get(position);
}
Now getValue can call from MainActivity.

OK. I had the same problem, but my solution was different and simpler. Basically, I'd a list of objects, and I was using the edittext to update their values. So, to do it correctly, instead of using position, I used a for loop and if I reach the object that have the same name of my textview, I break the loop and update using i as my index. You can see the code that I have been using in my adapter bellow:
int i;
for(i = 0; i<list.size(); i++){
if(list.get(i).getName().equals(holder.name.getText())){
break;
}
}
Commodity updated = list.get(i);
updated.setValor(Float.parseFloat(s.toString())); // recovering value of my edit text
list.set(i, updated);
atualizado[i] = Float.parseFloat(s.toString());

Related

How to access data in RecyclerView from a Fragment?

I know, this question asked many times but I am totally lost after reading some answers. I am new to Android development. I have created a Fragment and initialized and set the adapter from that fragment
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewModel = ViewModelProviders.of(this).get(ConfigDataloggerViewModel.class);
dataLoggerList = getResources().getStringArray(R.array.DataLoggerStringArray);
// TODO: Use the ViewModel
//for (int j=0; j< dataLoggerList.length; j++){ DummyArrayList.add(dataLoggerList[j]);}
RecyclerView recyclerView = getView().findViewById(R.id.config_datalogger_recycle_view);
ConfigDataloggerAdapter configDataloggerAdapter = new ConfigDataloggerAdapter(dataLoggerList, getActivity());
recyclerView.setAdapter(configDataloggerAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
I have list of EditText in my fragment and I am setting those EditText in the adapter and I am also saving the values from EditTexts into an Array which is defined in Adapter itself.
public class ConfigDataloggerAdapter extends RecyclerView.Adapter<ConfigDataloggerAdapter.ViewHolder>{
//private ArrayList<RFIDReader> readers = new ArrayList<>();
private String [] mDummyList ;
// private ArrayList<String> mDummyArrayList = new ArrayList<>();
public String [] mDummyArrayList;
//public ArrayList<String> mConfigDataloggerData ;
public String[] mConfigDataloggerData;
// private ConfigDataloggerViewModel mConfigDataModel;
public Map<String, String> tempDataModel = new HashMap<>();
private Context mContext;
public ConfigDataloggerAdapter( String [] mDummyArrayList, Context mContext) {
this.mDummyArrayList = mDummyArrayList;
this.mContext = mContext;
mConfigDataloggerData = new String[mDummyArrayList.length];
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.config_datalogger_list,viewGroup,false);
ConfigDataloggerAdapter.ViewHolder holder = new ConfigDataloggerAdapter.ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ConfigDataloggerAdapter.ViewHolder holder, int i) {
String [] mConfigDataloggerText = null;
// for (int j=0; j< mDummyList.length; j++){ mDummyArrayList.add(mDummyList[j]);}
//ReaderDevice readerDevice = mDummyArrayList.get(i);
String temp = mDummyArrayList[i];
holder.mConfigDataloggerListText.setText(temp);
// tempDataModel.put(temp,mConfigDataloggerData.get(i) );
// mConfigDataModel.setConfigDataloggerVMData(tempDataModel);
//holder.reader_checkedTextView.setText(readerDevice.getName() );
}
#Override
public int getItemCount() {
return mDummyArrayList.length;
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView mConfigDataloggerListText;
public EditText mConfigDataloggarListEditText;
public LinearLayout configDataloggerLayout;
public ViewHolder(#NonNull View itemView) {
super(itemView);
mConfigDataloggerListText = itemView.findViewById(R.id.textView_config_datalogger);
mConfigDataloggarListEditText = itemView.findViewById(R.id.editText_config_datalogger);
mConfigDataloggarListEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mConfigDataloggerData[getAdapterPosition()] =
mConfigDataloggarListEditText.getText().toString();
//here I am storing data from editText to the array
}
#Override
public void afterTextChanged(Editable s) {
}
});
configDataloggerLayout = itemView.findViewById(R.id.config_datalogger_list_layout);
}
}
}
I have two questions, 1) how to access mConfigDataloggerData from adapter in the Fragment? 2) I have a button in same fragment. when I press the button, other fragment starts. Now, I want to save data from mConfigDataloggerData to ViewModel when press the button. So where exactly I write mViewModel = ViewModelProviders.of(this).get(xxxx.class); ?
For your reference, below code is of an activity where my Fragments are attached.
public class defaultActivity extends AppCompatActivity {
private String TAG = "default activity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datalogger_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ConfigDataloggerFragment configDataloggerFragment = new ConfigDataloggerFragment();
// getSupportFragmentManager().beginTransaction().add(R.id.default_datalogger_activity, datalogger).commit();
getSupportFragmentManager().beginTransaction().add(R.id.default_datalogger_activity, configDataloggerFragment).commit();
}
public void StartorStopInventory(View view) {
Button button = (Button) view;
if (application.mConnectedReader.isConnected()){
if (application.mIsInventoryRunning ){
application.mIsInventoryRunning = true;
button.setText("STOP");
try{
TriggerInfo triggerInfo = new TriggerInfo();
Log.d(TAG, "Start trigger setting when button is pressed" + triggerInfo.StartTrigger.getTriggerType());
Log.d(TAG, "Stop trigger setting when button is pressed" + triggerInfo.StartTrigger.getTriggerType());
application.mConnectedReader.Actions.Inventory.perform();
}catch (InvalidUsageException e){
Log.d(TAG, "StartorStopInventory: Inventory perform fail " + e);
} catch (final OperationFailureException op) {
op.printStackTrace();
Log.d(TAG, "StartorStopInventory: Operational failure " + op.getResults() + " " + op.getVendorMessage());
Toast.makeText(view.getContext(), op.getVendorMessage(), Toast.LENGTH_LONG);
}
}
}
}
public void start_data_logging_click(View view) {
Datalogger datalogger = new Datalogger();
// getSupportFragmentManager().beginTransaction().replace(R.id.default_datalogger_activity, datalogger);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.default_datalogger_activity, datalogger);
transaction.addToBackStack(null);
transaction.commit();
}
}
1) Create getters in Adapter for your fields, ex
public String[] getConfigDataloggerData(){
return mConfigDataloggerData;
}
...
To declare adapter globally;
public class YourFragment {
ConfigDataloggerAdapter configDataloggerAdapter,
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewModel = ViewModelProviders.of(this).get(ConfigDataloggerViewModel.class);
dataLoggerList = getResources().getStringArray(R.array.DataLoggerStringArray);
// TODO: Use the ViewModel
//for (int j=0; j< dataLoggerList.length; j++){ DummyArrayList.add(dataLoggerList[j]);}
RecyclerView recyclerView = getView().findViewById(R.id.config_datalogger_recycle_view);
configDataloggerAdapter = new ConfigDataloggerAdapter(dataLoggerList, getActivity());
recyclerView.setAdapter(configDataloggerAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
}
Then inside your Fragment, call configDataloggerAdapter.getConfigDataloggerData() and you will get your data. Same for other fields
2) I dont yet understand that part

Get EditText Value from RecyclerView adapter

I am trying to get the EditText values from the RecyclerView(Adapter).
class BillingOrderListHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView Product, type,vartie,unitnumber, Total;
LinearLayout CardView;
BillingOrderListHolder (View itemView) {
super(itemView);
Product = itemView.findViewById(R.id.Product);
type = itemView.findViewById(R.id.type);
vartie = itemView.findViewById(R.id.vartie);
unitnumber= itemView.findViewById(R.id.unitnumber);
rate = itemView.findViewById(R.id.rate);
Total= itemView.findViewById(R.id.Total);
rate.addTextChangedListener(new TextWatcher(){
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if (!charSequence.toString().equalsIgnoreCase("")) {
int result = (dataList.get(getAdapterPosition()).getUnitCount() * Integer.valueOf(charSequence.toString()));
Total.setText(" = Rs:" + String.valueOf(result));
onEditTextChanged.onTextChanged(i, charSequence.toString());
}
dataList.get(getAdapterPosition()).setEditTextValue(rate.getText().toString());
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
If the user changes the value in EditText the totall amount gets calculated accordingly. I just want to get the EditText value from the RecyclerView and I generate an Invoice with the total amount.
int sNO = 0, qTY = 0, TotalCharges = 0, Price = 0;
String ProductType, Type, Veriety;
for (int i = 0; i < DeliverOrderlist.size(); i++){
sNO = i + 1;
ProductType = DeliverOrderlist.get(i).getProduct();
Type = DeliverOrderlist.get(i).getType();
serilno.addElement(new Paragraph(String.valueOf(sNO)));
Veriety = DeliverOrderlist.get(i).getVariety();
qTY = DeliverOrderlist.get(i).getUnitCount();
qty.addElement(new Paragraph(String.valueOf(qTY)));
description.addElement(new Paragraph(ProductType + " " + Type + " " + Veriety));
Price = Integer.parseInt(Billing_InvoiceAdapter.dataList.get(i).getEditTextValue());
price.addElement(new Paragraph(String.valueOf(Price)));
int result = qTY * Price;
amount.addElement(new Paragraph(String.valueOf(result)));
TotalCharges += result;
}
When I run the above code, it works fine if I do not change the value in EditText. If I change the value in any one of the EditText in recyclerView, it gets the last EditText box value from the recyclerView and sets that value to the EditText value that I edited. I am sorry if I am not clear with the Information. Am I doing anything wrong? Can
I also tried this but it works only if I edit the value in the EditText. Otherwise it gives null point exception error. anyone help me with this please? Thanks in advance.
you need to null the listener in first in bindviewholder and add listener like below in your bindviewholder method.
billingListHolder.rate.addTextChangedListener(null);
billingListHolder.rate.addTextChangedListener(new TextWatcher(){
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if (!charSequence.toString().equalsIgnoreCase("")) {
int result = (dataList.get(getAdapterPosition()).getUnitCount() * Integer.valueOf(charSequence.toString()));
Total.setText(" = Rs:" + String.valueOf(result));
onEditTextChanged.onTextChanged(i, charSequence.toString());
}
dataList.get(getAdapterPosition()).setEditTextValue(rate.getText().toString());
}
#Override
public void afterTextChanged(Editable s) {
}
});

libPhoneNumber not giving correct US results on edittext

I am trying to create an edit text with a text watcher that displays the phone number correctly, to start out I am just testing it with a US number.
If I go to this site
http://libphonenumber.appspot.com/
And type in US and the number 9188143287 the results look perfect however when I implement the library as they say I am getting different results
Here is my fragment
ublic class PhoneNumberFragment extends Fragment{
private TextView next;
private EditText phoneNumber;
private PhoneNumberUtil phoneUtil;
private AsYouTypeFormatter formatter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_phone_number, container, false);
phoneUtil = PhoneNumberUtil.getInstance();
formatter = phoneUtil.getAsYouTypeFormatter("US");
init(v);
return v;
}
private void init(View v){
next = (TextView) v.findViewById(R.id.next);
phoneNumber = (EditText) v.findViewById(R.id.phoneNumber);
phoneNumber.addTextChangedListener(new PhoneNumberTextWatcher());
}
private void turnOnNext(){
next.setBackgroundResource(R.color.warningRed);
}
private class PhoneNumberTextWatcher implements TextWatcher{
private boolean isFormatting = false;
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO: implement your code
}
#Override
public void afterTextChanged(Editable s) {
// TODO: implement your code
}
#Override
public synchronized void onTextChanged(CharSequence s, int start, int before,
int count) {
if(before > count) {
return;
}
if(!isFormatting) {
isFormatting = true;
try {
formatter.clear();
for(int i=0; i<s.length();i++) {
phoneNumber.setText(formatter.inputDigit(s.charAt(i)));
phoneNumber.setSelection(phoneNumber.getText().length());
}
} catch (Exception e) {}
isFormatting = false;
}
}
}
}
My results are correct up to 7 digits, but then on the 8th digit the brackets are supposed to appear around the first 3 and they do not.
Thanks for any help!

Use of different TextWatcher-implementations inside RecyclerView.Adapter

Currently I use a RecyclerView to represent a dynamically configuration list form.
Every configuration item (entry at RecyclerView list) contains one EditText item.
To avoid wrong user input (some fields allow only integer, others only one digit after comma), I've implemented two different TextWatcher-filters which correct illegal input ("DecimalFilterDigitsAfterComma" and "DecimalFilterInteger").
My RecyclerView has 16 configuration items in total, but can only display maximum 8 at one time.
My problem is that the TextWatchers are assigned to specific Items (Integers and Decimal-Point TextEdit). But when I'm scrolling a bit, they change their order, so that Decimal- and Integer-Filters get swapped.
The TextWatcher items will be created inside the ConfigurationAdapter which is a RecyclerView.Adapter. I've event managed that the TextWatcher is only created once for each entry by using the mListConfigInit which is a boolean flag list for the items.
ConfigurationAdapter.java:
public class ConfigurationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
/*
...
*/
private List<ConfigItem> mConfiguration = new ArrayList<>();
// make sure that DecimalFilter is only created once for each item
private List<Boolean> mListConfigInit = new ArrayList<>();
public ConfigurationAdapter() {
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.listitem_configuration,
parent,
false);
final ConfigurationViewHolder vh = new ConfigurationViewHolder(v);
/*
...
*/
return vh;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final ConfigurationViewHolder vh = (ConfigurationViewHolder) holder;
ConfigItem config = mConfiguration.get(position);
if(config.ShowValueAsFloat()) {
vh.SetTextWatcherType(ConfigurationViewHolder.TextWatcherType.type_FloatActive);
} else {
vh.SetTextWatcherType(ConfigurationViewHolder.TextWatcherType.type_IntActive);
}
// set name and unit
vh.mName.setText(config.mName);
vh.mUnit.setText(config.mUnit);
/*
...
*/
}
#Override
public int getItemCount() {
return mConfiguration.size();
}
public void addConfigItem(ConfigItem item) {
mConfiguration.add(item);
mListConfigInit.add(new Boolean(false));
notifyItemInserted(mConfiguration.size() - 1);
//notifyDataSetChanged();
}
/*
...
*/
}
ConfigurationViewHolder.java (changed according to pskink-comments):
public final class ConfigurationViewHolder extends RecyclerView.ViewHolder implements TextWatcher {
public TextView mName;
public CheckBox mCheckbox;
public SeekBar mSeekbar;
public EditText mValueEditText;
public TextView mUnit;
private List<TextWatcher> mListTextWatchers = new ArrayList<>();
public enum TextWatcherType {
type_FloatActive(0),
type_IntActive(1);
private int mValue;
TextWatcherType(int value) {
mValue = value;
}
int val() { return mValue; }
}
private TextWatcherType mTextWatcherType = TextWatcherType.type_FloatActive;
public ConfigurationViewHolder(View itemView) {
super(itemView);
mName = (TextView) itemView.findViewById(R.id.textView_configuration_name);
mValueEditText = (EditText) itemView.findViewById(R.id.editText_configuration_value);
mUnit = (TextView) itemView.findViewById(R.id.textView_configuration_unit);
mCheckbox = (CheckBox) itemView.findViewById(R.id.checkbox_configuration);
mSeekbar = (SeekBar) itemView.findViewById(R.id.seekBar_configuration);
mListTextWatchers.add(0, new DecimalFilterDigitsAfterComma(mValueEditText, 1));
mListTextWatchers.add(1, new DecimalFilterInteger(mValueEditText));
mValueEditText.addTextChangedListener(this);
}
public void SetTextWatcherType(TextWatcherType textWatcherType) {
mTextWatcherType = textWatcherType;
}
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
#Override
public void afterTextChanged(Editable editable) {
mListTextWatchers.get(mTextWatcherType.val()).afterTextChanged(editable);
}
}
DecimalFilterInteger.java
public class DecimalFilterInteger implements TextWatcher {
private final static String TAG = ConfigurationAdapter.class.getSimpleName();
private final EditText mEditText;
private String mLastTextValue = new String("");
public DecimalFilterInteger(EditText editText) {
this.mEditText = editText;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public synchronized void afterTextChanged(final Editable text) {
String strInput = text.toString().trim();
if(strInput.isEmpty()) {
return;
}
if(strInput.equals(mLastTextValue)) { // return when same value as last time to avoid endless loop
return;
}
if ((strInput.charAt(0) == '.')) { // handle dot at beginning
strInput = "";
}
if(strInput.contains(".")){ // cut trailing comma
String numberBeforeDecimal = strInput.split("\\.")[0];
strInput = numberBeforeDecimal;
}
mEditText.removeTextChangedListener(this);
mEditText.getText().clear(); // do not use setText here to avoid changing the keyboard
mEditText.append(strInput); // back to default (e. g. from 123-mode to abc-mode),
// see: http://stackoverflow.com/questions/26365808/edittext-settext-changes-the-keyboard-type-to-default-from-123-to-abc
mLastTextValue = mEditText.getText().toString();
mEditText.setSelection(mEditText.getText().toString().trim().length());
mEditText.addTextChangedListener(this);
}
}
Many thanks in advance for your help!
The cause of the swap/switching behaviour of the two different TextWatcher-implementations inside the RecyclerView was that I called removeTextChangedListenerand addTextChangedListenerinside their afterTextChanged-methods to avoid retriggering of the afterTextChanged-method.
The best way to avoid retriggering is a simple check if the text changed since the last call:
public class DecimalFilterInteger implements TextWatcher {
private final static String TAG = ConfigurationAdapter.class.getSimpleName();
private final EditText mEditText;
private String mLastTextValue = new String("");
// ...
#Override
public synchronized void afterTextChanged(final Editable text) {
String strInput = text.toString().trim();
if(strInput.isEmpty()) {
return;
}
if(strInput.equals(mLastTextValue)) { // return when same value as last time to avoid endless loop
return;
}
if ((strInput.charAt(0) == '.')) { // handle dot at beginning
strInput = "";
}
if(strInput.contains(".")){ // cut trailing comma
String numberBeforeDecimal = strInput.split("\\.")[0];
strInput = numberBeforeDecimal;
}
//mEditText.removeTextChangedListener(this); // CAUSE OF SWAP-ERROR !!!
mEditText.getText().clear(); // do not use setText here to avoid changing the keyboard
mEditText.append(strInput); // back to default (e. g. from 123-mode to abc-mode),
// see: http://stackoverflow.com/questions/26365808/edittext-settext-changes-the-keyboard-type-to-default-from-123-to-abc
mLastTextValue = mEditText.getText().toString();
mEditText.setSelection(mEditText.getText().toString().trim().length());
//mEditText.addTextChangedListener(this); // CAUSE OF SWAP-ERROR !!!
}
}

Update activity's TextView from EditText value within ListView's item

So, I have an activity with a TextView and a ListView with a custom BaseAdapter. This activity looks like this:
As you can see, every item of the list is a custom layout and the basic idea is: every time the numeric EditText within it changes, the "total" TextView from the activity (which is the sum of the prices of every product) must be updated as well.
I suppose it must somehow be done from the Adapter class, but I don't know how to do it.
My Activity file looks like this (it gets products data from server via "GetCollectionProducts" AsyncTask, where I set the adapter):
public class ProductAisleActivity extends AppCompatActivity implements View.OnClickListener{
ListView productList;
Button participate;
ImageButton search;
EditText searchET;
TextView productsTotal;
Product[] colProducts;
RelativeLayout collectionHeader;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_aisle);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
/* ...
Irrelevant code to this question
*/
productsTotal = (TextView) findViewById(R.id.products_aisle_total);
productsTotal.setText(
getResources().getString(
R.string.productsTotal,
String.valueOf(0.00)
)
);
productList = (ListView) findViewById(R.id.products_aisle_list);
new GetCollectionProducts().execute();
}
private class GetCollectionProducts extends AsyncTask<Void,Void,JSONArray>{
#Override
protected JSONArray doInBackground(Void... voids) {
/* Irrelevant code to this question */
}
#Override
protected void onPostExecute(JSONArray jsonArray) {
/* Irrelevant code to this question */
productList.setAdapter(
new CollectionProductsAdapter(
ProductAisleActivity.this,
colProducts
)
);
}
}
And my Adapter file looks as follows:
public class CollectionProductsAdapter extends BaseAdapter {
Context context;
ProductAisleActivity.Product[] data;
private static LayoutInflater inflater = null;
public CollectionProductsAdapter(Context context, ProductAisleActivity.Product[] data) {
this.context = context;
this.data = data;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.length;
}
#Override
public Object getItem(int i) {
return data[i];
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
if (v == null) {
v = inflater.inflate(R.layout.product_row_layout, null);
}
ProductAisleActivity.Product product = data[i];
/* ...
Irrelevant code to this question
*/
EditText productQuantity = (EditText) v.findViewById(R.id.productQuantity);
productQuantity.setText("0");
return v;
}
}
I'm stuck at this point, any help will be appreciated.
First you need to listen for any changes in the EditText so you can handle things dynamically without explicitly using something like a submit button. You can do this with a TextWatcher.
productQuanity.addTextChangedListener(new TextWatcher() {
private double originalCost = 0.0;
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Before we change the text, set the originalCost
// so we can know what the change is after the edit
originalCost = getCost(s.toString());
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// You don't need to utilize this method
}
#Override
public void afterTextChanged(Editable s) {
// After the change has taken place in the text,
// get the new cost and calculate the difference
double newCost = getCost(s.toString());
double changeInCost = newCost - originalCost;
}
private double getCost(String input){
String count = input.toString();
if(TextUtils.isEmpty(count))
return 0.0;
else
return (double) Integer.parseInt(count) * product.getPrice();
}
});
Now that we have the change in cost, what do we do with it? We need to notify the activity that we have a change. We can do that with an observer, which is fine, but for fun let's use an interface to implement a listener.
Modify your adapter class
public class CollectionProductsAdapter extends BaseAdapter {
public interface CostChangedListener{
void onCostChanged(double change);
}
Context context;
ProductAisleActivity.Product[] data;
private LayoutInflater inflater = null; // THIS SHOULDN'T BE STATIC
CostChangedListener listener;
public CollectionProductsAdapter(Context context, ProductAisleActivity.Product[] data, CostChangedListener listener) {
this.context = context;
this.data = data;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.listener = listener;
}
// The rest of your code
}
Now when we update the cost in our TextWatcher we can call
if(listener != null)
listener.onCostChanged(changeInCost);
Last, to make sure we utilize this correctly, we will need to pass a listener in our CollectionProductsAdapter constructor
productList.setAdapter(new CollectionProductsAdapter(
ProductAisleActivity.this, colProducts,
new CostChangeListener(){
#Override
public void onCostChanged(double change){
double currentTotal = Double.valueOf(productTotal.getText());
double newTotal = currentTotal + change;
productTotal.setText(String.valueOf(newTotal));
}));
Obviously you may need to tweak some of this to get it to match perfectly, and I haven't tested it so some things might be off a bit, but this should get you going in the right direction. If you have any issue feel free to comment and I will try to help you through it.
Notes
Do not keep a static reference like you were with your layout inflater
It is worth taking a look at the RecyclerView or at least the ViewHolder pattern with an Adapter
You want to add a textChangedListener for changing item values as user changes values in EditText.
You can use TextChangedListener here:
EditText myEditTextField = new EditText(this);
myEditTextField.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
You can perform tasks according to your need. It has 3 methods:
1.beforeTextChanged
2.onTextChanged
3.afterTextChanged
So you can get your task done by the help of "afterTextChanged". You have to simply call your method of calculating price for no. of items when user enters a particular number.And it will show you the price as you want.
Hope this help!

Categories

Resources