首页 技术 正文
技术 2022年11月15日
0 收藏 787 点赞 2,358 浏览 2826 个字

本人初学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

Android中SimpleAdapter的使用—自定义列表

不好看就先凑合吧,测试用

接下来我们开始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();
}
});
}
}      //本篇文章记录日常代码,希望也可以帮到需要的人
                    ————鲨哒哒
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,895