2012-02-26 14 views
4

我正在開發一款類似於所有Android手機上的默認短信應用的應用。我的問題是選擇多個用戶發送短信到。到目前爲止,我所做的工作是使用複選框將我的聯繫人存儲爲列表視圖項目。現在我只需要從選定的聯繫人中獲取電話號碼。使用複選框來過濾聯繫人並獲取電話號碼

所以我在做什麼麻煩。 1)拉在我的列表視圖 2顯示的聯繫人顯示在一個新的活動在一個TextView這個數字的電話號碼)

很抱歉,如果我的代碼是很難理解,請詢問您是否需要clerification。

這是在列表視圖中顯示的XML,叫contact_manager.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" 
     android:orientation="vertical" > 

     <ListView 
      android:id="@+id/contactList" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 

     <Button 
      android:id="@+id/showInvisible" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/showInvisible" /> 

    </LinearLayout> 

這是我的活動,共同呼籲一切。

public final class ContactManager extends Activity { 

public static final String TAG = "ContactManager"; 

private ListView mContactList; 
private boolean mShowInvisible; 
private Button mShowInvisibleControl; 

/** 
* Called when the activity is first created. Responsible for initializing 
* the UI. 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    Log.v(TAG, "Activity State: onCreate()"); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.contact_manager); 

    // Obtain handles to UI objects 

    mContactList = (ListView) findViewById(R.id.contactList); 
    mShowInvisibleControl = (Button) findViewById(R.id.showInvisible); 

    // Initialize class properties 
    mShowInvisible = false; 
    // mShowInvisibleControl.setChecked(mShowInvisible); 
    mShowInvisibleControl.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 

     } 
    }); 
    populateContactList(); 
} 

/** 
* Populate the contact list based on account currently selected in the 
* account spinner. 
*/ 
private void populateContactList() { 
    // Build adapter with contact entries 
    Cursor cursor = getContacts(); 
    String[] fields = new String[] { ContactsContract.Data.DISPLAY_NAME }; 
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
      R.layout.contact_entry, cursor, fields, 
      new int[] { R.id.contactEntryText }); 
    mContactList.setAdapter(adapter); 
} 

/** 
* Obtains the contact list for the currently selected account. 
* 
* @return A cursor for for accessing the contact list. 
*/ 
private Cursor getContacts() { 
    // Run query 
    Uri uri = ContactsContract.Contacts.CONTENT_URI; 
    String[] projection = new String[] { ContactsContract.Contacts._ID, 
      ContactsContract.Contacts.DISPLAY_NAME }; 
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" 
      + (mShowInvisible ? "0" : "1") + "'"; 
    String[] selectionArgs = null; 
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME 
      + " COLLATE LOCALIZED ASC"; 

    return managedQuery(uri, projection, selection, selectionArgs, 
      sortOrder); 
} 

contact_entry.xml

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

    <CheckBox 
     android:id="@+id/contactEntryText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+id/contactEntryText" /> 

</LinearLayout> 

<?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" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/contactList" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

    <Button 
     android:id="@+id/showInvisible" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/showInvisible" /> 

</LinearLayout> 

這是我的invite_text.xml這實際上是我想要輸入的數字變成這樣我就可以發送羣發短信文本視圖。

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

     <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/contacts" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="10dp" 
       android:gravity="center" 
       android:paddingBottom="10dp" 
       android:paddingLeft="10dp" 
       android:paddingTop="10dp" 
       android:text="@string/contacts" 
       android:textAppearance="?android:attr/textAppearanceLarge" /> 
      <!-- android:textColor="#fff" android:background="@drawable/header" for header background --> 

      <Button 
       android:id="@+id/contactsButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentTop="true" 
       android:text="@string/contacts" /> 
     </RelativeLayout> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/enter_contact" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <AutoCompleteTextView 
      android:id="@+id/contactnumber" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:hint="@string/to" > 

      <requestFocus /> 
     </AutoCompleteTextView> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/message_to_send" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <EditText 
      android:id="@+id/invite_text" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/message_join" /> 

     <Button 
      android:id="@+id/sendtxt" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:onClick="doLaunchContactPicker" 
      android:text="@string/send_txt" /> 
    </LinearLayout> 

</ScrollView> 

如果你需要我發佈任何詳細信息,請詢問。

+0

仍然還沒有解決這問題:( – 2012-04-01 02:29:56

+0

你好,請張貼合作ntact_entry.xml佈局文件 – 2012-04-06 07:18:38

+0

關於我的問題的我的.xml代碼已發佈,如果您有更多的問題,請在這裏發帖 – 2012-04-07 03:18:19

回答

0

我只是想發佈一個策略答案。使用hashmap作爲列表適配器。使用未顯示的密鑰來存儲要撥打的電話號碼。允許在列表視圖中進行多項選擇。使用所選條目中存儲的電話號碼。

+0

Ooops。用'聯繫信息'替換'電話號碼' – 2012-02-26 03:37:30

1

請按照下面的步驟,使其工作:

- >有一個布爾數組的大小,等於光標。該數組將代表檢查的聯繫狀態。 - >在xml使複選框不可點擊和unfocusable。 - > ListOnW上的setOnItemClickListener和onItemClick方法切換布爾數組的位置,選擇的項目的值。 - >設置OnClickListener的按鈕,監聽器的onClick方法,通過follosing獲取從光標號碼:

ArrayList<String> numbers=new ArrayList<String>(); 
cursor.moveToFirst(); 
for(int i=0;i<cursor.getCount;i++) 
{ 
    cursor.moveToNext(); 
    if(selected[i]) 
    { 
      //fetch contact from cursor 
      //add number to numbers 
    } 
} 

//使用ArrayList號碼發送聯繫,它不列入似乎可以追加多個號碼,以同樣的消息,在的情況下,通過一個循環將消息發送到號碼,請參閱以下主題: Sending sms to multiple people in android AMD Unable to send sms using SMSManager in Android

0

我建議你看看,我上個月寫的,在此link來管理複選框的選擇教程列表(它可以幫助你保持狀態s並通過ID檢索項目)。

我認爲,如果您通過ID管理您的列表,但向用戶顯示聯繫人姓名會更好。之後,您可以傳遞您的ID並使用jeet告訴您的機制發送它們。

1

沒有回答你的問題。您必須編寫自己的適配器並支持多選。我已經回覆的SO到類似的問題(如果這能幫助別人OK讓我知道,我能詳細解釋):

Android listview toggle button

android : checkboxes in a ListView (strange behaviour of selected element)

+0

是否有人有一個你不介意分享的項目的zip文件。我仍然是新的,我正在嘗試做同樣的事情(讓用戶選擇聯繫人,然後在調用活動中獲取選定列表)。分散的代碼壓倒了我一點。標記:@SimoneCasagranda以及(我找不到您的項目的zip)。 – learner 2013-04-07 04:25:25

0

我認爲下面的代碼會給出結果是什麼你預期... 主類將lokk像下面...

import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 

public class GetSelectedContacts extends Activity{ 
    int CONTACTS_REQUEST_CODE =1; 
    Activity thisActivity; 
    ArrayList<String> selectedConatcts; 
    LinearLayout contactdisp; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     thisActivity = this; 
     Button btn = (Button)findViewById(R.id.btn_selectContact); 
     contactdisp = (LinearLayout)findViewById(R.id.lnr_contactshow); 
     btn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent(thisActivity,ListActivitySampleActivity.class); 
       startActivityForResult(intent, CONTACTS_REQUEST_CODE); 
      } 
     }); 


    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if(data!=null){ 
      Bundle bundle = data.getExtras(); 
      if(requestCode ==1){ 
       selectedConatcts = bundle.getStringArrayList("sel_contacts"); 
       Log.v("", "Selected contacts-->"+selectedConatcts); 
       if(selectedConatcts.size()<0){ 

       }else{ 
        for(int i =0;i<selectedConatcts.size();i++){ 
         LinearLayout lnr_inflate = (LinearLayout)View.inflate(thisActivity, R.layout.contacts_inflate, null); 
         EditText edt = (EditText)lnr_inflate.findViewById(R.id.edt_contact); 
         edt.setText(selectedConatcts.get(i)); 
         contactdisp.addView(lnr_inflate); 
        } 

       } 
      } 
     } 
    } 
} 

觸點選擇類像

import java.util.ArrayList; 
import java.util.List; 

import android.app.Activity; 
import android.content.ContentResolver; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.util.Log; 
import android.util.SparseBooleanArray; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 

public class ListActivitySampleActivity extends Activity { 
    static ContentResolver cr; 
    String[] phone_nos; 
    ArrayList<String> selectedContacts = new ArrayList<String>(); 
    Activity thisActivity; 
    Button btn; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.test); 
     thisActivity = this; 
     final ListView lst = (ListView)findViewById(R.id.listView1); 
     populateContact(); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(thisActivity, android.R.layout.simple_list_item_multiple_choice, phone_nos); 
     lst.setAdapter(adapter); 
     lst.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
     lst.setOnItemClickListener(new OnItemClickListener() { 

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

      } 
     }); 
     final int len = lst.getCount(); 
     final SparseBooleanArray checked = lst.getCheckedItemPositions(); 


     btn = (Button)findViewById(R.id.btn_send); 
     btn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) {  
       for (int i = 0; i < len; i++) 
        if (checked.get(i)) { 
         selectedContacts.add(phone_nos[i]); 
         //you can you this array list to next activity 
         /* do whatever you want with the checked item */ 
        } 
       Bundle bundle = new Bundle(); 
       bundle.putStringArrayList("sel_contacts", selectedContacts); 

       Intent contactIntent = new Intent(); 
       contactIntent.putExtras(bundle); 
       setResult(1, contactIntent); 
       thisActivity.finish(); 
//    Log.v("", "selected-->"+selectedContacts); 
      } 
     }); 

    } 
    private void populateContact(){ 
     Uri myContacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI ; 
     Cursor mqCur = managedQuery(myContacts, null, null, null, null); 
     phone_nos = new String[mqCur.getCount()]; 
     int i =0; 
     if(mqCur.moveToFirst()) 
     {    
      do 
      {   
       String phone_no = mqCur.getString(mqCur 
         .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       phone_nos[i] = phone_no; 
       i++; 
      } 

      while(mqCur.moveToNext()); 
     } 
    } 
} 
+0

該代碼與我想要做的相似。除了我需要提取該信息並將其顯示在EditText – 2012-04-12 03:45:41

+0

中,如果您還需要我的xml文件,請與我聯繫。 – Satheesh 2012-04-12 09:10:02

+0

是否可以上傳你的XML文件Satheesh? – dythe 2012-12-07 03:41:42

2

我沒有看過你的代碼,但在這裏是爲我工作機制:

setOnCheckedChangeListener你的複選框。

2.如果CheckboxChecked然後將該聯繫人添加到arraylist

3.如果CheckBoxunchecked然後從arraylist移除接觸。

4.startActivityForResult()開始您的聯繫人列表活動,並覆蓋onActivityResult()

5.之前leaving contact activity設置您的selected contacts in intent

6.接收活動中選定的聯繫人。

7.現在您已選擇聯繫人,您可以在TextView中顯示。

注:您需要使用自定義列表適配器這樣的:

自定義列表適配器:

public class YourAdapterName extends BaseAdapter{ 

private Context mContext; 
private ArrayList<string> mValuestoShow; 

/** 
* Constructor to be called to initialize adapter with values. 
* @param context 
* @param vector 
*/ 
public YourAdapterName(Context context, ArrayList<string> contacts){ 
    mContext = context; 
    mValuestoShow = contacts; 
} 

public int getCount() { 
    if(null != mValuestoShow){ 
     return mValuestoShow.size(); 
    } 
    return 0; 
} 

public Object getItem(int position) { 
    if(position < mValuestoShow.size()) 
     return mValuestoShow.get(position); 
    else 
     return null; 
} 

public long getItemId(int position) { 
    return 0; 
} 

/** 
* This method can be override to enable/disable particular list row. 
*/ 
@Override 
public boolean isEnabled(int position) { 
    //Write your code here...... 
    return super.isEnabled(position); 
} 

public View getView(final int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      LayoutInflater li =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = li.inflate(R.layout.contact_list_layout, null); 
      holder = new ViewHolder(); 
      holder.name = (TextView)convertView.findViewById(R.id.name); 
      holder.checkbox = (CheckBox)convertView.findViewById(R.id.checkbox); 
      convertView.setTag(holder); 
     } 
     else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.name.setText(text goes here ....); 

     holder.checkbox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() { 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if (isChecked) 
        //Add contact... 
       else 
        //Remove contact. 
      } 
     }); 

     return convertView; 
    } 

    class ViewHolder { 
     TextView name; 
     CheckBox checkbox; 
    } 

} 

your_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp" > 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="50dp" 
     android:ellipsize="end" 
     android:singleLine="true" 
     android:textColor="@android:color/black" /> 

    <CheckBox 
     android:id="@+id/checkbox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" /> 

</RelativeLayout> 
+0

我現在正在爲此工作 – 2012-04-13 02:57:56

+0

有沒有人有一個項目的zip,你不介意分享。我仍然是新的,我正在嘗試做同樣的事情(讓用戶選擇聯繫人,然後在調用活動中獲取選定列表)。分散的代碼壓倒了我一點。標記:@jeet也是如此。 – learner 2013-04-07 04:20:13

相關問題