listview分页联系人,并实现打电话功能,

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:id="@+id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="bottom">
<Button android:id="@+id/pre" android:text="@string/pre"
android:layout_width="wrap_content" android:layout_height="wrap_content"
/>
<Button android:id="@+id/next" android:text="@string/next"
android:layout_width="wrap_content" android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>

lvdata.xml配置

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/lv_name"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textSize="15dip" android:textColor="#ffffff" />
<TextView android:id="@+id/lv_phone"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignRight="@id/lv_name"
android:layout_below="@id/lv_name"
  android:textSize="16dip" android:textColor="#00ffff" />
</RelativeLayout>

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">联系人应用</string>
    <string name="pre">上一页</string>
    <string name="next">下一页</string>
    <string name="page">分页</string>
</resources>

在AndroidManifest.xml中配置权限

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>

java代码

package com.contacts;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends Activity {
private ListView lv;
private Button bt1,bt2;
private MyAdapter myadapter;
private View.OnClickListener vc;
private int VIEW_COUNT = 10;
private int index = 0;
private List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
private Map<String,Object> map;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        new String[]{CommonDataKinds.Phone.NUMBER,CommonDataKinds.Phone.DISPLAY_NAME}, null, null, null);
        while(cursor.moveToNext()){
        map = new HashMap<String,Object>();
        //获取联系人姓名
        map.put("name", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
        //获取联系人电话
        map.put("phone", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
        list.add(map);
        }
        vc = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.pre:
leftView();
break;
case R.id.next:
rightView();
break;
}
}
};
        //关闭cursor
        cursor.close();
        bt1.setOnClickListener(vc);
        bt2.setOnClickListener(vc);
        checkButton();
        myadapter = new MyAdapter(this);
        lv.setAdapter(myadapter);
        lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("您确定要打电话?");
builder.setMessage("打电话功能");
builder.setIcon(R.drawable.icon);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
which = position + index * VIEW_COUNT;;
String phone = list.get(which).get("phone").toString();
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone));
startActivity(intent);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
});
    }
private void initView() {
lv = (ListView)findViewById(R.id.list);
bt1 = (Button)findViewById(R.id.pre);
bt2 = (Button)findViewById(R.id.next);
}
public void leftView() {
index--;
myadapter.notifyDataSetChanged();
checkButton();
}


public void rightView() {
index++;
myadapter.notifyDataSetChanged();
checkButton();
}


public void checkButton() {
// 索引值小于等于0,表示不能向前翻页了,以经到了第一页了。
// 将向前翻页的按钮设为不可用。
if (index <= 0) {
bt1.setEnabled(false);
}
// 值的长度减去前几页的长度,剩下的就是这一页的长度,如果这一页的长度比View_Count小,表示这是最后的一页了,后面在没有了。
// 将向后翻页的按钮设为不可用。
else if (list.size() - index * VIEW_COUNT <= VIEW_COUNT) {
bt2.setEnabled(false);
} else {
bt1.setEnabled(true);
bt2.setEnabled(true);
}
}
class MyAdapter extends BaseAdapter{
private Activity activity;
public MyAdapter(Activity activity){
this.activity = activity;
}
@Override
public int getCount() {
// ori表示到目前为止的前几页的总共的个数。
int ori = VIEW_COUNT * index;
/**
* 值的总个数-前几页的个数就是这一页要显示的个数,如果比默认的值小,说明这是最后一页, 只需显示这么多就可以了
*/
if (list.size() - ori < VIEW_COUNT) {
return list.size() - ori;
}
// 如果比默认的值还要大,说明一页显示不完,还要用换一页显示,这一页用默认的值显示满就可以了。
return VIEW_COUNT;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(activity).inflate(R.layout.lvdata, parent,false);
}
TextView name = (TextView)convertView.findViewById(R.id.lv_name);
TextView phone = (TextView)convertView.findViewById(R.id.lv_phone);
Map<String,Object> map = (Map<String, Object>) list.get(position + index * VIEW_COUNT);
if(map != null){
name.setText(map.get("name").toString());
phone.setText(map.get("phone").toString());
}
return convertView;
}
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值