百度网站推广怎么样,广点通投放平台登录,汽车之家官网首页,英文网站制作 官网本人初学Android#xff0c;今天研究到Adapter这块感觉挺有意思的#xff0c;写了个自定义列表进行测试 首先我们新建一个layout列表布局文件#xff0c;具体布局可以自己设定。 下面贴上我的自定义布局文件代码 1 ?xml version1.0 encodingutf-8今天研究到Adapter这块感觉挺有意思的写了个自定义列表进行测试 首先我们新建一个layout列表布局文件具体布局可以自己设定。 下面贴上我的自定义布局文件代码 1 ?xml version1.0 encodingutf-8?2 LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android3 android:orientationhorizontal4 android:layout_widthmatch_parent5 android:backgrounddrawable/list6 android:layout_heightwrap_content7 8 ImageView9 android:idid/ico
10 android:layout_width64dp
11 android:layout_height64dp
12 android:backgroundmipmap/ic_launcher/
13 LinearLayout
14 android:layout_widthmatch_parent
15 android:layout_heightwrap_content
16 android:orientationvertical
17 TextView
18 android:idid/biaoti
19 android:layout_widthmatch_parent
20 android:layout_heightwrap_content
21 android:text我是标题
22 android:layout_marginTop5dp
23 android:textSize22sp/
24 TextView
25 android:idid/content
26 android:layout_marginTop5dp
27 android:layout_widthmatch_parent
28 android:layout_heightwrap_content
29 android:textSize16sp
30 android:text我是项目信息/
31 /LinearLayout
32 /LinearLayout 上面代码的效果图如下整体用的是一个Image以及两个TextView 不好看就先凑合吧测试用 接下来我们开始MainActivity.java 1 package yuntu.com.yuntu;2 3 import android.support.v7.app.AppCompatActivity;4 import android.os.Bundle;5 import android.view.View;6 import android.widget.AdapterView;7 import android.widget.ListView;8 import android.widget.SimpleAdapter;9 import android.widget.TextView;
10 import android.widget.Toast;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15
16 public class MainActivity extends AppCompatActivity {
17 private ListView listView;
18 //声明标题
19 private String[] title new String[]{
20 我是第1个Title, 我是第2个Title, 我是第3个Title, 我是第4个Title
21 };
22 //声明内容
23 private String[] content new String[]{
24 我是第1个content, 我是第2个content, 我是第3个content, 我是第4个content
25 };
26 //声明图标
27 private int[] imgIds new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,R.mipmap.ic_launcher};
28 Override
29 protected void onCreate(Bundle savedInstanceState) {
30 super.onCreate(savedInstanceState);
31 setContentView(R.layout.activity_main);
32 listView (ListView) findViewById(R.id.list_item01);
33 ListMapString, Object listitem new ArrayListMapString, Object();
34 for (int i0;icontent.length;i){
35 MapString, Object map new HashMapString, Object();
36 map.put(ico,imgIds[i]);
37 map.put(title,title[i]);
38 map.put(content,content[i]);
39 listitem.add(map);
40 }
41 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});
42 listView.setAdapter(simpleAdapter);
43
44 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
45 Override
46 public void onItemClick(AdapterView? parent, View view, int position, long id) {
47 TextView bt (TextView) view.findViewById(R.id.biaoti);
48 TextView nr (TextView) view.findViewById(R.id.content);
49 Toast.makeText(MainActivity.this, bt.getText() | nr.getText(), Toast.LENGTH_SHORT).show();
50 }
51 });
52 }
53 } //本篇文章记录日常代码希望也可以帮到需要的人 ————鲨哒哒 转载于:https://www.cnblogs.com/shadada/p/6903637.html