显示从电话簿中选择的联系人以编辑文本

时间:2014-01-21 06:20:54

标签: android android-layout android-edittext

到目前为止所做的是按下“添加联系人”按钮电话簿打开,然后选择一个在Edittext View中显示的联系人,当另一个按钮“添加更多联系人”被按下时,另一个编辑 - 文本视图显示在顶部,我可以再次从电话簿中选择联系人。 但我面临的问题是,当我为第二个编辑文本视图选择联系人时,它会替换先前编辑文本的联系人。在这种情况下该怎么做。 用于显示编辑文本视图的代码

public class MainActivity extends Activity {
private Button btn_cntct;
public int REQUESTCODE = 1;
private LinearLayout layoutLinear;
private Button btn_addmore_cntct;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // defining button elements for picking contacts from phone-book
    btn_cntct = (Button) findViewById(R.id.bpickperson);
    btn_cntct.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            // using Intent for fetching contacts from phone-book
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
            startActivityForResult(intent, REQUESTCODE);
        }

    });

    layoutLinear = (LinearLayout) findViewById(R.id.mLayout);
    btn_addmore_cntct = (Button) findViewById(R.id.baddmorecontacts);
    btn_addmore_cntct.setOnClickListener(OnClick());
    EditText editview = new EditText(this);
    editview.setText("Add more");
}


private OnClickListener OnClick() {
    // TODO Auto-generated method stub
    // changing return type "null" to "new OnClickListner"
    return new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            EditText tab = new EditText(getApplicationContext());
            tab.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            // adding (0) to display edit-text view on the top
            layoutLinear.addView(tab, 0);
            tab.requestFocus();

        }
    };

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (data != null) {
        Uri uri = data.getData();
        Log.i("data", uri.toString());
        if (uri != null) {
            Cursor c = null;
            try {
                c = getContentResolver()
                        .query(uri,
                                new String[] {
                                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                                        ContactsContract.CommonDataKinds.Phone.NUMBER,
                                        ContactsContract.CommonDataKinds.Phone.TYPE },
                                null, null, null);

                if (c != null && c.moveToFirst()) {
                    String name = c.getString(0);
                    String number = c.getString(1);
                    int type = c.getInt(2);

                    showSelectedNumber(name, number, type);
                }
            } finally {
                if (c != null) {
                    c.close();
                }
            }
        }
    }
}


public void showSelectedNumber(String name, String number, int type) {
    EditText userNumber = (EditText) findViewById(R.id.edittext);
    String typeNumber = (String) ContactsContract.CommonDataKinds.Phone
            .getTypeLabel(getResources(), type, "");
    userNumber.setText(name + ": " + number + " " + typeNumber);

}

我的logcat是

01-21 14:29:01.766: W/IInputConnectionWrapper(590): showStatusIcon on inactive InputConnection
01-21 14:29:04.327: I/data(590): content://com.android.contacts/data/1
01-21 14:29:04.387: I/layoutLinear is not  null(590): not null
01-21 14:29:04.387: I/edittext is null(590): null
01-21 14:29:04.437: D/AndroidRuntime(590): Shutting down VM
01-21 14:29:04.437: W/dalvikvm(590): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-21 14:29:04.477: E/AndroidRuntime(590): FATAL EXCEPTION: main
01-21 14:29:04.477: E/AndroidRuntime(590): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/data/1 flg=0x1 (has extras) }} to activity {com.example.zzz1/com.example.zzz1.MainActivity}: java.lang.NullPointerException
01-21 14:29:04.477: E/AndroidRuntime(590):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
01-21 14:29:04.477: E/AndroidRuntime(590):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
01-21 14:29:04.477: E/AndroidRuntime(590):  at android.app.ActivityThread.access$2000(ActivityThread.java:117)
01-21 14:29:04.477: E/AndroidRuntime(590):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
01-21 14:29:04.477: E/AndroidRuntime(590):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 14:29:04.477: E/AndroidRuntime(590):  at android.os.Looper.loop(Looper.java:123)
01-21 14:29:04.477: E/AndroidRuntime(590):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-21 14:29:04.477: E/AndroidRuntime(590):  at java.lang.reflect.Method.invokeNative(Native Method)
01-21 14:29:04.477: E/AndroidRuntime(590):  at java.lang.reflect.Method.invoke(Method.java:507)
01-21 14:29:04.477: E/AndroidRuntime(590):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-21 14:29:04.477: E/AndroidRuntime(590):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-21 14:29:04.477: E/AndroidRuntime(590):  at dalvik.system.NativeStart.main(Native Method)
01-21 14:29:04.477: E/AndroidRuntime(590): Caused by: java.lang.NullPointerException
01-21 14:29:04.477: E/AndroidRuntime(590):  at com.example.zzz1.MainActivity.showSelectedNumber(MainActivity.java:145)
01-21 14:29:04.477: E/AndroidRuntime(590):  at com.example.zzz1.MainActivity.onActivityResult(MainActivity.java:115)
01-21 14:29:04.477: E/AndroidRuntime(590):  at android.app.Activity.dispatchActivityResult(Activity.java:3908)
01-21 14:29:04.477: E/AndroidRuntime(590):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
01-21 14:29:04.477: E/AndroidRuntime(590):  ... 11 more

3 个答案:

答案 0 :(得分:1)

您应该做的是每次创建新的EditText时都必须为创建的EditText提供ID,如下所示:

private int id = 1 ; 

private OnClickListener OnClick() {
    // TODO Auto-generated method stub
    // changing return type "null" to "new OnClickListner"
    return new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            EditText tab = new EditText(getApplicationContext());
            tab.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

        // here you set the ID 
        tab.setId(id);
        // and increment it 
        id++;
            // adding (0) to display edit-text view on the top
            layoutLinear.addView(tab,0);
            tab.requestFocus();

        }
    };

}

然后在showSelectedNumber方法中,您应该从LinearLayout获取EditText并通过您最近创建的EditText的id获取它,如下所示:

public void showSelectedNumber(String name, String number, int type) {

    if(layoutLinear== null ) {
        Log.i("layoutLinear is null" , "null");
    }else {
        Log.i("layoutLinear is not  null" , "not null");

    }

    EditText userNumber = (EditText) layoutLinear.getChildAt(0);
    if(userNumber == null ) {
        Log.i("edittext is null" , "null");
    }else {
        Log.i("edittext is not  null" , "not null");

    }
    String typeNumber = (String) ContactsContract.CommonDataKinds.Phone
            .getTypeLabel(getResources(), type, "");
    userNumber.setText(name + ": " + number + " " + typeNumber);

}

希望有助于此。

答案 1 :(得分:0)

showSelectedNumber方法中,EditText userNumber始终是第一个EditText,因为您尝试通过R.id.edittext找到它。这就是为什么第一个EditText的内容会被更改的原因。

您应该在id中为新添加的EditText分配新的OnClickListener

EditText tab = new EditText(getApplicationContext());
// For example
tab.setId( A_NEW_ID );

然后,在showSelectedNumber

EditText userNumber = (EditText) findViewById( A_NEW_ID );

=============================================== ===

或者,您可以在列表中添加新添加的EditText。然后在每次添加新联系人时使用最后一个。

答案 2 :(得分:0)

当您单击EditText时,

在类变量中存储引用,然后在setText()时将此EditText传递给方法。

public void showSelectedNumber(String name, String number, int type,EditText et) {
EditText userNumber = et;
String typeNumber = (String) ContactsContract.CommonDataKinds.Phone
        .getTypeLabel(getResources(), type, "");
userNumber.setText(name + ": " + number + " " + typeNumber);

}