Use SimpleAdapter in Android-custom list, custom simpleadapter
I am a beginner in Android. Today I have found the Adapter interesting. I wrote a custom list for testing.
First, create a layout file. You can set the layout by yourself.
Paste my custom Layout file code below
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 = "I'm title" 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>
The code above is as follows, which uses an Image and two textviews as a whole.
Let's just try it out first. It can be used for testing.
Next we start 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 impor T java. util. map; 15 16 public class MainActivity extends AppCompatActivity {17 private ListView listView; 18 // declare title 19 private String [] Title = new String [] {20 "I am 1st titles", "I am 2nd titles ", "I have 3rd titles" and "I have 4th titles" 21 }; 22 // declared content 23 private String [] content = new String [] {24 "I am 1st content", "I am 2nd content ", "I am 3rd content", "I am 4th content" 25}; 26 // declare the icon 27 private int [] imgIds = new int [] {R. mipmap. ic_launc Her, 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 <Strin G, 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. setOnItemClickLis Tener (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}
// This article records the daily code, hoping to help those who need it
---- Shark