每行有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;
}
我的设置似乎只能做一个文本视图或另一个文本视图。我确定有办法。提前谢谢!
答案 0 :(得分: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属性替换为您自定义的资源。
答案 1 :(得分:0)
为您的程序创建一个自定义适配器...使用一个视图,您可以在其中添加标题视图和文本视图。
答案 2 :(得分:0)
因为看起来您已经为列表项(R.layout.setting_twolinetext_checkbox)
定义了布局,所以您可以使用像这样的自定义适配器:
@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;
}