如何从复选框中获取选定的联系人 (Android)

发布于 2024-10-03 18:35:54 字数 3664 浏览 2 评论 0原文

我正在创建一个 Android 短信群。因此,我需要复选框。但是,我的问题是我不知道如何获取选定的联系人?

这是我的布局:

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/topLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true">

<ListView 
 android:id="@+id/list" 
 android:layout_width="fill_parent" android:layout_height="fill_parent"        android:choiceMode="multipleChoice">
</ListView>

<ImageView android:id="@+id/contact_image"
    android:src="@drawable/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
<TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Name: "
     android:id="@+id/contact_name"
     android:textSize="18dip"
     android:layout_below="@id/contact_image"      
        />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Phone: "
           android:id="@+id/phone_number"
            android:textSize="28dip"
            android:layout_below="@id/contact_name"  


         />
         <CheckBox android:id="@+id/checkBox"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
    android:checked="false" android:layout_alignParentRight="true"     android:focusable="false">
   </CheckBox>

  </RelativeLayout>

这是我的代码:-

  package com.droidnova.android.samples;

   import android.app.ListActivity;
   import android.content.Intent;
   import android.database.Cursor;
   import android.os.Bundle;
  import android.provider.Contacts.People;
  import android.util.SparseBooleanArray;
  import android.view.Menu;
 import android.view.MenuItem;
 import android.view.MenuInflater;
 import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
 import android.widget.SimpleCursorAdapter;



   public class Contacts extends ListActivity {
private CheckBox checkBox;
private ListView listView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    listView=(ListView)findViewById (R.id.list);
    final ListView listView = getListView();
    listView.setItemsCanFocus(false);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setAdapter(new ArrayAdapter<String>(this,  
     android.R.layout.simple_list_item_multiple_choice, lv_items));  





    Cursor cursor = getContentResolver().query(People.CONTENT_URI, 
            new String[]{People._ID,People.NAME,People.NUMBER}, null, null, null);
    startManagingCursor(cursor);

    // start mappings
    String[] columns = new String[] {People.NAME, People.NUMBER};
    int[] names = new int[] {R.id.contact_name, R.id.phone_number};

    SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.contact,   cursor, columns, names);
    setListAdapter(myAdapter);
    this.setListAdapter(myAdapter);
}



     public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.firstmenu,menu);
    return true;  
  }  
    public boolean onOptionsItemSelected(MenuItem item){
  switch(item.getItemId()){
  case R.id.next:
      next();
    break;
  case R.id.select:
     return true;
  case R.id.back:
      final Intent i = new Intent(this,SelectContact.class);
      startActivity(i);
      break;
  }
return false;
  }

I am creating a group android SMS. Therefore, i need checkbox. But,my problem is i cant figure out how to get the selected contacts?

This my layout:

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/topLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true">

<ListView 
 android:id="@+id/list" 
 android:layout_width="fill_parent" android:layout_height="fill_parent"        android:choiceMode="multipleChoice">
</ListView>

<ImageView android:id="@+id/contact_image"
    android:src="@drawable/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
<TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Name: "
     android:id="@+id/contact_name"
     android:textSize="18dip"
     android:layout_below="@id/contact_image"      
        />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Phone: "
           android:id="@+id/phone_number"
            android:textSize="28dip"
            android:layout_below="@id/contact_name"  


         />
         <CheckBox android:id="@+id/checkBox"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
    android:checked="false" android:layout_alignParentRight="true"     android:focusable="false">
   </CheckBox>

  </RelativeLayout>

This my codes:-

  package com.droidnova.android.samples;

   import android.app.ListActivity;
   import android.content.Intent;
   import android.database.Cursor;
   import android.os.Bundle;
  import android.provider.Contacts.People;
  import android.util.SparseBooleanArray;
  import android.view.Menu;
 import android.view.MenuItem;
 import android.view.MenuInflater;
 import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
 import android.widget.SimpleCursorAdapter;



   public class Contacts extends ListActivity {
private CheckBox checkBox;
private ListView listView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    listView=(ListView)findViewById (R.id.list);
    final ListView listView = getListView();
    listView.setItemsCanFocus(false);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setAdapter(new ArrayAdapter<String>(this,  
     android.R.layout.simple_list_item_multiple_choice, lv_items));  





    Cursor cursor = getContentResolver().query(People.CONTENT_URI, 
            new String[]{People._ID,People.NAME,People.NUMBER}, null, null, null);
    startManagingCursor(cursor);

    // start mappings
    String[] columns = new String[] {People.NAME, People.NUMBER};
    int[] names = new int[] {R.id.contact_name, R.id.phone_number};

    SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.contact,   cursor, columns, names);
    setListAdapter(myAdapter);
    this.setListAdapter(myAdapter);
}



     public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.firstmenu,menu);
    return true;  
  }  
    public boolean onOptionsItemSelected(MenuItem item){
  switch(item.getItemId()){
  case R.id.next:
      next();
    break;
  case R.id.select:
     return true;
  case R.id.back:
      final Intent i = new Intent(this,SelectContact.class);
      startActivity(i);
      break;
  }
return false;
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮光沉寂 2024-10-10 18:35:54

分别读取数组中的联系人,然后对该数组执行任何您想要的操作.........

对于示例,您可以在此处查看,
http://www.higherpass.com/Android/Tutorials/Working- With-Android-Contacts/

这会对你有帮助......

Read contacts separately in array, then do whatever you want with that array.........

For sample, you can check over here,
http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/

This will help you.........

旧人哭 2024-10-10 18:35:54

这是我更新的代码。每当我单击“标记”菜单按钮时,它都不会获取所选项目并显示在“编辑文本”中。

更新的代码:-

    package com.droidnova.android.samples;

   import java.util.ArrayList;
   import java.util.zip.Inflater;
   import android.app.ListActivity;
   import android.content.Context;
   import android.content.Intent;
   import android.database.Cursor;
    import android.os.Bundle;
    import android.provider.Contacts.People;
    import android.util.Log;
    import android.util.SparseBooleanArray;
    import android.view.Menu;
   import android.view.MenuItem;
 import android.view.MenuInflater;
 import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.ListView;
 import android.widget.SimpleCursorAdapter;

 public class Contacts extends ListActivity {
ListView list;
CheckBox cb;


private static String[] PROJECTION = new String[]
{
    People._ID,
    People.NAME,
    People.NUMBER,
};

  @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.contact);
    Cursor cursor = getContentResolver().query(People.CONTENT_URI, 
            null, null, null, null);
    startManagingCursor(cursor);


    // start mappings
    String[] columns = new String[] {People.NAME, People.NUMBER};
    int[] names = new int[] {R.id.contact_name, R.id.phone_number};

    ListAdapter mAdapter = new SimpleCursorAdapter(this,R.layout.contact, cursor,columns,names);
    setListAdapter(mAdapter);
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

  }


 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.firstmenu,menu);
    return true;  
 }  
 public boolean onOptionsItemSelected(MenuItem item){
  switch(item.getItemId()){
  case R.id.mark:
    next();


    break;
  case R.id.select:



      return true;
  case R.id.back:
      final Intent i = new Intent(this,SelectContact.class);
      startActivity(i);
      return true;
  }
return false;
  }
    private void next() {
// TODO Auto-generated method stub

  SparseBooleanArray a = list.getCheckedItemPositions();

  for(int i = 0; i < PROJECTION.length ; i++) 

  {

  if (a.valueAt(i))

  {

  Long val = list.getAdapter().getItemId(a.keyAt(i));

  Log.v(TAG, "index=" + val.toString());

  list.add(list.getAdapter().getItemId((a.keyAt(i))));

  }

  }

}

}

This my updated codes. Whenever, i click on the 'Mark' menu button, it doesnt get the selected item and display at Edit Text.

updated Codes:-

    package com.droidnova.android.samples;

   import java.util.ArrayList;
   import java.util.zip.Inflater;
   import android.app.ListActivity;
   import android.content.Context;
   import android.content.Intent;
   import android.database.Cursor;
    import android.os.Bundle;
    import android.provider.Contacts.People;
    import android.util.Log;
    import android.util.SparseBooleanArray;
    import android.view.Menu;
   import android.view.MenuItem;
 import android.view.MenuInflater;
 import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.ListView;
 import android.widget.SimpleCursorAdapter;

 public class Contacts extends ListActivity {
ListView list;
CheckBox cb;


private static String[] PROJECTION = new String[]
{
    People._ID,
    People.NAME,
    People.NUMBER,
};

  @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.contact);
    Cursor cursor = getContentResolver().query(People.CONTENT_URI, 
            null, null, null, null);
    startManagingCursor(cursor);


    // start mappings
    String[] columns = new String[] {People.NAME, People.NUMBER};
    int[] names = new int[] {R.id.contact_name, R.id.phone_number};

    ListAdapter mAdapter = new SimpleCursorAdapter(this,R.layout.contact, cursor,columns,names);
    setListAdapter(mAdapter);
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

  }


 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.firstmenu,menu);
    return true;  
 }  
 public boolean onOptionsItemSelected(MenuItem item){
  switch(item.getItemId()){
  case R.id.mark:
    next();


    break;
  case R.id.select:



      return true;
  case R.id.back:
      final Intent i = new Intent(this,SelectContact.class);
      startActivity(i);
      return true;
  }
return false;
  }
    private void next() {
// TODO Auto-generated method stub

  SparseBooleanArray a = list.getCheckedItemPositions();

  for(int i = 0; i < PROJECTION.length ; i++) 

  {

  if (a.valueAt(i))

  {

  Long val = list.getAdapter().getItemId(a.keyAt(i));

  Log.v(TAG, "index=" + val.toString());

  list.add(list.getAdapter().getItemId((a.keyAt(i))));

  }

  }

}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文