I have a List<Restaurant> restaurants (11 items). I want to get only 6 items from 11 items to show in gridview. Now, I loop all size(). How to loop to get only 6 items.
if (restaurants != null && restaurants.size() > 0) {
for (int i = 1; i < restaurants.size(); i++) {
adapter = new ItemAdapter(MainActivity.this, restaurants);
grid_view.setAdapter(adapter);
}
}
Best way,
#Override
public int getItemCount() {
if(lotteryList.size()>6){
return 6;
}
else{
return lotteryList.size();
}
}
Try this code.
List<Restaurant> restaurantsTemp=new ArrayList();
for (int i = 0; i < 6; i++) {
restaurantsTemp.add(restaurants.get(i));
}
adapter = new ItemAdapter(MainActivity.this, restaurantsTemp);
grid_view.setAdapter(adapter);
no need loop if your list already have 11 records.Pass third parameter in adapter constructer and set it into count return as 6 in the adapter.
adapter = new ItemAdapter(MainActivity.this, restaurants,6);
grid_view.setAdapter(adapter);
Hi you need to use this code, Also you don't need to set adapter in loop.
if (restaurants != null && restaurants.size() > 0) { // restaurants.size() (11 items)
List<Restaurant> modifiedList=new ArrayList();
for (int i = 1; i < restaurants.size(); i++) { // 11 items
modifiedList.add(restaurants.get(i-1));
if(modifiedList.size()==6){
break;
}
}
adapter = new ItemAdapter(MainActivity.this, modifiedList);
grid_view.setAdapter(adapter);
}
ArrayList<String> sixItem = new ArrayList<>;
if (restaurants.size() >= 6) { // restaurants.size() (11 items)
for (int i = 1; i < 7; i++) { // 11 items
sixItem.add(i-1);
}
} else {
for (int i = 1; i < restaurants.size(); i++) {
sixItem.add(i-1);
}
}
adapter = new ItemAdapter(MainActivity.this, sixItem);
grid_view.setAdapter(adapter);
if restaurants.size() is less than 6 it won't show error.
try this in your ItemAdapter getCount() or getItemCount()
#Override
public int getItemCount() {
if(lotteryList.size()>6){
return 6;
}
else{
return lotteryList.size();
}
}
Related
I am not getting the result true even when the first element of both arraylists are same.i want to check my checkbox when result matches.I have implemented it in my recyclerview I want to compare the elements of the arraylists but both are different in size.I found a solution online and implemented it like:.
List<String> list1 = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
list1.add(data.get(i).getChannel_names());
}
List<String> list2 = new ArrayList<>();
for (int i = 0; i < listNewsChannelsSelected.size(); i++) {
list2.add(listNewsChannelsSelected.get(i).getSelectedChannelsFromApi());
}
private boolean equalLists(List<String> one, List<String> two) {
if (one == null && two == null) {
return false;
}
if (one != null && two == null) {
return false;
}
one = new ArrayList<>(one);
two = new ArrayList<>(two);
Collections.sort(one);
Collections.sort(two);
return one.equals(two);
}
if (equalLists(list1,list2)) {
holder.mCheckBox.setChecked(true);
} else {
holder.mCheckBox.setChecked(false);
}
I just tested it and saw that there is nothing wrong in the code. (Bit modified to test in java). I am getting the result as TRUE so the code is correct.
Try to check your API data. It seems to be incorrect (not what you are expecting it to be). Try to generate logs (print api data in loop).
public class Main {
public static void main(String args[]){
List<String> list1 = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list1.add(""+i);
}
List<String> list2 = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list2.add(""+i);
}
if (equalLists(list1,list2)) {
System.out.println("TRUE");
} else {
System.out.println("FALSE");
}
}
private static boolean equalLists(List<String> one, List<String> two) {
if (one == null && two == null) {
return false;
}
if (one != null && two == null) {
return false;
}
one = new ArrayList<>(one);
two = new ArrayList<>(two);
Collections.sort(one);
Collections.sort(two);
return one.equals(two);
}
how does one iterate through a view's children and remove all ImageViews with a Tag that StartsWith a particular string?
All examples I can find this iterator;
for (int pos = 0; pos < mat.getChildCount(); pos++)
{
Object tag = mat.getChildAt(pos).getTag();
if (tag != null)
{
String s = tag.toString();
if (s.startsWith("Z"))
{
mat.removeView(mat.getChildAt(pos));
}
}
}
do a test, then remove the object.
The issue is both 'pos' and getChildCount change throughout the process. If I want to remove the first item and then the 2nd item (which after the first remove is actually the first item) it won't work, as pos is now 1 (ie 2nd item).
thanks
There are a few options.
for (int pos = 0; pos < mat.getChildCount();) {
if (remove) {
mat.removeViewAt(pos);
continue;
}
// only increment if the element wasn't removed
pos++;
}
for (int pos = 0; pos < mat.getChildCount(); pos++) {
if (remove) {
mat.removeViewAt(pos);
// balance out the next increment
pos--;
}
}
// don't remove views until after iteration
List<View> removeViews = new ArrayList<>();
for (int pos = 0; pos < mat.getChildCount(); pos++) {
if (remove) {
removeViews.add(mat.getChildAt(pos));
}
}
for (View view : removeViews) {
mat.removeView(view);
}
// count in reverse
for (int pos = mat.getChildCount() - 1; pos >= 0; pos--) {
if (remove) {
mat.removeViewAt(pos);
}
}
I am working on a methods in which when I use a single loop with condition it checks both if and else conditions. But when I add more than one loop with condition it only checks else condition I do not know where is problem. please help me for that
here is my code
public void compareArray() {
ArrayList<Boolean> subset = new ArrayList<>();
for (int j = 0; j < (arrayBeef1.length); j++) {
for (int i = 0; i < Items.size(); i++) {
if ((arrayBeef1[j].equals(Items.get(i)))) {
subset.add(true);
break;
}
}
}
if (subset != null) {
if (arrayBeef1.length == subset.size()) {
Intent nextClass = new Intent();
nextClass.setClass(AddItem.this, BeefRecipes.class);
startActivity(nextClass);
finish();
} else {
Toast.makeText(getApplicationContext(), "Un-Matched", Toast.LENGTH_LONG).show();
}
}
for(int j = 0 ; j < (arrayBeef2.length) ; j++) {
for (int i = 0; i < Items.size(); i++) {
if ((arrayBeef2[j].equals(Items.get(i)))) {
subset.add(true);
break;
}
}
}
if (subset != null) {
if (arrayBeef2.length == subset.size()) {
Intent nextClass = new Intent();
nextClass.setClass(AddItem.this, BeefRecipes.class);
startActivity(nextClass);
finish();
} else {
Toast.makeText(getApplicationContext(), "Un-Matched", Toast.LENGTH_LONG).show();
}
}
where Item array is dynamic array which is populated through spinner and beefArray1 and two are
String[] arrayBeef1 = { "carrot","ginger",
"onion","beef","unsalted butter"};
String[] arrayBeef2 = { "garlic","olive oil", "onion",
"Dijon mustard","beef","salt","carrot","rosemary"};
and Items arraList is produced with similar items
please help what should I do with this method
What i am doing is showing a list view and reload its data when list is Moved to last Element.
Its working fine but the problem is when it reloads it went back to the top element in list. I want List to be stayed at same position and append new Elements below it.
Here is what i am doing
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (firstVisibleItem + visibleItemCount == totalItemCount
&& totalItemCount != 0) {
if (flag_loading == false) {
flag_loading = true;
toast("Last view");
loadMorePost();
}
}
}
private void loadMorePost() {
page_no = page_no + 1;
String url = "api/post/index/page_no/" + page_no;
syncManager.sendToServer(url, null, this);
}
if (action.equals("index") && controller.equals("post")) {
if (status) {
//postItemsList.clear();
JSONArray postArray = jsonObject.getJSONArray("post_list");
if (postArray.length() <= 0) {
toast("No Post Available");
} else {
for (int i = 0; i < postArray.length(); i++) {
JSONObject obj = postArray.getJSONObject(i);
ItemsAllPost data = new ItemsAllPost();
data.user_first_name = obj
.getString("User_first_name");
data.user_address = obj.getString("User_address");
data.user_post_count = obj
.getString("user_post_count");
data.description = obj
.getString("post_description");
data.category_id = obj.getString("category_id");
data.create_user_id = obj
.getString("create_user_id");
data.post_image = obj.getString("post_image");
data.user_ph_no = obj.getString("User_ph_no");
data.user_long = obj.getString("User_long");
data.date = obj.getString("date");
data.post_fav = obj.getString("post_fav");
data.amount = obj.getString("amount");
data.id = obj.getString("id");
data.category_title = obj
.getString("category_title");
data.time_period = obj.getString("time_period");
data.is_private = obj.getString("is_private");
data.fav_count = obj.getString("fav_count");
JSONArray imagesarry = obj
.getJSONArray("post_image");
data.imgList = new String[imagesarry.length()];
for (int j = 0; j < imagesarry.length(); j++) {
data.imgList[j] = imagesarry.getString(j);
}
data.view_count = obj.getString("view_count");
data.user_email = obj.getString("User_email");
data.user_lat = obj.getString("User_lat");
data.title = obj.getString("post_title");
postItemsList.add(data);
}
inflateList(postItemsList);
}
}
}
private void inflateList(ArrayList<ItemsAllPost> postItemsList) {
if (postItemsList.size() > 0) {
adapter = new AllPostAdapter((BaseActivity) activity,
postItemsList, userDataFrag);
allpostsLV.setAdapter(adapter);
adapter.notifyDataSetChanged();
} else {
// do nothing
}
}
Any Help
What should i do
Thanks in Advance
using one List to store your data :
private List<ItemsAllPost> datas;
init your adapter only one when the first time you get data :
adapter = new AllPostAdapter((BaseActivity) activity,
postItemsList, datas);
when you load more data finish just add all extra data to : datas
datas.addAll(moredataList);
and only need to call :
adapter.notifyDataSetChanged();
I cant see where postItemsList is declared so I guess you made it like the : datas variable so you can only call : adapter.notifyDataSetChanged(); but don't re-init your adapter.
try this
add this code where you get your updated data and also adapter : adapter.notifyDataSetChanged();
remove these lines :
private void inflateList(ArrayList<ItemsAllPost> postItemsList) {
if (postItemsList.size() > 0) {
adapter = new AllPostAdapter((BaseActivity) activity,
postItemsList, userDataFrag);
allpostsLV.setAdapter(adapter);
adapter.notifyDataSetChanged();
} else {
// do nothing
}
}
and add adapter.notifyDataSetChanged(); after
postItemsList.add(data);
I have a multiselection ListView in my android app. What I need to know is how many items on that list I have selected.
Use getCheckedItemCount() of ListView. This gives you directly an int.
I found a solution.
getCheckedItemPositions() will create a SparceBooleanArray. So it is just to count the elements which are true in that array.
Example:
SparseBooleanArray positions = theListView.getCheckedItemPositions();
int counter = 0;
if (positions != null) {
int length = positions.size();
for (int i = 0; i < length; i++) {
if (positions.get(positions.keyAt(i))) {
counter++;
}
}
}
Instead of using keyAt followed by get, we can use valueAt directly.
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int count = 0;
for (int i = 0, ei = checkedItemPositions.size(); i < ei; i++) {
if (checkedItemPositions.valueAt(i)) {
count++;
}
}