转载请注明出处:http://blog.csdn.net/qq_32451699/article/details/52422390
实例1.ArrayAdapter做适配器
avtivity_list_test1.xml文件:
ListTest1Activity.java文件:
package com.example.control;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ListTestActivity extends Activity {
private ListView listview;
private List list;
private ArrayAdapter aa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_test);
//实例化列表视图
listview = (ListView) findViewById(R.id.listView1);
//listview使用的MVC开发模型,其中listview是视图,Adapter是模型,在java源文件中进行控制
//list为listview提供显示的数据
list=new ArrayList();
list.add("刘德华");
list.add("刘德华");
list.add("刘德华");
list.add("刘德华");
list.add("刘德华");
//设置listview的适配器,使用android系统提供的布局文件android.R.layout.simple_list_item_1
aa=new ArrayAdapter(this, android.R.layout.simple_list_item_1,list);
listview.setAdapter(aa);
//为listview设置列表点击监听器用于监听用户点击Listview中每一项的点击事件
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(ListTestActivity.this,"别点我",Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.list_test, menu);
return true;
}
}
实例2.SimpleAdapter做适配器
activity_list_test2.xml文件:
还需要提供一个user.xml布局用于listview每一行里面的1布局效果:
ListTest2Activity.java文件:
package com.example.control;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class ListTest2Activity extends Activity {
private ListView listview2 = null;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_test2);
//实例化listview2
listview2 = (ListView) findViewById(R.id.listView2);
//ArrayList里面放的是HashMap,HashMap里面放着三个key-value为布局文件中的三个组件提供数据
ArrayList> list = new ArrayList>();
@SuppressWarnings("rawtypes")
HashMap[] map = new HashMap[10];
//向HashMap中添加key-value键值对
for(int i=0;i<10;i++)
{
map[i]=new HashMap();
map[i].put("image", R.drawable.liudehua);
map[i].put("name", "刘德华");
map[i].put("phone", "1234567");
list.add(map[i]);
}
//SimpleAdapter需要提供五个参数
//第一个参数是上下文对象,就在当前activity中,因此使用this
//第二个参数是list列表用提供listview中将显示那些数据
//第三个参数是布局文件,决定listview每一行的布局样式
//第四个参数是需要显示的value1所对应的key值,是一个字符串数组
//第五个参数是每一项布局中用那些控件来显示数据,注意此处一顺序与第四个参数中的顺序保持一致
SimpleAdapter listadapter = new SimpleAdapter(this, list,
R.layout.user, new String[] { "image", "name", "phone" },
new int[] { R.id.img1, R.id.textView3, R.id.textView4 });
//为listview设置适配器,其实就是为view设置model
listview2.setAdapter(listadapter);
//为listview注册点击项监听器
listview2.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(ListTest2Activity.this,"你点击了列表"+arg2,Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.list_test2, menu);
return true;
}
}
运行效果:
activity_main.xml文件:
group.xml文件为每一父列表项提供布局:
item.xml文件为每一子列表项提供布局:
MainActivity.java文件:
package com.example.expandablelistviewtest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.LinearLayout;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ExpandableListView elvFirst;
private ExpandableListView elvSecond;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化第一个可扩展列表框
initFirstExpandableListView();
//初始化第二个可扩展列表框
initSecondExpandableListView();
}
private void initFirstExpandableListView(){
elvFirst = (ExpandableListView)findViewById(R.id.elvFirst);
//list1为组列表框的每一个列表项提供数据
List
运行效果:
activity_main.xml布局文件:
picturegriditem.xml文件为每个项提供布局:
MainActivity.java文件:
package com.example.gridviewtest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private GridView gv;
private int[] images = new int[]{
R.drawable.st0,
R.drawable.st1,
R.drawable.st2,
R.drawable.st3,
R.drawable.st4,
R.drawable.st5,
R.drawable.st6,
R.drawable.st7,
R.drawable.st8,
R.drawable.st9,
};
String[] movieName = new String[]{
"谎言西西里",
"情况不妙",
"冰川时代",
"危城",
"封神传奇",
"盗墓笔记",
"使徒行者",
"超越星辰",
"宝贝当家",
"爱宠大机密",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initGridView();
}
@SuppressWarnings("unchecked")
private void initGridView(){
gv = (GridView)findViewById(R.id.gridview);
List> list =new ArrayList>();
@SuppressWarnings("rawtypes")
HashMap[] map = new HashMap[10];
for(int i = 0 ; i<10 ; i++){
map[i] = new HashMap();
map[i].put("image", images[i]);
map[i].put("name", movieName[i]);
list.add(map[i]);
}
SimpleAdapter sa = new SimpleAdapter(this, list, R.layout.picturegriditem, new String[]{"image","name"},
new int[]{R.id.imageView1,R.id.ItemText});
//设置适配器
gv.setAdapter(sa);
//注册监听器
gv.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("rawtypes")
@Override
public void onItemClick(AdapterView> arg0, View arg1,int arg2, long arg3) {
Toast.makeText(MainActivity.this,"你点击了:"+ ((HashMap)arg0.getItemAtPosition(arg2)).get("name").toString(), Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
运行效果: