I have multiple arraylist in my Android studio project (Java) like :
firstArray = [1,2,3]
secondtArray = [A,B,C]
thirdArrayt = [X,Y,Z]
Now I want to populate my recyclerview with the items from these arraylist.Each time i want to show the spicific index from all the lists.
For example, in my first view i want to show firstArray[0],secondArray[0] and thirdArray[0] in my viewholder.
How do I get that?
Related
I have a list and its size is x numbers. I want to show 6 items on the screen even user scrolled to view the list , it should show 6 items on the screen.
I tried this Answer but my item's height was cropped and it didn't work for me.
Edit I want to retrieve all list as usual. I just want to make screen carry 6 items .
// get the reference of RecyclerView
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
// set a GridLayoutManager with 6 number of columns, Vertical gravity
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),6,LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(gridLayoutManager); // set LayoutManager to RecyclerView
In lack of code I only can assume you like to get a list of X number from Api or database, etc and you only want to show max of 6 items
it would have been easier to see some code of what you have done so-far so the correction/suggestion will suite you better, in lack of code I give you few suggestions:
your view should not know anything on how to handle data , it should simply show the data , in you case it should receive a 6 items and show them in the view (recyclerView in your case).
I assume you are not using any pattern (like MVVM or MVP) and you just obtaining your list from (api or database) inside your Activity - (extra tip -> it is always better to use sort of Architecture design to separate your logic into a Presenter / viewModel layer for easy unit test purposes later)
lets assume you define your Adapter in Activity and passing your list like:
In your activity
var listOfAllYourItems = arrayListOf(object1,object2,...,object100)
val myAdapter = MyAdapter()
myAdapter.setItems(listOfAllYourItems.sublist(1,6)) //this will only pass the first 6 items
the rest of normal stuff ...
so this is like you have only 6 items and displaying it in recyclerView - simple (view does not hold any logic on how to handle items over 6 etc, view just know how to display item and should not care on how to handle anything else)
Extra point :
you can add a next / previous button in your view (perhaps below you RecyclerView in you activity xml), and then you add some logic to them to show next 6 items from list onNextTapped or previous 6 items when tapped previous button like :
In your activity
fun noNextTapped(){
myAdapter.setItems(listOfYour6Items.sublist(6,12))
//of course you need to add your logic to handle edge cases like what if the list size does not have enough items (like if it is only have 10 items) etc , and some other logic on how to know which page you are displaying so you can pass the correct number of items to the adapter onNextTapped fun (each page shows 6 items) I leave those to you
}
in your MyAdapter :
class MyAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder> {
var list List<Objects> ;
fun setItems(new6Items : List<Objects>){
list.clear()
list.addAll(new6Items)
notifyDataSetChanged()
}
...
}
I am Having a trouble. In my project i am having a spinner . Each value i will select in spinner it will make an api call which in return is receiving various objects array.
Important : Each value in spinner will get data from api consisting of different array length.
What i want is to display that each value of array in a spinner .The thing is i am not finding any way to create only that amount of spinner which are equal to data in array.
Example :Spinner value is 1 and array we get from api call has 3 values. Then if spinner value is 2, array we get has 5 values. I have to create 3 spinners if spinner value is 1 and 5 spinners on layout if spinner value is 2 automatically.
Note : Array values can go upto 15 .
Use a RecyclerView for dynamic content. When you get different array from api change you Adapter data source and notify for Adapter for data change .
Check the link i provided, there is a Adapter class called MyAdapter. You just have to update mDataset String Array and need to call notifyDataSetChanged. Adapter will automatically update its resource.
I would suggest you just create one textview and use StringBuilder.append to append all values from the array with proper styling (meaning commas etc)
How to make my ListView displays the list of subject for the firstSemester multiple time in the ListView in different row
I want to make it looks like this
You can do that by adding it multiple times to your ArrayList<> listItems Or you can create two listItems and a custom adapter where you can add logic to repeat the one after another.
I spent some time to google it, but didn't help.
I have a listview that contains an ArrayList.
Can I collect all data in array, show it in listView and delete array, but not update listView - so data will be on screen?
Yes until you scroll the list view to next visible item (or call notify item set change), then ListView ask adapter to provide object for example at 5th position and you will get ArrayIndexOutOfBoundsException or NullPointerException. So if you want to avoid that you have to copy your list elements to another ArrayList instance and pass it to your adapter.
arrayListMain.get(position).remove(arrayListChild.get(position));
notifydatasetchange();
arrayListMain is which arraylist your are use at setAdapter and arraylistChild is arrayList in your adapter
In my program I have a spinner that show three Strings taken from an Arraylist<String>. I use this code:
pinnerArray = new ArrayList<String>();
spinnerArray.add("IT");
spinnerArray.add("EN");
spinnerArray.add("PR");
ArrayAdapter lenguageAdapter=new ArrayAdapter(convertView.getContext(), R.layout.lenguage_item_layout, spinnerArray);
holder.spinnerLenguage.setAdapter(lenguageAdapter);
My spinner initially shows IT and, when I click on it, opens the list of items. In this list there are all 3 items (IT, EN, PR) but I want show the selected item (in this case IT) only one time and not two times.
How can I correct my code?