Android Development notes (12) -- ListView & Adapter, androidlistview
Reprinted Please note: http://www.cnblogs.com/igoslly/p/6947225.html
The next chapter is aboutListFragmentFirst, we will introduceListViewRelated configurations, understandingListFragmentIt is also relatively easy.
In the fznpcy column: http://blog.csdn.net/fznpcy/article/details/8658155/ has a more detailed explanation and examples.
ListView & Adapter
I. Adapter
Adapter is an Adapter interface connecting back-end data and front-end display. It is an important link between data and UI (View.
Adapter is required in common View (List View, Grid View) and other places.
For example, the relationship among Data, Adapter, and View is intuitively expressed:
Adapter has multiple forms, but here we only introduce the commonly used ArrayAdapter, SimpleAdapter, and BaseAdapter
Ii. ArrayAdapter
Each Adapter must customize its own item layout file. ArrayAdapter only contains text. Generally, android. R. layout. simple_list_item_1 is used (the layout file defined by the system only displays one line of text)
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceListItemSmall" android:gravity="center_vertical" android:paddingLeft="16dp" android:paddingRight="16dp" android:textColor="#fff" android:background="?android:attr/activatedBackgroundIndicator" android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
Create ArrayAdapter
String[] strs = {"1","2","3","4","5"};ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.xxx,strs);
Of course, you can also define the ArrayList directly in Android Studio-strings. xml.
<string-array name="list_item"> <item>Practice Record</item> <item>Competition Record</item> <item>Previous History</item> <item>Game Rules</item> <item>How to use this app?</item> <item>User Information</item> </string-array>
You can set the menu list in the Drawer Layout of the App.
Iii. SimpleAdapter
Compared with ArrayAdapter, SimpleAdapter can implement more complex structures, such as ImageView, Button, and CheckBox.
The ListActivity can be directly inherited from the ListView, which is not much different from the ordinary Activity. The difference is that the display ListView is optimized and displayed in a few aspects.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_weight="8" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="Null" android:textSize="20sp" android:padding="2dp" android:textColor="@color/black" android:id="@+id/list_competition_player"/> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="Null" android:textSize="16sp" android:padding="2dp" android:id="@+id/list_competition_date"/> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_weight="3" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="100" android:gravity="center" android:textSize="24sp" android:textColor="@color/black" android:id="@+id/list_competition_scoreA"/> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="—" android:gravity="center" android:textSize="28sp"/> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_weight="3" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="100" android:gravity="center" android:textSize="24sp" android:textColor="@color/black" android:id="@+id/list_competition_scoreB"/> </LinearLayout></LinearLayout>
The receiving parameters include:
This, layout file (alist. XML), Hash Map, layout file Control Id. Each key-value data of Hash Map maps to the components corresponding to IDs in the layout file.
Create an ArrayList. Each element is a separate Hash Map.Add string/image name.
map.put("key_word1","value1");
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "G1"); map.put("info", "google 1"); map.put("img", R.drawable.i1); list.add(map);
Create SimpleAdapter
SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.vlist, new String[]{"title","info","img"}, new int[]{R.id.title,R.id.info,R.id.img});
Actual
The
4. BaseAdapter
Add a Button to the list and modify the Adapter in real time. BaseAdapter is required.
SimpleAdapter also inherits from BaseAdapter.
BaseAdapter is an abstract class. You must customize an Adapter to inherit it., You can also use ViewHolder for display.
/* WhenListWhen a large amount of data needs to be loaded, it will occupy a large amount of memory, which affects performance. At this time, it needs to be filled on demand and reused.ViewTo reduce object Creation
* The fastest way is to defineViewholder, SetConvexOfTagSetViewholder. Use it again if it is not null.
*FindViewByIdIs ParsingLayout. xmlLayout the childView, ParsingXmlIs a force, so I proposedViewHolderConcept
* Use a static class to saveXmlSub-ViewSo that it is not necessary to parse the reference relationship every time.Xml.
*/
When inheriting the BaseAdapter definition, you must overload four functions: getCount (), getItem (int position), getItemId (int position), getView (int position, View convertView, ViewGroup parent)
public class CompetitionListAdapter extends BaseAdapter { private LayoutInflater mInflater=null; public CompetitionListAdapter(Context context){ this.mInflater=LayoutInflater.from(context); } @Override public int getCount(){ return competitionlist.size(); } @Override public Object getItem(int position){ return competitionlist.get(position); } @Override public long getItemId(int position){ return position; } @Override public View getView(int position, View convertView, ViewGroup parent){ ViewHolder holder = null; if (convertView ==null){ holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.history_list_competition,null); holder.date=(TextView)convertView.findViewById(R.id.list_competition_date); holder.scoreA=(TextView)convertView.findViewById(R.id.list_competition_scoreA); holder.scoreB=(TextView) convertView.findViewById(R.id.list_competition_scoreB); holder.player=(TextView)convertView.findViewById(R.id.list_competition_player); convertView.setTag(holder); }else { holder = (ViewHolder)convertView.getTag(); } holder.date.setText((String)competitionlist.get(position).get("date")); holder.scoreA.setText((String)competitionlist.get(position).get("scoreA")); holder.scoreB.setText((String)competitionlist.get(position).get("scoreB")); holder.player.setText((String)competitionlist.get(position).get("player")); return convertView; } private class ViewHolder{ public TextView date; public TextView player; public TextView scoreB; public TextView scoreA; } }
NotifyDataSetChanged () is required when the data source is modified.
public void setDataList(List<Map<String,Object>> list){ if (list!=null){ competitionlist=list; notifyDataSetChanged(); } }
The content of this note is for personal study. For more information, see blog-igoslly.