本文共 6578 字,大约阅读时间需要 21 分钟。
package com.example.leiqi.listview;import android.support.annotation.NonNull;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.LinearLayout;import android.widget.ListView;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;import java.util.List;import java.util.ListIterator;/** * ListView的使用ArrayAdapter */public class L extends AppCompatActivity { private LinearLayout mlayout; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_l); mlayout = (LinearLayout) this.findViewById(R.id.mlayout); listView = new ListView(this); //创建ArrayAdapter适配器,android.R.layout.simple_expandable_list_item_1:系统定义好的只显示一行文字。 ArrayAdapter对应的布局文件adapter = new ArrayAdapter (this,android.R.layout.simple_expandable_list_item_1,getData()); listView.setAdapter(adapter); mlayout.addView(listView); }/** * 获取数据 * @return List */public List getData(){ List mlist = new ArrayList (); mlist.add("数据项1"); mlist.add("数据项2"); mlist.add("数据项3"); return mlist;}}
package com.example.leiqi.cehua;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Objects;/** * */public class L2 extends Activity { private LinearLayout mlayout; private ListView mlist; //创建List对象,用于存放列表项没一行的列表信息 List对应的布局文件
listviewrow.xml 在layout目录下、
列表按下、获取焦点、选中时的效果res/drawable/trippoilistviewbg.xml
package com.example.leiqi.cehua;import android.app.Activity;import android.database.Cursor;import android.provider.Contacts;import android.support.v4.widget.SimpleCursorAdapter;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.LinearLayout;import android.widget.ListAdapter;import android.widget.ListView;/** * */public class L3 extends Activity {private ListView listView; private LinearLayout mlayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_l3); mlayout = (LinearLayout) findViewById(R.id.l3); listView = new ListView(this); Cursor cursor = getContentResolver().query(Contacts.People.CONTENT_URI,null,null,null,null); startManagingCursor(cursor); ListAdapter listAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_1, cursor,new String[]{Contacts.People.NAME}, new int[]{ android.R.id.text1}); listView.setAdapter(listAdapter); mlayout.addView(listView); }}
对应的布局文件