Not able to add footer item in Recycler View dynamically - android

I am creating a list in which each list item has 3 widgets i.e. 2 EditText and 1 Spinner. And I have one add button to add a new view in the list.
I have created two views one is header view that has all 3 widgets with cross button and footer view that has all 3 widgets with the add button.
Initially, my footer view is shown. When I enter a value on all 3 widgets of that footer view I want to add those values into a model without clicking on it. Now it is working fine when I click on add button but I want to add it without clicking on add button when all values are filled.
Please help.
if (holder instanceof FooterViewHolder) {
final FooterViewHolder footerHolder = (FooterViewHolder) holder;
unitlist = new ArrayList<>();
// unitlist.add(0, "Select");
unitlist.add(0, "unit");
unitlist.add(1, "gram");
unitlist.add(2, "litre");
spinnerAdapter = new ArrayAdapter<String>(activity, R.layout.spinner, unitlist) {
#Override
public boolean isEnabled(int position) {
return true;
}
};
spinnerAdapter.setDropDownViewResource(R.layout.spinner_dropdown);
((FooterViewHolder) holder).spinner.setAdapter(spinnerAdapter);
//spinner select type of life stage
((FooterViewHolder) holder).spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
unitName = parent.getItemAtPosition(position).toString();
selected_item = ((FooterViewHolder) holder).spinner.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
//Another interface callback
}
});
footerHolder.ibAddMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (footerHolder.etIngredientName.getText().length() == 0) {
Toast.makeText(activity, R.string.pls_enter_ingredient_name, Toast.LENGTH_SHORT).show();
} else if (((FooterViewHolder) holder).spinner.getSelectedItem().toString().trim().equals("Select")) {
Toast.makeText(activity, R.string.pls_enter_unit_conversion_value, Toast.LENGTH_SHORT).show();
} else if (footerHolder.etAmount.getText().length() == 0) {
Toast.makeText(activity, R.string.pls_enter_qty, Toast.LENGTH_SHORT).show();
} else {
ArrayList<ModelInventory> modelInventoryArrayList = new ArrayList<>();
modelInventoryArrayList = databaseHelper.getInventoryByName(footerHolder.etIngredientName.getText().toString());
Log.e("TAG", "checkUnit:d "+footerHolder.etAmount.getText().toString() + "))" +modelInventoryArrayList.size());
if(modelInventoryArrayList.size() >0){
for(ModelInventory modelInventory : modelInventoryArrayList){
Log.e("TAG", "checkUnit: "+modelInventory.getItemQty() +")))"+unitName );
if(modelInventory.getItemQty().equals(unitName)){
fragmentNewRecipe.addView(footerHolder.etAmount.getText().toString(), position, footerHolder.etIngredientName.getText().toString(), unitName);
}else{
Toast.makeText(activity,R.string.measurement_diff,Toast.LENGTH_SHORT).show();
}
}
}else{
fragmentNewRecipe.addView(footerHolder.etAmount.getText().toString(), position, footerHolder.etIngredientName.getText().toString(), unitName);
}
}
}
});
}
View:

Try adding listeners to each edit text and whenever an input change detected, Check whether all inputs are non empty, and add to your list
footerHolder.etAmount.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (s.toString() != ""){
if (footerHolder.etIngredientName.getText().length() != 0) {
//Add item to your list
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
footerHolder.etIngredientName.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (s.toString() != ""){
if (footerHolder.etAmount.getText().length() != 0) {
//Add item to your list
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});

Related

Andriod EditText lose focus after clicking on Expandable list view parent items

I have expandable list view each view has multiple text views. For keep tracking of edit text focus and its data I am calling setOnFocus listener. But it is working only when I change my textview focus. When I select or click New Parent item I am losing its focus.
Code Sample
final ViewHolder holder;
#Override
public View getChildView(final int parent, final child, boolean b, View view, ViewGroup viewGroup) {
holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
if (view == null) {
holder = new ViewHolder();
holder.editText = view.findViewById(R.id.txtRecordComment);
} else {
holder = (ViewHolder) view.getTag();
}
holder.editText.setText(map.get("ed_" + parent + "" + child));
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(!hasFocus)
map.put("ed_" + parent + "" + child, holder.editText.getText().toString());
}
});
}
Code working fine if I change focus of editText but it fails to store if I click on next parent item
Is their any way that I can use Key Up function??
You have to implement TextWatcher class if you would like use onKeyUp function.
EditText tv_filter = (EditText) findViewById(R.id.filter);
TextWatcher fieldValidatorTextWatcher = new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (filterLongEnough()) {
populateList();
}
}
private boolean filterLongEnough() {
return tv_filter.getText().toString().trim().length() > 2;
}
};
tv_filter.addTextChangedListener(fieldValidatorTextWatcher);

How to update TextView of custom ArrayAdapter on the basis of EditText?

I have a custom ArrayAdapter which is a horizontal LinearLayout containing 02 EditText fields and 01 TextView.....all are set to take/show numberDecimals.
All I want is that whenever a number is input in any of the EditText fields, the product of the two numbers (EditText 1 & EditText 2) is shown in TextView.
I've tried both OnFocusChangedListener and TextChangedListener but the TextView doesnt get updated during live entries. It doesnt show anything.
I'm new to programming, so cant find any other solution for that.
Thanks in advance!
Below is the Adapter code:
TextView labelView;
EditText ratioView;
EditText rateView;
TextView avgView;
public FibresAdapter(Activity context, ArrayList<Fibres> fibresList) {
super(context, 0, fibresList);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
final Fibres currentFibre = getItem(position);
labelView = (TextView) convertView.findViewById(R.id.label);
labelView.setText(currentFibre.fibreLabel);
ratioView = (EditText) convertView.findViewById(ratio);
rateView = (EditText) convertView.findViewById(R.id.rate);
avgView = (TextView) convertView.findViewById(R.id.avg);
ratioView.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) {
if (s.toString().length() > 0) {
currentFibre.fibreRatio = Double.parseDouble(s.toString());
avgView.setText("Testing");
} else {
currentFibre.fibreRatio = 0;
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
rateView.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) {
if (s.toString().length() > 0) {
currentFibre.fibreRate = Double.parseDouble(s.toString());
avgView.setText("Testing");
} else {
currentFibre.fibreRate = 0;
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
return convertView;
}
Below is the code for Activity:
double totalAvg;
double totalRatio;
TextView totalAvgView;
TextView totalRatioView;
static ArrayList<Fibres> fibres;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fibres_list);
fibres = new ArrayList<>();
int x = 0;
while (x < Fibres.fibCatArray.length) {
fibres.add(new Fibres(Fibres.fibCatArray[x]));
x++;
}
ListView listView = (ListView) findViewById(R.id.list);
FibresAdapter adapter = new FibresAdapter(this, fibres);
listView.setAdapter(adapter);
totalRatioView = (TextView) findViewById(R.id.total_ratio);
totalAvgView = (TextView) findViewById(R.id.total_avg);
}
public void displayTotals() {
totalRatio = 0;
totalAvg = 0;
for (Fibres materials : fibres) {
totalRatio += materials.fibreRatio;
totalAvg += (materials.fibreRatio / 100 * materials.fibreRate);
}
totalRatioView.setText(totalRatio + "");
totalAvgView.setText(totalAvg + "");
}
public boolean validateData() {
if (totalRatio != 100) {
Toast.makeText(this, "Total should be 100%!", Toast.LENGTH_SHORT).show();
return false;
}
for (Fibres materials : fibres) {
if (materials.fibreRatio > 1 && materials.fibreRate <= 0) {
Toast.makeText(this, "Enter both Ratio & Rate!", Toast.LENGTH_SHORT).show();
return false;
}
}
return true;
}
In your adapter add addTextChangedListener to both EditText and use textView.setText() inside that. Something like this.
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() != 0)
textView.setText("");
}
});

How to get text value from Listview Edit Text feild.....?

How to get the values from the edit text .
Hello everyone im facing a problem with android task in which there so many edit text available in the edit text .
i need to get the values from 1 to 4 all edit texts ..?
Below is my get view method of Adapter.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.social_media_row, null);
holder = new ViewHolder();
holder.social_id = (EditText) convertView.findViewById(R.id.socialMediaId);
convertView.setTag(holder);
holder.social_id.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) {
Log.i("AfterTextChange",holder.social_id.getText().toString());
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
SocialMediaModel contact = contactsList.get(position);
holder.social_id.setText(contact.getSocialMediaId());
holder.social_id.setTag(contact);
return convertView;
}
Update : i want to get the values when user click on the button
savebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/here what i can do
}
});
//at top Globally declare it
private String[] valueList = new String[];
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.social_media_row, null);
holder = new ViewHolder();
holder.social_id = (EditText) convertView.findViewById(R.id.socialMediaId);
convertView.setTag(holder);
holder.social_id.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) {
valueList[pos] = s.toString();
}
#Override
public void afterTextChanged(Editable s) {
Log.i("AfterTextChange",holder.social_id.getText().toString());
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
SocialMediaModel contact = contactsList.get(position);
holder.social_id.setText(contact.getSocialMediaId());
holder.social_id.setTag(contact);
return convertView;
}
// method to get Values
public String[] getValueList(){
return valueList;
}
Now on button click event put this code
String[] myvl = yourAdapteName.getValueList();
You can implement a method like this one for each EditText with the list position as parameter :
public String getValueFromFirstEditText(int position){
//here you need to recreate the id for the first editText
String result = textValues.get("theFirstEditTextAtPos:"+position);
if(result ==null)
result = "default value";
return result;
}
i hope this code help you
USE Adapter cell position
follow this code
in getView(in Adapter Class)
edittext.getText().toString();
Declare this in your declaration
protected AdapterView.OnItemClickListener messageClickedHandler;
Click event for the list view using Adapter view
messageClickedHandler = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, int position, long id) {
String strName=rowItems.get(position).getStrName();
Toast.makeText(getApplicationContext(),"Name : "+strName , Toast.LENGTH_LONG).show();
}
};
list.setOnItemClickListener(messageClickedHandler);
}
rowItems is an array list Object , getStrName() from the user defined class.

Getting item position in AutoCompleteTextView addTextChangeListener method

I have Autocomplete textviews in my custom adapter. when i write something in onTextChange listener, i want to get item position. I it gives all items positions. Please help me regarding this issue. Thanks!
autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
String selection = (String)parent.getItemAtPosition(position);
//TODO Do something with the selected text
}
});
This is how you can get position of Item you typed in AutoCompleteTextView. But you have to add the full name of item like "apple" or "apples" then this code will give you the position of that particular item. But its very expensive process if you have lots of items in your list.
public class Test extends AppCompatActivity {
private AutoCompleteTextView autoCompleteTextView;
private List<String> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chintan_test);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
list = new ArrayList<>();
list.add("Apple");
list.add("Apples");
list.add("Banana");
list.add("Aunt");
list.add("Orange");
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
autoCompleteTextView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));
autoCompleteTextView.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) {
for (String string : list)
if (string.toLowerCase().equals(s.toString().toLowerCase())) {
int pos = list.indexOf(string);
Toast.makeText(Test.this, "" + pos, Toast.LENGTH_SHORT).show();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
}
Let me know if this code helps you.
Use textwatcher, implement it in activity class.
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
hi can you please check there is some issue
AutoCompleteTextView tv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
final ArrayList<Student> list = new ArrayList<MainActivity.Student>();
list.add(new Student("abc"));
list.add(new Student("abc"));
ArrayAdapter<Student> adapter = new ArrayAdapter<MainActivity.Student>(
this, android.R.layout.simple_dropdown_item_1line, list);
tv.setAdapter(adapter);
tv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Student selected = (Student) arg0.getAdapter().getItem(arg2);
Toast.makeText(MainActivity.this,
"Clicked " + arg2 + " name: " + selected.name,
Toast.LENGTH_SHORT).show();
}
});
for more checking you can go from here...
you can use addTextChangedListener for this purpose
yourAutoCompleteTextView.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
} });

Get original ID from filtered Listview

I have a class in where I can put in a StringArray, and it shows the list with a search box to filter contents. My application is prone that every field has a set ID. However, when I search for a value, the ids change into the filtered list. Is there a way to get the id from the original list, so that even in the filtered list I can have ids like 9, 14 instead of 0, 1, 2....
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , searchItems));
ed.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textlength = ed.getText().length();
arr_sort.clear();
for (int i = 0; i < searchItems.length; i++) {
if (textlength <= searchItems[i].length()) {
if (ed.getText().toString().equalsIgnoreCase((String) searchItems[i].subSequence(0, textlength))) {
arr_sort.add(searchItems[i]);
}
}
}
lv1.setAdapter(new ArrayAdapter<String>(Rest_Search.this, android.R.layout.simple_list_item_1, arr_sort));
// lv1.smoothScrollToPosition(20);
}
});
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View v,
int position, long id) {
// String a = parent.getItemAtPosition(position);
// menuID = (int) id;
int a = (int) parent.getAdapter().getItemId(position);
// menuID = (int) parent.getAdapter().getItemId(position);
Toast.makeText(Rest_Search.this, ""+a, Toast.LENGTH_SHORT).show();
You can customize your adapter. create a new Class extends BasaAdapter and Override the getItemId method:
#Override
public long getItemId(int position) {
return items[position].getID();
}
and then in onItemClick use item ids.
In your customized adapter use getItemId(pos)
you can obtain pos overriding onClick method of View.OnClickListener()
here an example
private View.OnClickListener mOnAddContactClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
final int position = mListView.getPositionForView(view);
Log.v(JBOOK,"original id "+getItemId(position));

Categories

Resources