Custom dropdown/popup with shorting ArrayList<Object> - java

i'll create a custom Dropdown/popup and it's work perfect.
See below Custom dropdown/popup code:
case R.id.linerSort:
PopupMenu popup = new PopupMenu(activity, linerSort);
//Inflating the Popup using xml file
popup.getMenuInflater()
.inflate(R.menu.popup, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(
activity,
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT
).show();
return true;
}
});
popup.show();
break;
Now i want to sorting a list which i select a item of Dropdown/popup.
Sorting is according to ArrayList<Object> So how to do that with my code.
if suppose i select a price then is shorting a acceding or descending order.
i follow this link: How to sort ArrayList<Object> in Ascending order android But how to i merge with my code i don't understand.

Try below code to sort in ascending or descending order:
case R.id.HighToLow:
Collections.sort(subCategoryViewDataBeanArrayList, new Comparator<SubCategoryViewDataBean>() {
#Override
public int compare(SubCategoryViewDataBean s1, SubCategoryViewDataBean s2) {
return Integer.parseInt(String.valueOf(s2.price)) - Integer.parseInt(String.valueOf(s1.price));
}
});
BindData(subCategoryViewDataBeanArrayList);
break;
case R.id.LowToHigh:
Collections.sort(subCategoryViewDataBeanArrayList, new Comparator<SubCategoryViewDataBean>() {
#Override
public int compare(SubCategoryViewDataBean s1, SubCategoryViewDataBean s2) {
return Integer.parseInt(String.valueOf(s1.price)) - Integer.parseInt(String.valueOf(s2.price));
}
});
BindData(subCategoryViewDataBeanArrayList);

Related

How to add a Switch to a PopupMenu programmatically ( Without using XML )?

I want to add a Switch to a PopupMenu as an item, I also don't want to use any XML codes, Is there is any way to do this?
here is an example of my code :
options.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View _view) {
PopupMenu myPopupMenu = new PopupMenu(getApplicationContext(), options);
myPopupMenu.getMenu().add("Option 1");
myPopupMenu.getMenu().add("Option 2");
myPopupMenu.getMenu().add("Option 3");
//Here I want to add a Switch
myPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch(item.getTitle().toString()) {
case "Option 1": {
onFirstOptionClicked();
break;
}
case "Option 2": {
onSecondOptionClicked();
break;
}
case "Option 3": {
onThirdOptionClicked();
break;
}
case "The Title of The Switch": {
//here i want to get a boolean value
//that detects if the Switch is checked or not
break;
}
}
return true;
}
});
myPopupMenu.show();
}});
I tried using the setCheckable method but it made a CheckBox item, what I want is a Switch item :
myPopupMenu.getMenu().add("The Title of The Switch").setCheckable(true);

I need to hide/show a particular item from popup menu on given condition

I tried this solution, but it doesn't work as I expected. Here is my code, and this is what i have tried.
PopupMenu popup = new PopupMenu(TableActivity.this, view);
popup.setOnMenuItemClickListener(TableActivity.this);
menu = popup.getMenu();
popup.inflate(R.menu.popup_shift);
popup.show();
popup.setOnMenuItemClickListener(this);
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_merge:
if(tableDbList.get(positionShift).getMergeTableId()== 0) {
//this is the condition to show/hide popup menuitem
popup.getMenu().findItem(R.id.menu_merge).setVisible(false);
}else {
checkPinCode.checkPinCodemethod(TableActivity.this, "mergeCancel");
}
}
return true;
}
You are trying to change visibility on MenuItem click . It will work but popupMenu will dismiss just after click . So it does not make any sense.
If your requirement is to show items on some condition you should set visibility before show(). Below is a simple example .
private void showPopup() {
final PopupMenu popup = new PopupMenu(MainActivity.this, view);
popup.getMenuInflater().inflate(R.menu.popup_shift, popup.getMenu());
if(someCondition){
popup.getMenu().findItem(R.id.menu_merge).setVisible(false);
}
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
}

Populating recyclerview - nothing happens

I have two recyclerviews, one to select a category, and one with various items belonging to various categories. When I click on a category in my first recyclerview, I would like to see only the items in that category displayed in the second recyclerview. In my displayDataItems method, the System.out.print gives me the correct filtered data. However, in my recyclerview, when I select a category, all items in all categories are still displayed.I'd be grateful for some pointers. Thanks in advance!
This is in my dataProvider class:
public static Predicate<DataItem> predAnimals = new Predicate<DataItem>() {
#Override
public boolean apply(DataItem dItem) {
return (dItem.getCategory() == "Animals");
}
};
public static void displayDataItems(List<DataItem> dataItemList, final Predicate<DataItem> pred) {
for (DataItem dItem : dataItemList) {
if (pred.apply(dItem)){
System.out.println(dItem);
}
}
}
And this is in my category recyclerAdapter class:
holder.myView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String itemName = item.getItemName();
Intent intent = new Intent (catContext, SelectionPage.class);
intent.putExtra(ITEM_KEY_TWO, itemName);
catContext.startActivity(intent);
switch (itemName){
case "Animals":
SampleDataProvider.displayDataItems(dataItemList, predAnimals);
Toast.makeText(catContext, "Animals", Toast.LENGTH_SHORT).show();
break;
case "Cartoons":
Toast.makeText(catContext, "Cartoons", Toast.LENGTH_SHORT).show();
break;
}
}
});
}

Getting the selected item's position in Android popup menu class

I want to store some data into every item in a popup menu. All of the items are inflated programatically in a for loop based on results returned from a feed.
In the following example, I use a HashMap storedOption to store each item's data with the loop indices as keys. But I need to find a way to get the position of the selected item in onMenuItemClick so that I can retrieve the data from storedOption. Can anyone tell me how to do that? Besides the following attempt, I have also tried item.getOrder() but it always returns 0 regardless of how many items it has in the menu.
public DynamicPopUpMenu{
private Map<String,FeatureList> storedOption = new HashMap();
public void main(final Context context,List<FeatureList> featureList){
int count = 0;
PopupMenu menu = new PopupMenu(context, featureList);
for(FeatureList f:featureList) {
MenuItem item = menu.getMenu().add(f.key);
storedOption.put(count, f);
count++;
}
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int position = info.position;
new ShowToast(context,Integer.toString(position)); // show position in a toast
return true;
}
});
menu.show();
}
}
You could use featureList.key as the key of your storeOption and them use item.getItemId(); to get the value from storeOption.
Like this:
public DynamicPopUpMenu{
private Map<String,FeatureList> storedOption = new HashMap();
public static void main(final Context context,List<FeatureList> featureList){
int count = 0;
PopupMenu menu = new PopupMenu(context, featureList);
for(FeatureList f:featureList) {
MenuItem item = menu.getMenu().add(f.key);
storedOption.put(f.key, f);
count++;
}
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int id = item.getItemId();
FeatureList mFeatureList = (FeatureList)storedOption(id)
new ShowToast(context,Integer.toString(value)); // show position in a toast
return true;
}
});
menu.show();
}
}

Less than or Equal Condition Android

I have one quote application which has a "Load more" button visible only if the quote list size is 15. Now I want change the condition so that it must show the button only if the quote list size is more than 15. My current code is like below and I have tried to change it to:
if(c.getCount()<=15){
// Not Showing Load More Button
}
but it's not showing my button.
My code for that button is below:
final Button btnLoadMore=new Button(this);
btnLoadMore.setBackgroundColor(Color.parseColor("#351802"));
btnLoadMore.setTextColor(Color.parseColor("#e8d8a7"));
btnLoadMore.setTypeface(btnLoadMore.getTypeface(), Typeface.BOLD);
btnLoadMore.setText("Load More Quotes");
if(c.getCount()<15){
// Not Showing Load More Button
}
else {
list.addFooterView(btnLoadMore);}
list.setAdapter(adapter);
anifadein=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slidedown);
list.startAnimation(anifadein);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
map = quotesList.get(position);
Intent intent = new Intent(QuotesActivity.this,
QuoteDialogActivity.class);
int itemPosition = position;
if(startingPoint>=30){
intent.putExtra("Pos", itemPosition+1);
intent.putExtra("LstCount", list.getCount()-1);
}else{
intent.putExtra("Pos", itemPosition+1);
intent.putExtra("LstCount", list.getCount());}
intent.putExtra("QuoteId", map.get(KEY_ID));
intent.putExtra("quotesType", quType);
intent.putExtra("startFrom", getIntent().getStringExtra("startFrom"));
intent.putExtra("Quotes", quotesList);
// Log.i("COUNT",""+(itemPosition+1)+"-"+list.getCount());
intent.putExtra("Fav", map.get(KEY_FAVORITE));
startActivity(intent);
if (mInterstitial.isLoaded()) {
mInterstitial.show();
}
}
});
btnLoadMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(btnLoadMore.getVisibility()==View.VISIBLE){
Cursor newC = null;
if (quType != 0) {
switch (quType) {
case 1:
newC = db.getQuotes(""+startingPoint);
break;
case 2:
newC = db.getFavoriteQuotes(""+startingPoint);
//page.setVisibility(View.GONE);
break;
case 3:
newC = db.getAuthorQuotes(getIntent().getStringExtra("AuthorId"),""+startingPoint);
// page.setVisibility(View.GONE);
break;
}
}
// Starting a new async task
if(newC.getCount()<15){
btnLoadMore.setVisibility(View.INVISIBLE);
}
startingPoint+=15;
do{
map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(KEY_ID, newC.getString(newC.getColumnIndex(KEY_ID)));
map.put(KEY_TEXT, newC.getString(newC.getColumnIndex(KEY_TEXT)));
map.put(KEY_AUTHOR, newC.getString(newC.getColumnIndex(KEY_AUTHOR)));
map.put(KEY_PICTURE, newC.getString(newC.getColumnIndex(KEY_PICTURE)));
map.put(KEY_PICTURE_SDCARD, String.valueOf(newC.getInt(newC
.getColumnIndex(KEY_PICTURE_SDCARD))));
map.put(KEY_WEB_ID,
String.valueOf(newC.getInt(newC.getColumnIndex(KEY_WEB_ID))));
//Log.i("web_id",String.valueOf(newC.getInt(newC.getColumnIndex(KEY_ID))));
map.put(KEY_FAVORITE, newC.getString(newC.getColumnIndex(KEY_FAVORITE)));
// adding HashList to ArrayList
quotesList.add(map);
if (mInterstitial.isLoaded()) {
mInterstitial.show();
}
} while (newC.moveToNext());
adapter.notifyDataSetChanged(); }}
});
}
}
I not sure from where Object "c" is coming but i m assuming it list object and getCount is return the current count of list. So, If you want to show "Show more" button when the size of list is greater than or equals to 15 then use this condition.
if(c.getCount() >= 15){
// Will show Load More Button
}
Put the Button in a layout XML File. Inflate it, then:
if (c.getCount() > 15) {
btnLoadMore.setVisibility(View.INVISIBLE);
}
If you want the Button to be invisible.

Categories