首页 > 代码库 > adapterView及子类
adapterView及子类
1、使用arrayadapter创建ListView
public class ArrayAdapterActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] str = {"1","2","3","4","5","6"};
//android.R.layout.simple_expandable_list_1 android提供布局文件作为列表组件 ArrayAdapter<String> adaper = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1, str); } }
2、使用simpleAdapter创建ListView
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:contentDescription="@string/app_name" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="20sp" /> </LinearLayout>
public class MainActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 对象中那些key生成对应value的列表项 SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.simple, new String[] { "title", "img" }, new int[] { R.id.title, R.id.img }); setListAdapter(adapter); } private List<Map<String, Object>> getData() { // map.put(参数名字,参数值) List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "摩托罗拉"); map.put("img", R.drawable.ic_launcher); list.add(map); map = new HashMap<String, Object>(); map.put("title", "诺基亚"); map.put("img", R.drawable.ic_launcher); list.add(map); map = new HashMap<String, Object>(); map.put("title", "三星"); map.put("img", R.drawable.ic_launcher); list.add(map); return list; } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。