2

每行有 2 个文本视图,基本上是一个标题,然后是它下面的描述。如何在每一行中设置每个视图的文本?

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

看来我的设置只能做一个或另一个文本视图。我确定有办法。提前谢谢!

4

3 回答 3

3

您可以使用内置的两个项目ListView。这是一个简单的例子:

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);

或者您可以制作自己的布局资源文件并用您自定义的资源替换 android.R 属性。

于 2013-07-09T19:17:38.127 回答
0

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;
}
于 2013-07-09T19:32:27.613 回答
0

为您的程序创建一个自定义适配器...带有一个视图,您将在其中添加一个标题视图和一个文本视图。

于 2013-07-09T17:44:27.107 回答