如何在通讯录中显示对话框

时间:2012-02-06 06:17:05

标签: android contacts contact

我使用此代码作为按钮,之后我在“联系人活动”中:

btnPhonebook.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent pb = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(pb, 1);
        }
    });

之后,会有一个包含许多联系人的联系人列表。现在我想要的是,每当我点击一个联系人时,都会显示一个对话框。我怎样才能做到这一点。任何人都可以帮助我,因为目前我没有问题如何制作它。我试过这个代码,但它没有用。

Dialog dialog = new Dialog(this);
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Choose a phone number");

            ListView lp = new ListView(this);
            lp.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, _listPhoneNumber));

            builder.setView(lp);
            dialog = builder.create();

我还将上面的代码放在onActivityResult()方法中。

提前致谢。

5 个答案:

答案 0 :(得分:0)

而不是使用内置意图来显示联系人..为什么不自己构建列表,然后执行您打算做的事情。

您是否将对话框设置为.show()?

答案 1 :(得分:0)

非常简单,

progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true);

new Thread ( new Runnable()
{
     public void run()
     {
      // your loading code goes here
     }
}).start();

 Handler progressHandler = new Handler() 
 {

     public void handleMessage(Message msg1) 
     {

         progDailog.dismiss();
         }
 }

答案 2 :(得分:0)

您无法在原生联系人应用中显示Dialog

但是,您可以自己提取联系人,并使用ListViewCheckBox中显示,然后继续下一步。

Here是最简单的ListView来显示联系人。你可以自己编辑。

答案 3 :(得分:0)

我已在我的应用程序中完成了如何从listview获取联系人详细信息, 使用此数据,您可以显示对话框。

    lv.setClickable(true);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView <? > arg0, View arg1, int position, long arg3) {

        Object o = lv.getItemAtPosition(position);
        Toast.makeText(getApplicationContext(), o.toString(), Toast.LENGTH_LONG).show();
        String infoString = o.toString();
        String arr[] = infoString.split(",");
        String names[] = arr[1].split("=");
        id = Integer.parseInt(names[1]);
        System.out.println("info" + id);

        db = dh.getReadableDatabase();

        String select = "select * from '" + dh.tablename + "' WHERE adb_id='" + id + "' ";
        Cursor c = db.rawQuery(select, null);

        if (c.moveToFirst()) {

            name.setText(c.getString(1));
            address.setText(c.getString(2));
            contact.setText(c.getString(3));    
        }    
    }
});

答案 4 :(得分:-1)

如果您想在联系中使用对话框,那么您必须为联系人制作自己的自定义布局。例如,请参阅下面的代码... 点击按钮

public static final int NUMBER_SELECT = 1;
Intent intent = new Intent(clsBlockNumbers.this,CallLog_Activity.class);
startActivityForResult(intent,NUMBER_SELECT);

在同一个Activity中写/ make onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            switch(requestCode) {
            case NUMBER_SELECT: 
                if (resultCode == RESULT_OK) {
                    String number = data.getStringExtra("SelectedNumber");
                    if(number == null)
                    {
                        Toast.makeText(this, "No Record found: ", Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                        //Your code
                    }
                    break;
                }

            }

        }

CallLog_Activity.java

package com.demo;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;


public class CallLog_Activity extends Activity implements OnItemClickListener
{
    ArrayList<String> strAyyNumber,strAyyType,listNumber, strType, strAyyName ;

    private CallLogListAdapter adapter ;
    CallLog callLog;
    String noType;
    ListView listCallLog;
    private String[] listCallLog_arr={};
    Cursor cursor;
    String strArr;
    TextView tv, tv1, txtEmptyMsg;


    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.calllog_list);

        callLog = new CallLog();

        listCallLog = (ListView)findViewById(R.id.list);

        strAyyNumber = new ArrayList<String>();
        strAyyType = new ArrayList<String>();
        strAyyName = new ArrayList<String>();
        listNumber = new ArrayList<String>();
        strType = new ArrayList<String>();

        System.out.println("In Call log list activity");            
        try
        {

            final String[] projection = null;
            final String selection = null;
            final String[] selectionArgs = null;
            final String sortOrder = "DATE DESC";
            Cursor cursor = this.getContentResolver().query(
                    Uri.parse("content://call_log/calls"),
                    projection,
                    selection,
                    selectionArgs,
                    sortOrder);
            if (cursor != null) 
            {
                //Loop through the call log.
                while (cursor.moveToNext()) 
                { 
                    //Common Call Log Items
                    String callNumber = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
                    strAyyNumber.add(callNumber);

                    String callType = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.TYPE));
                    strAyyType.add(callType);

                    String callName = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
                    strAyyName.add(callName);



                }               

                for(int i=0;i<strAyyNumber.size();i++)
                {
                    String no = strAyyNumber.get(i).toString();//.concat("\n").concat(strAyyType.get(i).toString());
                    Log.d("No length ", "No length ::" + no.length());
                    listNumber.add(no);
                }


                listCallLog_arr = listNumber.toArray(new String[listNumber.size()]);

                Log.d("size", "list listCallLog_arr"+ listCallLog_arr.length);

                if(!listNumber.isEmpty())
                {
                    listCallLog.setVisibility(View.VISIBLE);
                    adapter = new CallLogListAdapter(CallLog_Activity.this,R.layout.calllog_list_row, listCallLog_arr,strAyyNumber,strAyyType,strAyyName);
                    listCallLog.setAdapter(adapter);
                }
                else
                {   
                    txtEmptyMsg = (TextView)findViewById(R.id.txtEmptyMsg);
                    txtEmptyMsg.setVisibility(View.VISIBLE);
                    txtEmptyMsg.setText("No Record found Press Back to Continue");
                }
                listCallLog.setOnItemClickListener(this);

            }
            listCallLog.setOnItemClickListener(this);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }


    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
    {

        String o = arg0.getItemAtPosition(position).toString();
        Intent returnIntent = new Intent();

        StringBuffer sb = new StringBuffer(o);
        sb.reverse().setLength(10);

        Log.d("Item click", "String buffer"+ sb);

        String revercenum = sb.toString().trim();

        Log.d("Item click", "revercenum "+ revercenum);

        StringBuffer sb1 = new StringBuffer(revercenum);
        sb1.reverse();
        Log.d("Item click", "sb1 "+ sb1);

        String revercenum1 = sb1.toString().trim();
        revercenum1.replace("+", "");
        Log.d("Item click", "revercenum 1"+ revercenum1);

        returnIntent.putExtra("SelectedNumber",revercenum1.replace("+", ""));
        setResult(RESULT_OK,returnIntent);      
        finish();
    }
}

colllog_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ListView
android:id="@+id/list"
android:layout_width="fill_parent" android:layout_height="fill_parent" 
android:visibility="gone"
/>

<TextView android:id="@+id/txtEmptyMsg"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:textStyle="bold"
        android:textSize="25dp"
        android:text=""
        android:visibility="gone"
    /> 

</LinearLayout>

colllog_list_row.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="horizontal">


        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">


                <TextView
                    android:id="@+id/txtCallLogName"
                    android:layout_width="wrap_content" android:layout_height="fill_parent" 
                    android:textSize="20dp" android:layout_margin="10dp" 
                    android:layout_weight="1" />

                <TextView
                    android:id="@+id/txtCallLogNumber"
                    android:layout_width="wrap_content" android:layout_height="fill_parent" 
                    android:textSize="20dp" android:layout_weight="1"
                    android:layout_marginLeft="10dp" android:layout_marginBottom="10dp" 
                    android:layout_marginTop="5dp" android:layout_marginRight="10dp"    />

        </LinearLayout>

        <TextView
            android:id="@+id/txtCallLogType"
            android:layout_width="wrap_content" android:layout_height="fill_parent" 
            android:textSize="12dp"  android:layout_marginBottom="10dp" 
            android:layout_marginTop="10dp" android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"  />

</RelativeLayout>

CallLogListAdapter.java

package com.Demo;

import java.util.ArrayList;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CallLogListAdapter extends ArrayAdapter<Object>
{
    private static final String TAG = "CallLogListAdapter";
    private LayoutInflater inflater = null;
    private int resource;
    private Activity activity;
    CallLog callLog ;
    String[] strTemp;
    ArrayList<String> arrayItem = new ArrayList<String>();
    ArrayList<String> ayyType = new ArrayList<String>();
    ArrayList<String> tempType = new ArrayList<String>();
    ArrayList<String> ayyName = new ArrayList<String>();
    ArrayList<String> tempName = new ArrayList<String>();
    ArrayList<String> tempName1 = new ArrayList<String>();

    ArrayList<String> tempNo = new ArrayList<String>();

    String strType, strName, strName1, strNo ;
    String[] tmpName, tmpName1, tmpNo;

    public CallLogListAdapter(Activity activity, int resorce, String[] strTemp, ArrayList<String> arryListNumber, ArrayList<String> arryListType, ArrayList<String> arryListName) 
    {
        super(activity, resorce,strTemp);
        this.resource = resorce;
        this.activity = activity;
        this.strTemp = strTemp;
        Log.d("in adapter", "In Adapter");
        arrayItem = arryListNumber;
        ayyType = arryListType;
        ayyName = arryListName;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        Log.d("in adapter", "In get View");

        ViewHolder holder;

        if (convertView == null)
        {

            LayoutInflater layoutInflater = LayoutInflater.from(getContext());
            convertView  = layoutInflater.inflate(resource, parent, false);
            holder = new ViewHolder();

            holder.txtName = (TextView)convertView.findViewById(R.id.txtCallLogName);
            holder.txtNumber = (TextView)convertView.findViewById(R.id.txtCallLogNumber);
            holder.txtType = (TextView)convertView.findViewById(R.id.txtCallLogType);

            holder.txtName.setVisibility(View.VISIBLE);
            try
            {
                for(int i=0;i<ayyName.size();i++)
                {
                    strName = ayyName.get(i);
                    Log.d("in get view in  ", "Name is: **"+ strName);
                    tempName.add(strName);  
                }

                tmpName = tempName.toArray(new String[tempName.size()]);
                if(tmpName[position] == null)
                {
                    holder.txtName.setVisibility(View.GONE);                
                }
                else
                {
                    holder.txtName.setVisibility(View.VISIBLE);
                    holder.txtNumber.setTextSize(12);
                    holder.txtName.setText(""+ tmpName[position]);
                }


            }
            catch (NullPointerException e) 
            {
                e.printStackTrace();
            }


            for(int i=0;i<arrayItem.size();i++)
            {
                strNo = arrayItem.get(i);//.toString();
                Log.d("in get view in  ", "Number is: **"+ strNo);
                tempNo.add(strNo);
            }

            String[] tmpNo = tempNo.toArray(new String[tempNo.size()]);

            Log.d("in get view ", "Number is String[]** : "+ tmpNo[position]);
            holder.txtNumber.setText(""+ tmpNo[position]);

            for(int i=0;i<ayyType.size();i++)
            {
                strType = ayyType.get(i).toString();

                if(strType.equalsIgnoreCase("1"))
                {
                    strType = "Incoming Call";
                }
                else if(strType.equalsIgnoreCase("2"))
                {
                    strType = "Outgoing Call";

                }
                else if(strType.equalsIgnoreCase("3"))
                {
                    strType = "Missed Call";
                }
                tempType.add(strType);
            }

            String[] tmpType = tempType.toArray(new String[tempType.size()]);

            holder.txtType.setText(""+ tmpType[position]);
            convertView.setTag(holder);

        } else {
             holder=(ViewHolder)convertView.getTag();
        }
        return convertView;
    }

    public static class ViewHolder
    {
        private TextView txtNumber, txtType, txtName;
    }

}

我希望它可能对你有所帮助.. :)