1,和最简单的ArrayAdapter的使用:
package com.wt.app;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
public class App extends ListActivity {
String[] array={/*"aaa","bbb","ccc"*/};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//为空时的显示内容
this.getListView().setEmptyView(findViewById(R.id.empty));
ListAdapter adapter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,array);
this.setListAdapter(adapter);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Nothing"
/>
</LinearLayout>
2,和SimpleAdapter的使用:
package com.wt.app;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
public class App extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//绑定Layout里面的ListView
ListView list = (ListView) findViewById(R.id.ListView01);
//生成动态数组,加入数据
ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
int[] images=new int[]{android.R.drawable.ic_menu_add,android.R.drawable.ic_menu_delete,android.R.drawable.ic_menu_edit,android.R.drawable.ic_menu_view};
for(int i=0;i<4;i++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("itemImage", images[i]);//图像资源的ID
map.put("itemTitle", "Title "+i);
map.put("itemText", "this is Text "+i);
listItem.add(map);
}
//生成适配器的Item和动态数组对应的元素
SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,//数据源
R.layout.row,//ListItem的XML实现
//动态数组与ImageItem对应的子项
new String[] {"itemImage","itemTitle", "itemText"},
//ImageItem的XML文件里面的一个ImageView,两个TextView ID
new int[] {R.id.itemImage,R.id.itemTitle,R.id.itemText}
);
//添加并且显示
list.setAdapter(listItemAdapter);
//添加点击
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
setTitle("点击第"+arg2+"个项目");
}
});
//添加长按点击
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
menu.setHeaderTitle("长按菜单-ContextMenu");
menu.add(0, 0, 0, "弹出长按菜单0");
menu.add(0, 1, 0, "弹出长按菜单1");
}
});
}
//长按菜单响应函数
@Override
public boolean onContextItemSelected(MenuItem item) {
setTitle("点击了长按菜单里面的第"+item.getItemId()+"个项目");
return super.onContextItemSelected(item);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ListView01"
/>
</LinearLayout>\
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:paddingTop="4dip"
android:paddingBottom="4dip"
android:paddingLeft="12dip"
android:paddingRight="12dip">
<ImageView
android:paddingTop="12dip"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/itemImage"
/>
<TextView
android:layout_height="wrap_content"
android:textSize="20dip"
android:layout_width="fill_parent"
android:id="@+id/itemTitle"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@+id/itemTitle"
android:id="@+id/itemText"
/>
</RelativeLayout>
3,和SimpleCursorAdapter的使用:
package com.wt.app;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.telephony.PhoneNumberUtils;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class App extends Activity {
private static final String TAG="App";
ListView listView;
ListAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
LinearLayout linearLayout=new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setBackgroundColor(Color.BLACK);
LinearLayout.LayoutParams param=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
listView=new ListView(this);
listView.setBackgroundColor(Color.BLACK);
linearLayout.addView(listView,param);
this.setContentView(linearLayout);
//从数据库获取联系人姓名和电话号码
Cursor cur=this.getContentResolver().query(People.CONTENT_URI,null, null,null,null);
adapter=new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cur,new String[]{People.NAME,People.NUMBER},new int[]{android.R.id.text1,android.R.id.text2});
this.startManagingCursor(cur);
listView.setAdapter(adapter);
//listView.setEmptyView(findViewById(R.id.empty));
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
//openToast("滚动到:"+arg0.getSelectedItemId());
//短信发送
// PendingIntent pi = PendingIntent.getActivity(App.this,0,new Intent(App.this,App.class),0);
// SmsManager sms = SmsManager.getDefault();
// sms.sendTextMessage("5554", null, "message", pi, null);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
// String[] names=((CursorWrapper)listView.getItemAtPosition(position)).getColumnNames();
//从指针的封装类中获得选中项的电话号码并拨号
CursorWrapper wrapper=(CursorWrapper)listView.getItemAtPosition(position);
int columnIndex=wrapper.getColumnIndex(People.NUMBER);
if(!wrapper.isNull(columnIndex)){
String number=wrapper.getString(columnIndex);
Log.d(TAG,"number="+number);
// //判断电话号码的有效性
if(PhoneNumberUtils.isGlobalPhoneNumber(number)){
Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel://"+ number));
startActivity(intent);
}
}
}
});
//添加长按点击,弹出快显菜单
listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
menu.setHeaderTitle("你想要...?").setHeaderIcon(R.drawable.icon);
menu.add(0, 0, 0, "删除").setIcon(R.drawable.icon);
menu.add(0, 1, 0, "修改").setIcon(R.drawable.icon);
}
});
}
//长按菜单响应函数
@Override
public boolean onContextItemSelected(MenuItem item) {
//setTitle("点击了长按菜单里面的第"+item.getItemId()+"个项目");
openToast("你点击了"+item.getTitle());
return super.onContextItemSelected(item);
}
private void openToast(String str){
Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wt.app"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".App"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- 点击拨号时,询问调用默认的程序还是调用本程序拨号 -->
<intent-filter>
<action android:name="android.Intent.Action.CALL_BUTTON" />
<category android:name="android.Intent.Category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-sdk android:minSdkVersion="3" />
</manifest>