Skip to content Skip to sidebar Skip to footer

Multiply Textviews In A Listview

There are 2 text views in each row, basically a Title and then a description underneath it. How do I set the text of each view within each row? @Override public View onCreateVi

Solution 1:

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

List<Map<String, String>> data = newArrayList<Map<String, String>>();

Map<String, String> dataMap = newHashMap<String, String>(2);
dataMap.put("Item1", item1Str);
dataMap.put("Item2", item2Str);
data.add(dataMap);

dataMap = newHashMap<String, String>(2);
dataMap.put("Item1", item1Str);
dataMap.put("Item2", item2Str);
data.add(dataMap);

SimpleAdapter adapter = newSimpleAdapter(getActivity().getBaseContext(), 
                                          data,
                                          android.R.layout.simple_list_item_2, 
                                          newString[] { "Item1", "Item2" }, 
                                          new int[] { android.R.id.text1, android.R.id.text2 });

listView.setAdapter(adapter);

Or you can make your own layout resource files and replace the android.R attributes with your custom made resources.

Solution 2:

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

Solution 3:

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:

@Overridepublic 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(newArrayAdapter(getActivity()) {
        @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null)
                convertView = getActivity().getLayoutInflater().inflate(R.layout.setting_twolinetext_checkbox, null, false);

            TextViewtvRowTitle= (TextView) convertView.findViewById(R.id.tvRowTitle);
            TextViewtvRowDesc= (TextView) convertView.findViewById(R.id.tvRowDesc);
            tvRowTitle.setText(mItems[position]);
            tvRowDesc.setText(mItemDescription[position]);

            return convertView;
        }

        @OverridepublicintgetCount() {
            return mItems.length;
        }
    });

    return mRoot;
}

Post a Comment for "Multiply Textviews In A Listview"