I have a spinner and when an item is selected i want to recreate the activity. But when the activity is recreated it keeps constantly recreating because the new itemSelectedListener is triggered. I have fixed the bug but i am interested in why this is happening. Thank you in advance for any insights you offer.
sp_lang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
recreate();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
You can avoid the first event with a flag in the OnItemSelectedListener.
sp_lang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
Boolean firstEventConsumed = false;
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (firstEventConsumed) {
recreate();
} else {
firstEventConsumed = true;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
EDIT
I have found a solution but it is just a workaround. It is not a definitive solution.
It has a strange behaviour when you recreate the activity. When the activity is created for the first time, it doesn't call twice the onItemSelected, but when it is recreated it is called twice.
What I have done below is to control when the item is selected by the user (handling the touch event) and when it is done by the activity.
public class MainActivity extends AppCompatActivity {
private Boolean isUserAction = false;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get the spinner
// create the adapter
spinner.setAdapter(spinnerAdapter);
spinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
isUserAction = true;
return false;
}
});
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (isUserAction) {
recreate();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// do nothing
}
});
}
}
Related
ArrayAdapter<String> deptAdapter = new ArrayAdapter<>(Courses.this, R.layout.profile_spinner_textview, coursetype);
spinner1.setAdapter(deptAdapter);
Log.d("co_id", String.valueOf(coursetypeid));
ArrayAdapter<String> centAdapter = new ArrayAdapter<>(Courses.this, R.layout.profile_spinner_textview, centertype);
spinner2.setAdapter(centAdapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
co_id=coursetypeid.get(i);
Log.d("co_id",co_id);
loaddata();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
cent_id=centerid.get(i);
loaddata();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
while opening activity same function calling multiple times. how to avoid this. i want call same function while selecting items from spinners. please help me
I'm going to POST value of spinners to the database
unfortunately, I do not know how to implement the POST method at any part of the CODE.
I'm having Problem with sending Snipper values.
public class FormActivity extends AppCompatActivity implements
....
spinnerStudy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String all_study = spinnerStudy.getItemAtPosition(spinnerStudy.getSelectedItemPosition()).toString();
if(!Objects.equals(all_study, "")){
Toast.makeText(getApplicationContext(),all_study, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String all_city = spinnerCity.getItemAtPosition(spinnerCity.getSelectedItemPosition()).toString();
if(!Objects.equals(all_city, "")) {
Toast.makeText(getApplicationContext(), all_city, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
s
pinnerUniversity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String all_university = spinnerUniversity.getItemAtPosition(spinnerUniversity.getSelectedItemPosition()).toString();
if(!Objects.equals(all_university, "")) {
Toast.makeText(getApplicationContext(), all_university, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
spinnerUnited.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String all_united = spinnerUnited.getItemAtPosition(spinnerUnited.getSelectedItemPosition()).toString();
if(!Objects.equals(all_united, "")) {
Toast.makeText(getApplicationContext(), all_united, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
button.setOnClickListener(this);
...
PLEASE Help ME...In this CASE, the post method HOW sends parameters?
How to get From Spinner a public String? Thank you ALL
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//position is spinner position
//mySpinner.getSelectedItem().toString(); IS value of position (string)
mMap.put("your Key",mySpinner.getSelectedItem().toString());
//the value add to map of volley
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
From MainActivity:
public class MainActivity extends ListActivity implements AdapterView.OnItemLongClickListener {
private DataSourceSql mDataSourceSql;
protected ArrayList<String> mProfileNames;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDataSourceSql = new DataSourceSql(MainActivity.this);
mNames = new ArrayList<String>();
//this part here
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setLongClickable(true);
}
The id of the ListView in this case is set in the XML as android:id="#android:id/list".
And then later on in the same activity:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(this, mNames.get(position), Toast.LENGTH_SHORT).show();
}
#Override
public boolean onItemLongClick(AdapterView<?> l, View v, final int position, long id) {
Toast.makeText(this, "long clicked: " + mNames.get(position), Toast.LENGTH_LONG).show();
return true;
}
But when I long-press an item in the list, the only thing that triggers is onListItemClick. I never get the message with the long click.
You shouldn't implement an interface like AdapterView.OnItemLongClickListener directly in your onCreate().. Just use your old way (Your class implement this interface) and with each method override, you have to write like this:
lv.setOnItemLongClickListener(this);
lv.setonListItemLongClickListener(this);
I think I figured it out, but someone please correct me if I'm missing something.
I removed the "implements..." thing and then added the following to the onCreate method:
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener
() {
#Override
public boolean onItemLongClick(AdapterView<?> av, View v, int
pos, long id) {
onListItemLongClick(v, pos, id);
return false;
}
});
Although I am not sure if I should be returning true or false there.
I also changed the long click function down below to
public boolean onListItemLongClick(View v, final int position, long id) {
Toast.makeText(this, "long clicked: " + mNames.get(position), Toast.LENGTH_LONG).show();
return true;
}
Edit:
Quicker approach is to just add lv.setOnItemLongClickListener(this); to my onCreate method in the OP.
Use This. Your Problem will Solve.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_SHORT).show();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Long Click", Toast.LENGTH_SHORT).show();
return true;
}
});
i have small issues in event handling , i have List view custom adapter data , each row having date , title , price . when i click on row i need to display details page but when i long press on price , date or title i need to sort the list view. i need to use gesture for on Long Press. please refer below code what i have tried.
Custom Adapter View
public EventAdapterView(Context context, List<EventUtil> eventList) {
this.mContext = context;
this.mEventUtil = eventList;
mLayoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new DrawableManager();
}
#SuppressLint("DefaultLocale")
#SuppressWarnings("deprecation")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
mView = convertView;
EventUtil eventUtil = mEventUtil.get(position);
mView = mLayoutInflater.inflate(R.layout.row_event_adapter, null);
TextView eventTitleView = (TextView) mView
.findViewById(R.id.list_view_event_title);
TextView eventDescView = (TextView) mView
.findViewById(R.id.list_view_event_location);
TextView eventDateView = (TextView) mView
.findViewById(R.id.list_view_event_price);
// final MyGestureDetector myGestureDetector= new MyGestureDetector();
// new ImageFeach().execute(mEventUtil.getEvent_Image_Url());
eventTitleView.setText(eventUtil.getEvent_Title());
// event title sorting
eventTitleView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
new MyGestureDetector() {
public void onLongPress(MotionEvent event1) {
//if (event1.getAction() == MotionEvent.ACTION_DOWN) {
Collections.sort(mEventUtil,
new Comparator<EventUtil>() {
#Override
public int compare(EventUtil obje1,
EventUtil obje2) {
return obje1
.getEvent_Title()
.compareTo(
obje2.getEvent_Title());
}
});
notifyDataSetChanged();
//}
};
}.onLongPress(event);
return true;
}
});
// event location sorting
eventDescView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
new MyGestureDetector() {
public void onLongPress(MotionEvent event1) {
//if (event1.getAction() == MotionEvent.ACTION_DOWN) {
Collections.sort(mEventUtil,
new Comparator<EventUtil>() {
#Override
public int compare(EventUtil event1,
EventUtil event2) {
return event1
.getEvent_location()
.compareTo(
event2.getEvent_location());
}
});
notifyDataSetChanged();
//}
};
}.onLongPress(event);
return true;
}
});
// event price sorting
eventDateView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
new MyGestureDetector() {
public void onLongPress(MotionEvent event1) {
// if (event1.getAction() == MotionEvent.ACTION_DOWN) {
Collections.sort(mEventUtil,
new Comparator<EventUtil>() {
#Override
public int compare(EventUtil event1,
EventUtil event2) {
return event1
.getEvent_Price()
.compareTo(
event2.getEvent_Price());
}
});
notifyDataSetChanged();
//}
};
}.onLongPress(event);
return true;
}
});
// event date sorting
ImageView dateImageView = (ImageView) mView
.findViewById(R.id.list_view_event_date);
dateImageView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
new MyGestureDetector() {
public void onLongPress(MotionEvent event1) {
//if (event1.getAction() == MotionEvent.ACTION_DOWN) {
Collections.sort(mEventUtil,
new Comparator<EventUtil>() {
#Override
public int compare(EventUtil event1,
EventUtil event2) {
return event2
.getEvent_Date()
.compareTo(
event1.getEvent_Date());
}
});
notifyDataSetChanged();
//}
};
}.onLongPress(event);
return true;
}
});
MyGestureDetector Class
public class MyGestureDetector extends SimpleOnGestureListener {
#Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
}
}
Help Me.. Thanks
I think, there's no need to use SimpleOnGestureListener.
In your Custom Adapter View:
First,
eventTitleView.setFocusable(false);
eventTitleView.setFocusableInTouchMode(false);
eventTitleView.setLongClickable(true);
eventDescView.setFocusable(false);
eventDescView.setFocusableInTouchMode(false);
eventDescView.setLongClickable(true);
eventDateView.setFocusable(false);
eventDateView.setFocusableInTouchMode(false);
eventDateView.setLongClickable(true);
Then,
just set setOnLongClickListener (View.OnLongClickListener l) on eventTitleView, eventDescView and eventDateView as,
eventTitleView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//do your sorting stuff here
}
});
eventDescView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//do your sorting stuff here
}
});
eventDateView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//do your sorting stuff here
}
});
Hope this helps you.
Why do you need to override onlongpress?
You know this function is available by default right?
just put the price , date or title on the same Layout and give each one OnLongPressListener with the action you want..:
EXAMPLE:
http://www.mikeplate.com/2010/01/21/show-a-context-menu-for-long-clicks-in-an-android-listview/
I have done this thro onLongClickListener. It's easy using this interface . You need to do
Class YourClass implements onLongClickListener{
public boolean onLongClick(View arg0){
// Event generated when user have long pressed the screen
return false; // If you do not want the event to keep occuring again and again
}
ListView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// YOUR SORTING CODE HERE
return false;
}
});
I have a ListFragment (for compatibility reasons I use android.support.v4.app.ListFragment) and want to use the contextual action mode.
Therefore I coded:
public class CustomListFragment extends ListFragment {
...
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setListAdapter(new CustomListAdapter(getActivity().getApplicationContext(), TYPE));
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
registerForContextMenu(getListView());
} else {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setMultiChoiceModeListener(new CustomMultiSpamItemChoiceModeListener());
}
}
...
}
class CustomMultiSpamItemChoiceModeListener implements MultiChoiceModeListener{
#Override
public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
return false;
}
#Override
public boolean onCreateActionMode(ActionMode arg0, Menu arg1) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode arg0) {
}
#Override
public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
return false;
}
#Override
public void onItemCheckedStateChanged(ActionMode arg0, int arg1,
long arg2, boolean arg3) {
}
}
public class CustomListAdapter extends BaseAdapter{
...
#Override
public View getView(int pos, View view, ViewGroup vg) {
...
}
}
The problem is that as soon as I set the setChoiceMode to ListView.CHOICE_MODE_MULTIPLE_MODAL the adapters method getView(int pos, View view, ViewGroup vg)
is called on every click (short pressed click) on an item of the ListView. The contextual action mode isn't even activated.
This become manifest in a long lasting item pressed animation.
Normally the blue indicator for an item pressed lasts a few ms but in my case its about a second because it takes some time to collect all needed information to create the ListItems.
Is this behavior as expected?
I don't think the ListView.CHOICE_MODE_MULTIPLE_MODAL flag needs to "reinitiate" the whole ListItems again.