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

来源:http://www.cnblogs.com/shadada/archive/2017/05/25/6903637.html
-Advertisement-
Play Games

本人初學Android,今天研究到Adapter這塊感覺挺有意思的,寫了個自定義列表進行測試 首先我們新建一個layout列表佈局文件,具體佈局可以自己設定。 下麵貼上我的自定義佈局文件代碼 上面代碼的效果圖如下,整體用的是一個Image,以及兩個TextView 不好看就先湊合吧,測試用 接下來我 ...


本人初學Android,今天研究到Adapter這塊感覺挺有意思的,寫了個自定義列表進行測試

 

首先我們新建一個layout列表佈局文件,具體佈局可以自己設定。

下麵貼上我的自定義佈局文件代碼

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="horizontal"
 4     android:layout_width="match_parent"
 5     android:background="@drawable/list"
 6     android:layout_height="wrap_content">
 7 
 8     <ImageView
 9         android:id="@+id/ico"
10         android:layout_width="64dp"
11         android:layout_height="64dp"
12         android:background="@mipmap/ic_launcher"/>
13     <LinearLayout
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:orientation="vertical">
17         <TextView
18             android:id="@+id/biaoti"
19             android:layout_width="match_parent"
20             android:layout_height="wrap_content"
21             android:text="我是標題"
22             android:layout_marginTop="5dp"
23             android:textSize="22sp"/>
24         <TextView
25             android:id="@+id/content"
26             android:layout_marginTop="5dp"
27             android:layout_width="match_parent"
28             android:layout_height="wrap_content"
29             android:textSize="16sp"
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         List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
34         for (int i=0;i<content.length;i++){
35             Map<String, Object> map = new HashMap<String, 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 }      



//本篇文章記錄日常代碼,希望也可以幫到需要的人
                    ————鯊噠噠

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在也個html頁面中加入bootstrap是很方便,就是一般的將css和js文件通過Link和Script標簽就行。 那麼在一個用vue-vli生成的前端項目中如何加入?因為框架不一樣了,略微要適應一下。 腳手架生成項目 執行命令用webpack模板生成一個名為vuestrap的項目(名字任意) 在 ...
  • 六邊形 今天看到別人分享的六邊形學習 自己也試著做了一下 原理: 圖片旋轉 transfrom:rotate(120deg); 溢出隱藏 overflow:hidden; 我們要用三層div來實現旋轉效果 第一次 rotate(120deg) 第二次 rotate(-60deg) 第三次 rotat ...
  • ...
  • 作者寫的很有意思拿出來和大家分享 作者:Shadow鏈接:https://www.zhihu.com/question/19553117/answer/114306637來源:知乎著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。 你是否好奇標識瀏覽器身份的User-Agent,為 ...
  • 如下圖點擊Assers.xcassets,點擊下方的“+”,即可通過下麵方法增加launchimage 點擊launchimage,右邊出現方格 將圖片拖入方格中,Main Interface清空, Launch Images Source選擇Launchimage Launch Screen Fi ...
  • SoftReference可以用於bitmap緩存 WeakReference 可以用於handler ...
  • 支付寶插件 2016年11月的時候支付寶進行了一次更新,支付寶的SDK升級到2.0版本。以前在app中使用支付寶進行支付叫做移動支付,11月之後更名為APP支付。 1 根據支付寶的說明文檔的建議,為保證安全,簽名都放到後端去做,前端只需要接收後臺傳入簽名字元串,使用該插件調用支付寶SDK完成支付 2 ...
  • 最近想倒騰一個小小的 UIKit 到 JCenter,為開源社區貢獻一點綿薄之力,於是就有了一系列慘無人道的踩坑史。好,接下來,直奔主題,以下是發佈流程。 發佈到 JCenter 發佈到 JCenter 需要以下幾個步驟。 註冊 Bintray 賬號並創建倉庫 Bintray 是 JCenter 庫 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...