Android Development Series (19th): Set a style for SimpleAdapter,
Function of Adapter: After the data is processed in the adapter, it is displayed in the view.
Generally, for ArrayAdapter, you only need to pass an array and a style to the ArrayAdapter, and then you can display this string array in the view with a list.
For example, the following code:
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ls));
However, as shown in the contact list on our mobile phone, there are not only lists but also portraits and mobile phone numbers. Therefore, a simple ArrayAdapter cannot implement such a complex view.
We can use SimpleAdapter to implement this complex view, but we need to design the style.
Create an Android project, and define a ListView under the layout directory to store the list:
Main. xml:
<? 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: layout_height = "wrap_content"> <! -- Define a List --> <ListView android: id = "@ + id/mylist" android: layout_width = "fill_parent" android: layout_height = "wrap_content"/> </LinearLayout>
After defining this, we can write the java code:
SimpleAdapterTest. java:
Package org. crazyit. ui; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. app. activity; import android. graphics. color; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. adapterView. onItemSelectedListener; import android. widget. listView; Import android. widget. simpleAdapter; import android. widget. textView; import android. widget. toast; public class SimpleAdapterTest extends Activity {private String [] names = new String [] {"", "", "Li Qingzhao", "Li Bai "}; private String [] descs = new String [] {"Cute kids", "a girl who is good at music", "a female who is good at literature", "Romantic poet "}; // This is a collection of the IDs of the three images. private int [] imageIds = new int [] {R. drawable. tiger, R. drawable. nongyu, R. drawable. qingzhao, R. drawable. libai}; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // create a List set. The elements of the List set are MapList <Map <String, Object> listItems = new ArrayList <Map <String, Object> (); for (int I = 0; I <names. length; I ++) {Map <String, Object> listItem = new HashMap <String, Object> (); listItem. put ("header", imageIds [I]); listItem. put ("perso NName ", names [I]); listItem. put ("desc", descs [I]); listItems. add (listItem) ;}// create a SimpleAdapterSimpleAdapter simpleAdapter = new SimpleAdapter (this, listItems, R. layout. simple_item, new String [] {"personName", "header", "desc"}, new int [] {R. id. name, R. id. header, R. id. desc}); ListView list = (ListView) findViewById (R. id. mylist); // set Adapterlist for ListView. setAdapter (simpleAdapter); // It is the list item list of ListView. This method is triggered when the list. setOnItemClickListener (new OnItemClickListener () {// position item is clicked. @ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {Toast. makeText (getApplicationContext (), names [position] + "clicked", 1 ). show () ;}}); list. setOnItemSelectedListener (new OnItemSelectedListener () {// This method is triggered when the position item is selected. @ Overridepublic void onItemSelected (AdapterView <?> Parent, View view, int position, long id) {Toast. makeText (getApplicationContext (), names [position] + "selected", 1 ). show () ;}@ Overridepublic void onNothingSelected (AdapterView <?> Parent ){}});}}
In the above java code, when we create a SimpleAdapter object, we use a view: R. layout. simple_item: This is our custom style. You can use this style to implement a complex view.
Simple_item.xml:
<? 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: layout_height = "wrap_content"> <! -- Define an ImageView as part of the list item. --> <ImageView android: id = "@ + id/header" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: paddingLeft = "10dp"/> <LinearLayoutandroid: orientation = "vertical" android: layout_width = "match_parent" android: layout_height = "wrap_content"> <! -- Defines a TextView that is used as part of the list item. --> <TextView android: id = "@ + id/name" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textSize = "20dp" android: textColor = "# f0f" android: paddingLeft = "10dp"/> <! -- Defines a TextView that is used as part of the list item. --> <TextView android: id = "@ + id/desc" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textSize = "14dp" android: paddingLeft = "10dp"/> </LinearLayout>
By combining these components, the following effects can be formed:
In Android development, how does one rewrite SimpleAdapter to customize the font size?
The following is my implementation method. I hope it will help you.
Public class MySimpleAdapter extends SimpleAdapter {private LayoutInflater mInflater; private final float titleFontSize; private final float screenWidth; // screen width private final float screenHeight; // screen height public MySimpleAdapter (Context context Context, list <? Extends Map <String,?> Data, int resource, String [] from, int [] to) {super (context, data, resource, from, ); // obtain the screen length and width DisplayMetrics dm = new DisplayMetrics (); dm = context. getResources (). getDisplayMetrics (); screenWidth = dm. widthPixels; screenHeight = dm. heightPixels; // set the title Font size titleFontSize = adjustTextSize (); mInflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE);} @ Override p Ublic View getView (int position, View convertView, ViewGroup parent) {if (convertView = null) {convertView = mInflater. inflate (R. layout. judge_item, null);} TextView TV _title = (TextView) convertView. findViewById (R. id. itemTitle); TextView TV _content = (TextView) convertView. findViewById (R. id. itemContext); TV _title.setTextSize (titleFontSize); // set the font size, return super. getView (position, convertView, p Arent);} float adjustTextSize () {float textsize = 12; // implement your own font size algorithm here, textsize = screenWidth/320*12; return textsize ;}} can be displayed proportionally based on the height and width of the previously calculated screen. This is my algorithm and I hope it will help you. Replace SimpleAdapter with MySimpleAdapter ~
How does android configure listening for simpleadapter?
Generally, you do not set a listener for the adapter, such
Listview. setAdapter (adapter); // This adapter is your simpleadapter
Then set the listener for the list.
Listview. setOnItemClickListener (new OnItemClickListener ()
{
@ Override
Public void onItemClick (final AdapterView <?> Parent,
Final View view, final int position, final long id)
{
// Custom operation...
}
});