Android中SimpleAdapter的使用—自定义列表
2024-10-05 06:54:28
本人初学Android,今天研究到Adapter这块感觉挺有意思的,写了个自定义列表进行测试
首先我们新建一个layout列表布局文件,具体布局可以自己设定。
下面贴上我的自定义布局文件代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="@drawable/list"
android:layout_height="wrap_content"> <ImageView
android:id="@+id/ico"
android:layout_width="64dp"
android:layout_height="64dp"
android:background="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/biaoti"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是标题"
android:layout_marginTop="5dp"
android:textSize="22sp"/>
<TextView
android:id="@+id/content"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="我是项目信息"/>
</LinearLayout>
</LinearLayout>
上面代码的效果图如下,整体用的是一个Image,以及两个TextView
不好看就先凑合吧,测试用
接下来我们开始MainActivity.java
package yuntu.com.yuntu; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class MainActivity extends AppCompatActivity {
private ListView listView;
//声明标题
private String[] title = new String[]{
"我是第1个Title", "我是第2个Title", "我是第3个Title", "我是第4个Title"
};
//声明内容
private String[] content = new String[]{
"我是第1个content", "我是第2个content", "我是第3个content", "我是第4个content"
};
//声明图标
private int[] imgIds = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,R.mipmap.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_item01);
List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
for (int i=0;i<content.length;i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("ico",imgIds[i]);
map.put("title",title[i]);
map.put("content",content[i]);
listitem.add(map);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this,listitem,R.layout.main_list,new String[]{"title","content","ico"},new int[]{R.id.biaoti,R.id.content,R.id.ico});
listView.setAdapter(simpleAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView bt = (TextView) view.findViewById(R.id.biaoti);
TextView nr = (TextView) view.findViewById(R.id.content);
Toast.makeText(MainActivity.this, bt.getText() + "|" + nr.getText(), Toast.LENGTH_SHORT).show();
}
});
}
} //本篇文章记录日常代码,希望也可以帮到需要的人
————鲨哒哒
最新文章
- UIkit框架之UIPickerView
- 根据用户输入的二代身份证号码自动为其计算生成出生日期的javascript方法
- linux消息队列操作
- 【开源专访】Sea.js创始人玉伯的前端开发之路
- c#xml序列化对象,xml标记都缩写了
- javascript. String方法扩张.
- #include <;process.h>;
- JVM-类的四种载入方式
- PHP 反射类学习记录
- [Linux容器]当我们谈容器的时候,我们在谈什么
- Haproxy+Keepalived高可用环境部署梳理(主主和主从模式)
- JavaScript----特效代码
- CS229 6.7 Neurons Networks whitening
- ★itext-为pdf文件添加页眉页脚 | 3步完成 |
- Spring MVC @RequestParam
- React中的高阶组件,无状态组件,PureComponent
- UVA 580 Critical Mass
- Harbor实现容器镜像仓库的管理和运维
- 使用shell统计字符串出现的次数,并从大到小进行排序显示
- jQuery实现按钮5秒后可以点击