Multiply List Text Views

Each line has 2 textual views, mostly a name, and then a description below it. How to set the text of each view in each line?

@Override 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; } 

It seems I can only make one text view or the other with my customization. I am sure there is a way. Thanks in advance!

+2
source share
3 answers

You can use the built-in two ListView elements. Here is a quick example:

 List<Map<String, String>> data = new ArrayList<Map<String, String>>(); Map<String, String> dataMap = new HashMap<String, String>(2); dataMap.put("Item1", item1Str); dataMap.put("Item2", item2Str); data.add(dataMap); dataMap = new HashMap<String, String>(2); dataMap.put("Item1", item1Str); dataMap.put("Item2", item2Str); data.add(dataMap); SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), data, android.R.layout.simple_list_item_2, new String[] { "Item1", "Item2" }, new int[] { android.R.id.text1, android.R.id.text2 }); listView.setAdapter(adapter); 

Or you can create your own layout resource files and replace the android.R attributes with your resources you created.

+3
source

Create a custom adapter for your program ... with a view in which you add a title view and a text view below it.

0
source

Since it looks like you already have a layout defined for your list items (R.layout.setting_twolinetext_checkbox) , so you can use a custom adapter like this one:

 @Override 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); mItemListView.setAdapter(new ArrayAdapter(getActivity()) { @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) convertView = getActivity().getLayoutInflater().inflate(R.layout.setting_twolinetext_checkbox, null, false); TextView tvRowTitle = (TextView) convertView.findViewById(R.id.tvRowTitle); TextView tvRowDesc = (TextView) convertView.findViewById(R.id.tvRowDesc); tvRowTitle.setText(mItems[position]); tvRowDesc.setText(mItemDescription[position]); return convertView; } @Override public int getCount() { return mItems.length; } }); return mRoot; } 
0
source

Source: https://habr.com/ru/post/1490742/


All Articles