Adding different types of items a list view
- DDukesterman
- 2013-07-10 15:11
- 3
Is there a good tutorial or link that shows how to add different items to a listview? For example, one with two Text lines and a Check box, another that you just press and and something would pop up. All I have now is every list item is the same two line text view and checkbox... Or, if there is a way to add 1 row at a time with a different layout through R.layout.xxx
?
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mRoot = inflater.inflate(R.layout.frag_settings, container, false); mItems = getResources().getStringArray(R.array.setting_items); mItemDescription = getResources().getStringArray(R.array.setting_item_descriptions); mItemListView = (ListView) mRoot.findViewById(R.id.lvMainListView); ArrayAdapter<String> lvRowTitle = new ArrayAdapter<String>(getActivity(), R.layout.setting_twolinetext_checkbox, R.id.tvRowTitle, mItems); mItemListView.setAdapter(lvRowTitle); ArrayAdapter<String> lvRowDesc = new ArrayAdapter<String>(getActivity(), R.layout.setting_twolinetext_checkbox, R.id.tvRowDesc, mItemDescription); mItemListView.setAdapter(lvRowDesc); return mRoot;
3 Answers
You should create your own class extending BaseAdapter
. I recommend watching The World of ListView, it will help you understand everything you need to know about working with ListView.
android listview xml, The LogCat tells me I have to have a ListView whose id is android.R.id.lista. If I create a random ListView field in the XML file and give it the "lista" id, it still doesn't work. How to I call that ListView from XML to match something in my Java code? In other words, I know I'm wrong, but where and how do I fix it? xml file to include ListView content with the self explanatory attributes. 3, No need to change string.xml, Android studio takes care of default string constants. 4
Karakuri
2013-07-10 15:15
In addition to @LouMorda answer, I'd use some class, with fields that contains info about item and list item type:
public class Item { private int itemViewType; private Object tag; private String title; public Item(int itemViewType){ this.itemViewType = itemViewType; } public int getItemViewType() { return itemViewType; } public void setItemViewType(int itemViewType) { this.itemViewType = itemViewType; } ... }
So using this object gives more flexibility when adding items to the list in different sequences:
public class OptionsActivity extends ListActivity { private static final int LIST_ITEM_TYPE_1 = 0; private static final int LIST_ITEM_TYPE_2 = 1; private ArrayList<String> mItemsSource = new ArrayList<>(); ... @Override public int getItemViewType(int position) { return mItemsSource.get(position).getItemViewType(); } ... }
list adapter in android, Android Platform. API level 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1. Manifest.permission. Manifest.permission_group. android.accessibilityservice. AccessibilityService.MagnificationController.OnMagnificationChangedListener. AccessibilityService.SoftKeyboardController.OnShowModeChangedListener. submitList(list)); recyclerView.setAdapter(adapter); } } class UserAdapter extends ListAdapter<User, UserViewHolder> { public UserAdapter()
fs_dm
2018-04-27 09:40
In my example, the list activity that will display our custom list view is called OptionsActivity, because in my project this activity is going to display the different options my user can set to control my app. There are two list item types, one list item type just has a TextView and the second list item type just has a Button. You can put any widgets you like inside each list item type, but I kept this example simple.
The getItemView method checks to see which list items should be type 1 or type 2. According to my static ints I defined up top, the first 5 list items will be list item type 1, and the last 5 list items will be list item type 2. So if you compile and run this, you will have a ListView that has five items that just contain a button, and then five items that just contain a TextView.
Below is the activity code, the activity xml file, and an xml file for each list item type.
OptionsActivity.java:
activity_options.xml:
list_item_type_1.xml:
list_item_type2.xml:
Implementing a Heterogenous ListView, In my example, the list activity that will display our custom list view is called OptionsActivity, because in my project this activity is going to display the different​ In other words, different items in the list need to be represented differently. Another example would be Facebook with the many different types of feed items. Populate colors into the array aColors.add(new SimpleColor("Blue", ColorValues.
Lou Morda
2014-04-17 06:08