任何人都可以在android中的arrayadapter中解释android.R.layout.simple_list_item_1和android.R.layout.simple_list_item_2.
我知道android.R.layout.simple_list_item_1和android.R.layout.simple_list_item_2是在android本身定义的布局.
在android.R.layout.simple_list_item_1中只包含一个textview但android.R.layout.simple_list_item_2包含两个文本视图.
我想举例说明android.R.layout.simple_list_item_2 ...如何在listview中用适配器显示两个文本视图.
我的代码是
package com.app.listview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ExampleListViewActivity extends Activity {
private String[] nameArr = new String[]{"Arun","Anil","Ankit","Manoj"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
nameArr);
listView.setAdapter(adapter);
}
}
Run Code Online (Sandbox Code Playgroud)
wsa*_*lle 14
不同之处如下.simple_list_item_1只包含一个TextView,而simple_list_item_2在子类中有两个RelativeLayout.这些都来自果冻豆.
simple_list_item_1管理
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:paddingRight="?android:attr/listPreferredItemPaddingRight"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>
Run Code Online (Sandbox Code Playgroud)
simple_list_item_2
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="twoLine"
>
<TextView android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="?android:attr/listPreferredItemPaddingLeft"
android:layout_marginTop="8dip"
android:textAppearance="?android:attr/textAppearanceListItem"
/>
<TextView android:id="@android:id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@android:id/text1"
android:layout_alignLeft="@android:id/text1"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</TwoLineListItem>
Run Code Online (Sandbox Code Playgroud)
根据ArrayAdapter的文档:
默认情况下,此类期望提供的资源ID引用单个TextView.
因此,默认情况下,a ArrayAdapter不会自动填充多个TextView实例.你可以,但是,覆盖getView()方法,并在两个填充TextView出现在s中R.layout.simple_list_item_2
win*_*ne2 14
我发现这是你问题的最简单答案:
ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_list_item_2, android.R.id.text1, list) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text1.setText(person[position].getName());
text2.setText(person[position].getAge());
return view;
}
};
Run Code Online (Sandbox Code Playgroud)
如果您没有注意到:诀窍是android.R.id.text1向ArrayAdapter 提供(主要是不必要的)参数,否则调用super将导致异常.
此外,此解决方案不需要Inflater或使用TwoLineListItem,这在API 17中已弃用.
| 归档时间: |
|
| 查看次数: |
51952 次 |
| 最近记录: |