Android中SimpleAdapter的使用—自訂欄表,自訂simpleadapter
本人初學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 <LinearLayout14 android:layout_width="match_parent"15 android:layout_height="wrap_content"16 android:orientation="vertical">17 <TextView18 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 <TextView25 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 @Override29 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 @Override46 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 }
//本篇文章記錄日常代碼,希望也可以幫到需要的人
————鯊噠噠