Android: Update ListView from EditText - android

friends.
I have an app module that implements State pattern. Idea is:
1. User enter the Date -->
2. Screen changes to a ne state: dynamically inflates ListView and asks user to fill the list with some data
Problem is that when I call notifyDataSetChange() from update method 2nd time, EditText view doens't want to do its functions anymore.
This is a string from a logcat, that I think contains key of my problem.
09-10 10:33:38.937 26954-26954/com.example.android.turtleinfo D/ListView: change accessibility focus position = 0
Here are codes for everything.
1. My State Pattern Super Class
public abstract class ScreenState
{
public BuilderActivity activity;
public ScreenState(BuilderActivity activity) {
this.activity = activity;
//initializeScreen(activity);
}
public abstract void initializeScreen(Activity activity);
public abstract void switchState();
public abstract void updateText(View view);
public void addTextWatcher(EditText text) {
text.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
activity.state.updateText(v);
return true;
}
return false;
}
});
}
}
2. Implementation of initialize screen and updateData methods
#Override
public void initializeScreen(Activity activity) {
((BuilderActivity) activity).editText.setHint("Name");
addTextWatcher(((BuilderActivity) activity).editText);
textView = (TextView)activity.getLayoutInflater().inflate(R.layout.schedule_text_style, null);
textView.setHeight(100);
screenText = "End Date: " + new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH).format(((BuilderActivity) activity).data.getEndDate())+"\n\n";
textView.setText(screenText+"\n");
((BuilderActivity) activity).scheduleLayout.addView(textView);
((BuilderActivity) activity).data.setNames(new ArrayList<String>());
lv = (ListView)activity.getLayoutInflater().inflate(R.layout.listview_text_style, null);
listItems = new ArrayList<>();
adapt = new ArrayAdapter<>(super.activity, R.layout.schedule_text_style, listItems);
lv.setAdapter(adapt);
((BuilderActivity) activity).scheduleLayout.addView(lv);
}
#Override
public void updateText(View view) {
String text = activity.editText.getText().toString();
if (text.length() >= 2) {
if (!activity.data.names.contains(text)) {
activity.data.addToList(text);
listItems.add(text);
adapt.notifyDataSetChanged();
activity.editText.setText("");
counter++;
activity.editText.setText("");
} else {
Toast.makeText(activity, "same names are not allowed",
Toast.LENGTH_LONG).show();
((BuilderActivity) activity).editText.setText("");
}
}
else
{
Toast.makeText(activity, "name should contain 2 or more symbols",
Toast.LENGTH_LONG).show();
((BuilderActivity) activity).editText.setText("");
}
}
This is how it looks on device.
1. First insertion is ok
2. And second and further insertionas are impossible. OK softkeyboard button switches first to NEXT button
3. If I click next, EditText looses focus, nothing happens, but NEXT button Switches to '/n' button.
So, please, if anyone know, what am I doing worng, help!
Thanks in advance!

Your problem is in
public void addTextWatcher(EditText text)
function.
Look at the android:imeActionId and android:imeOptions attributes, plus the setOnEditorActionListener() method, all on TextView/Edittext.
You can also change the text of the keyboard button by using this.
mEditText.setImeActionLabel("Your text", KeyEvent.KEYCODE_ENTER);

Related

Transformations.distinctUntilChanged() not working with equals() override

My Aim: I would like to use the Transformations.distinctUntilChanged() method to create an application which shows a toast message every time a LiveData object is updated. The LiveData object is a simple custom Integer object (MyInteger) that increments by one every time a button is clicked. The application should stop showing the toast message when myInteger exceeds 5. To implement this >5 condition, the application must override the equals() method of MyInteger class (the equals() method is called in the distinctUntilChanged() class).
The Problem: In the below code, the currentValue and previousValue fields are always the same (except for first time button is clicked). Hence onChanged is not called in MainActivity and the Toast message does not show from the second click onwards. See below for current result vs. what i expect should happen.
Please help. Again the application must use distinctUntilChanged() and overridden equals() methods together.
Current result:
First time button clicked: currentValue=1 and previousValue=null, Toast Message shows.
Second time button clicked: currentValue=2 and previousValue=2, Toast Message does not show.
Third time button clicked: currentValue=3 and previousValue=3, Toast Message does not show.
Fourth time button clicked: currentValue=4 and previousValue=4, Toast Message does not show.
and so on...
Desired result:
First time button clicked: currentValue=1 and previousValue=null, Toast Message shows.
Second time button clicked: currentValue=2 and previousValue=1, Toast Message shows.
Third time button clicked: currentValue=3 and previousValue=2, Toast Message shows.
Fourth time button clicked: currentValue=4 and previousValue=3, Toast Message shows.
and so on...
Blockquote
public class MainActivity extends AppCompatActivity {
private MainActivityViewModel mMainActivityViewModel;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.my_button);
MyInteger myInteger = new MyInteger(0);
mMainActivityViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myInteger.incrementByOne();
mMainActivityViewModel.getLiveData().setValue(myInteger);
}
});
Transformations.distinctUntilChanged(mMainActivityViewModel.getLiveData()).observe(this, new Observer<MyInteger>() {
#Override
public void onChanged(MyInteger myInteger) {
Log.wtf("Transformations onChanged:", String.valueOf(myInteger.integer));
Toast.makeText(MainActivity.this, "Transformations onChanged:"+String.valueOf(myInteger.integer), Toast.LENGTH_SHORT).show();
}
});
}
}
Blockquote
public class MainActivityViewModel extends AndroidViewModel {
public MutableLiveData<MyInteger> myIntegerMutableLiveData;
public MainActivityViewModel(#NonNull Application application) {
super(application);
}
public MutableLiveData<MyInteger> getLiveData() {
if (myIntegerMutableLiveData == null) {
myIntegerMutableLiveData = new MutableLiveData<>();
}
// MediatorLiveData transformedLiveData = (MediatorLiveData) Transformations.distinctUntilChanged(myIntegerMutableLiveData);
return myIntegerMutableLiveData;
}
}
Blockquote
public class MyInteger {
public int integer;
public MyInteger(Integer integer) {
this.integer = integer;
}
public void incrementByOne() {
integer++;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyInteger myInteger = (MyInteger) o;
if ((myInteger.integer <= 5) && (integer == myInteger.integer)) {return true;}
if ((myInteger.integer <= 5) && (integer != myInteger.integer)) {return false;}
return (myInteger.integer > 5);
}
#Override
public int hashCode() {
return Objects.hash(integer);
}
}

I can't get the last state of an EditText

I realized that the UI elements listeners are called from top down order...
(OnTouch -> OnFocusChange -> OnClick.)
So i had a setOnFocusChangeListener in the following way:
editTextPhoneNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
clearIfNotMatch((EditText) v, new Integer[]{12, 13});
}
}
});
This method checks if the field was inserted correctly, if it is wrong the content of this field is deleted.
In the setOnClickListener method that where I perform the field validation it is not taking the final value of the field ... it's like it was still filled!!!
buttonRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//buttonRegister.requestFocus();
startValidation();
if (validation.isValid()) {
showsNewDialog();
}
}
});
If i click on another edittext before clicking the button the validation works.
If I click directly on the button I cannot validate correctly
Code of startValidation:
private void startValidation() {
validation= new AwesomeValidation(ValidationStyle.BASIC);
validation.addValidation(editTextNumberOfSections, new FieldRequired(), getString(R.string.validation_field_required));
validation.addValidation(editTextPhoneNumber, new FieldRequired(), getString(R.string.validation_field_required));
}
Please help me!

Is there a way to reset the OnClickListener to its default Android implementation?

I have an editText to which I have set an OnClickListener, which is set to open a Dialog. But I have an option in the Dialog to let the user enter data into the editText by manually typing. I tried calling setOnClickListener(null), but it makes the editText unresponsive.
As of yet I have tried a lot of things, but the only thing that works is recreating the activity by calling recreate(), but I'd rather the user not know that I'm recreating the Activity.
How do I reset the editText to behave normally like an Android editText works? (like opening the keyboard and entering data on tapping it)
Change
editText.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
//do as u wish
}
}
);
to
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// do as you wish
}
}
});
You can use a boolean member variable to keep track of when to allow user input and when to show the dialog.
private boolean mShouldAllowInput = false;
Then in your custom click listener you could do something like:
private View.OnClickListener editClickListener = new View.OnClickLIstener() {
#Override
public void onClick(View v) {
if(!mShouldAllowInput) {
showDialog();
mShouldAllowInput = true;
}
}
}
Now you need a way to revert back the value of the boolean member variable back to false. You can reset in the DialogInterface.OnClickListener as per your business logic.
To reset the OnClickListener of an EditText I tried:
View.OnClickListener defaultOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
v.requestFocus();
}
};
...
editText.setOnClickListener(defaultOnClickListener);
and it seems to work fine!

Checkboxes not changing checked status in UI?

I have a ListView that I am trying to use with a checkable item list. I am calling the toggle() method on my list item class in the ArrayAdaptor, but the checkbox is not being ticked in the UI. However, I can confirm that the correct items are being selected in the UI, and that the "isChecked()" status reports back correctly--just the UI doesn't change at all. Are there any special methods I need to call to update the checkbox graphic for the UI?
To put it another way--how do I programmatically tell the UI that a checkbox should show up as "checked"? It seems this should be a very simple process, but I've been having a lot of trouble finding this information.
Code is as follows:
For the item data class in the ArrayAdaptor:
public class SelectedItemData extends CheckedTextView {
public String _item_name;
public String getItemName()
{
return _item_name;
}
public void setItemName(String in_name)
{
_item_name = in_name;
}
// methods
public SelectedItemData(Context context) {
super(context);
init();
}
public void init()
{
this._item_name = "UNSET";
this.setChecked(false);
}
#Override
public String toString()
{
return _item_name;
}
}
In the Activity class (located within the onCreate method):
_selectedItemsListView = (ListView) findViewById(R.id.selected_items_listview);
_selectedItemsListView.setItemsCanFocus(false);
_selectedItemsListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
_selectedItemsListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listview, View view, int position, long id) {
#SuppressWarnings("unchecked")
ArrayAdapter<SelectedItemData> itemsAdapter = (ArrayAdapter<SelectedItemData>)_selectedItemsListView.getAdapter();
SelectedItemData selectedItem = itemsAdapter.getItem(position);
selectedItem.toggle();
Toast.makeText(view.getContext(), "Item " + selectedItem.getItemName() + " Selected!", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Is Item Checked? " + selectedItem.isChecked());
_selectedItemsListView.setAdapter(itemsAdapter);
}
});
Any guidance on how to enable the UI to properly display that one of the items have been selected/checked would be great. Thanks!
You have to update your adapter with the new item and set it to the listview. (itemsAdapter.setItem(item,position))

Android: Button within ListView not receiving onClick events

I am making a date picker activity that looks like a scrolling 30 day month/calendar (think Outlook calendar). The date picker contains a ListView (for scrolling) of MonthView views each of which is a TableView of the individual days. Each individual day in the MonthView is a button. When the MonthView is instantiated I walk each of the days (buttons) and attach a click listener:
final Button b = getButtonAt(i);
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
setSelectedDate(buttonDayClosure, b);
}
});
setSelectedDate does a variety of things, but it also turns the button's background to yellow to signify the date is selected.
On my emulator, everything works as you would expect. Activity comes up, you press a day, the day turns yellow. No problems.
However, on some of my peer's emulators and on physical devices when you touch a day nothing happens... until you scroll the ListView... and then all of a sudden the selected day turns yellow. So, for example, you touch "the 3rd" and then nothing happens. Wait a few seconds and then scroll the ListView (touching an area of the calendar that is NOT the 3rd) and as soon as ListView scrolls the 3rd magically turns yellow.
On my peer emulators that show this behavior, I can set a breakpoint on the fist line of onClick and I see that the BP is in fact not hit until the ListView is scrolled.
This behavior doesn't make any sense to me. I would expect the onClick behavior to be unrelated to the encapsulating View's scrolling efforts.
Any thoughts on why this might be the case and how I can rectify the situation so that onClicks always happen immediately when the button is touched?
Thanks.
Post Scriptus: ArrayAdapter and ListView code requested:
public class MonthArrayAdapter extends ArrayAdapter<Date> {
private MonthView[] _views;
private Vector<Procedure<Date>> _dateSelectionChangedListeners = new Vector<Procedure<Date>>();
public MonthArrayAdapter(Context context, int textViewResourceId, Date minSelectableDay, Date maxSelectableDay) {
super(context, textViewResourceId);
int zeroBasedMonth = minSelectableDay.getMonth();
int year = 1900 + minSelectableDay.getYear();
if(minSelectableDay.after(maxSelectableDay))
{
throw new IllegalArgumentException("Min day cannot be after max day.");
}
Date prevDay = minSelectableDay;
int numMonths = 1;
for(Date i = minSelectableDay; !sameDay(i, maxSelectableDay); i = i.addDays(1) )
{
if(i.getMonth() != prevDay.getMonth())
{
numMonths++;
}
prevDay = i;
}
_views = new MonthView[numMonths];
for(int i = 0; i<numMonths; i++)
{
Date monthDate = new Date(new GregorianCalendar(year, zeroBasedMonth, 1, 0, 0).getTimeInMillis());
Date startSunday = findStartSunday(monthDate);
this.add(monthDate);
_views[i] = new MonthView(this.getContext(), startSunday, minSelectableDay, maxSelectableDay);
zeroBasedMonth++;
if(zeroBasedMonth == 12)
{
year++;
zeroBasedMonth = 0;
}
}
for(final MonthView a : _views)
{
a.addSelectedDateChangedListener(new Procedure<MonthView>()
{
#Override
public void execute(MonthView input) {
for(final MonthView b: _views)
{
if(a != b)
{
b.clearCurrentSelection();
}
}
for(Procedure<Date> listener : _dateSelectionChangedListeners)
{
listener.execute(a.getSelectedDate());
}
}
});
}
}
void addSelectedDateChangedListener(Procedure<Date> listener)
{
_dateSelectionChangedListeners.add(listener);
}
private boolean sameDay(Date a, Date b)
{
return a.getYear() == b.getYear() && a.getMonth() == b.getMonth() &&
a.getDate() == b.getDate();
}
#Override
public View getView (int position, View convertView, ViewGroup parent)
{
return _views[position];
}
private Date findStartSunday(Date d)
{
return d.subtractDays(d.getDay());
}
public void setSelectedDate(Date date)
{
for(MonthView mv : _views)
{
mv.setSelectedDate(date);
}
}
}
and
public class DatePicker extends ActivityBase {
public static final String CHOSEN_DATE_RESULT_KEY = "resultKey";
public static final String MIN_SELECTABLE_DAY = DatePicker.class.getName() + "MIN";
public static final String MAX_SELECTABLE_DAY = DatePicker.class.getName() + "MAX";
private static final String SELECTED_DATE = UUID.randomUUID().toString();
private long _selectedDate = -1;
private MonthArrayAdapter _monthArrayAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Date now = new Date();
Bundle inputs = this.getIntent().getExtras();
long min = inputs.getLong(MIN_SELECTABLE_DAY, 0);
Date minSelectableDate;
if(min == 0)
{
minSelectableDate = new Date(now);
}
else
{
minSelectableDate = new Date(min);
}
Log.i(DatePicker.class.getName(), "min date = " + minSelectableDate.toString());
long max = inputs.getLong(MAX_SELECTABLE_DAY, 0);
Date maxSelectableDate;
if(max == 0)
{
maxSelectableDate = new Date(now.addDays(35).getTime());
}
else
{
maxSelectableDate = new Date(max);
}
setContentView(R.layout.date_picker);
Button doneButton = (Button) findViewById(R.id.DatePickerDoneButton);
if(doneButton == null)
{
Log.e(this.getClass().getName(), "Could not find doneButton from view id.");
finish();
return;
}
doneButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
Intent result = new Intent();
result.putExtra(CHOSEN_DATE_RESULT_KEY, _selectedDate);
setResult(RESULT_OK, result);
finish();
}
});
Button cancelButton = (Button) findViewById(R.id.DatePickerCancelButton);
if(cancelButton == null)
{
Log.e(this.getClass().getName(), "Could not find cancelButton from view id.");
finish();
return;
}
cancelButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
setResult(RESULT_CANCELED, null);
finish();
}
});
ListView lv = (ListView)findViewById(R.id.DatePickerMonthListView);
lv.setDividerHeight(0);
_monthArrayAdapter =
new MonthArrayAdapter(this, android.R.layout.simple_list_item_1, minSelectableDate, maxSelectableDate);
_monthArrayAdapter.addSelectedDateChangedListener(new Procedure<Date>()
{
#Override
public void execute(Date input) {
_selectedDate = input.getTime();
}
});
lv.setAdapter(_monthArrayAdapter);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
if(savedInstanceState.containsKey(SELECTED_DATE))
{
_selectedDate = savedInstanceState.getLong(SELECTED_DATE);
_monthArrayAdapter.setSelectedDate(new Date(_selectedDate));
}
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
savedInstanceState.putLong(SELECTED_DATE, _selectedDate);
}
}
Having the same problem, looking for an answer. I totally didn't believe it when I didn't get my onClick method until I scrolled my list. I'll post the answer here if I find it.
Right now, my best guess is to try different events besides click (because the scroll space is eating the complex touch events that turn into a click event):
"downView" is a static variable to track the element being clicked.
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
downView = v;
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (downView == v) {
handleClick(v);
return true;
}
downView = null;
}
return false;
}
});
The main reason is that ListView doesn't like an adapter having an array of views.
So the problem is triggered by
public View getView (int position, View convertView, ViewGroup parent)
{
return _views[position];
}
When looking at the ListView code (or rather it's parents AbsListView.obtainView method) you'll see code like
if (scrapView != null) {
...
child = mAdapter.getView(position, scrapView, this);
...
if (child != scrapView) {
mRecycler.addScrapView(scrapView);
It can happen that getView(position,...) is called with scrapView != _views[position] and hence scrapView will be recycled. On the other hand, it is quite likely that the same view is also added again to ListView, resulting in views having a weird state (see this issue)
Ultimately, this should be fixed in ListView IMO, but temporarily, I advise against using an adapter containing an array of views.
So I'll add a completely separate answer to this outside of manually composing your own click events from touch events.
I traded some emails with the Android Team (there's a few perks from being consumed by the googly) and they suggested that my attempt to implement ListAdapter by hand was inefficient and that if I don't correctly hook up the data observer methods of the adapter it can cause "funny problems with event handling."
So I did the following:
1) Replaced my implementation of ListAdapter with a subclass of BaseAdapter that overrode the necessary functions.
2) Stopped using list.invalidateViews() and started using adapter.notifyDataChanged()
and the bug seems to have gone away.
That's more work than manually composing a click event, but it's also more correct code in the long run.
Aswer is:
public View getView(int position, View convertView, ViewGroup parent) {
View v=makeMyView(position);
v.setFocusableInTouchMode(false);
return v;
}

Categories

Resources