使用ListView
来源:互联网 发布:淘宝店铺公告怎么修改 编辑:程序博客网 时间:2024/06/05 20:04
需要用适配器。
ArrayAdapter 仅显示文本,有泛型。 常用"android.R.layout.simple_list_item_1",只有一个TextView。
SimpleAdapter 可以自定义显示,没有泛型。创建Map类型的集合。需要创建HashMap 和显示数据的布局文件(item.xml),map.put添加数据再用SimpleAdapter创建适配器。
布局:
<ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"/>
java:
ArrayAdapter
import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.ArrayAdapter;import android.widget.ListView;import java.util.ArrayList;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv = (ListView) findViewById(R.id.lv); ArrayList<String> list = new ArrayList<>(); for (int i = 0; i < 100; i++) { list.add(i + ""); } ArrayAdapter<String> adapter = new ArrayAdapter<>(this , android.R.layout.simple_list_item_1, list); lv.setAdapter(adapter); }}
SimpleAdapter
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.GridView;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Spinner;public class MainActivity extends Activity {ListView lv;String[] names = { "范冰冰", "李晨", "王宝强" };String[] contents = {"简介1","简介2","简介3"};int[] imgs = { R.mipmap.1, R.mipmap.2, R.mipmap.3 };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);lv = (ListView) findViewById(R.id.lv);List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();for (int i = 0; i < 3; i++) {HashMap<String, Object> map = new HashMap<String, Object>();map.put("img", ids[i]);map.put("name", names[i]);map.put("content", contents[i]);data.add(map);}SimpleAdapter adapter = new SimpleAdapter(this, data,R.layout.item_lv_star,new String[] { "img", "name", "content" }, new int[] {R.id.img, R.id.name, R.id.content });lv.setAdapter(adapter);}}
每一项的布局item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/img" android:layout_width="80dp" android:layout_height="100dp" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/img" android:orientation="vertical" android:padding="10dp" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="名称" /> <TextView android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="简介" /> </LinearLayout></RelativeLayout>
0 0
- ListView使用
- ListView 使用
- listview使用
- ListView 使用
- ListView使用
- 使用ListView
- ListView使用
- ListView使用
- listview 的使用
- ListView控件使用简介
- ListView控件使用
- ListView的使用
- ListView控件使用简介
- listView使用小结
- ListView的使用技巧
- Dephi中使用ListView
- 使用 ListView 控件
- delphi ListView 的使用
- parseInt()和parseFloat()
- C++ operator new以及new operator的各种套路浅析
- ASP.NET操作mysql数据库的实例(傻瓜都能看懂,已测试过)
- JAVA实现页面置换算法(OPT,FIFO,LRU)
- 1.1.1 centos 下安装 zeroice
- 使用ListView
- 快速获取Qt编译Release程序所依赖的qt库
- LD_LIBRARY_PATH环境变量设置及Linux动态库查找方法
- iOS10 UI教程视图的生命周期
- jQuery选择器大全
- C#仿sqlServer中的checksum
- Java程序更新文件自动迁移工具(迁移后自启动)
- Fragment详解(1)
- Windows 10「设置」应用完整MS-Settings快捷方式汇总