Multichoice ListView

时间:2016-06-16 06:50:13

标签: android sqlite listview

我需要制作联系人的多重列表视图,当我点击确认按钮时,应用程序应该将手机号码插入数据库。

listview片段代码是:

public class GestionContact  extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gestion_contact);

    //Récupère le fragment permettant d'accèder à la liste des contacts

    getSupportFragmentManager().beginTransaction().replace(R.id.fragmentLayout,new MonFragment()).commit();
}

我的活动:

@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');

.multiBanner {
  position: relative;
  margin: auto;
  top: 0px;
  left: 0;
  right: 0;
  z-index: 0;
}
@media (max-width: 767px) {
  .multiBanner {
    display: none;
  }
}
.multiBanner .treatment {
  height: 575px;
  border-left: 2.5px solid white;
  background: linear-gradient(rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0.55)), url(../img/1.jpeg);
  background-size: cover;
  background-position-y: 65%;
}
@media (max-width: 992px) {
  .multiBanner .treatment {
    height: 650px;
  }
}
.multiBanner .webshop {
  height: 575px;
  border-right: 2.5px solid white;
  background: linear-gradient(rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0.55)), url(../img/2.jpeg);
  background-size: cover;
  background-position-y: 87%;
}
@media (max-width: 992px) {
  .multiBanner .webshop {
    height: 650px;
  }
}
.multiBannerText {
  position: absolute;
  z-index: 1;
  margin: auto;
  top: 200px;
  left: 0;
  right: 0;
}
@media (max-width: 767px) {
  .multiBannerText {
    margin-top: -20px;
    position: relative;
    top: 0;
  }
}
.multiBannerText .col-sm-6:first-child {
  padding-right: 50px;
}
@media (max-width: 767px) {
  .multiBannerText .col-sm-6:first-child {
    padding-right: 15px;
    padding: 50px 15px;
    padding-top: 35px;
    background: #D4CEC0;
  }
}
.multiBannerText .col-sm-6:last-child {
  padding-left: 50px;
}
@media (max-width: 767px) {
  .multiBannerText .col-sm-6:last-child {
    padding-right: 15px;
    padding: 50px 15px;
    padding-top: 35px;
    background: #E6E2D9;
  }
}
.multiBannerText h1 {
  font-family: 'Playfair Display', serif;
  color: #333;
  font-size: 44px;
  line-height: 54px;
  margin-top: 0px;
}
@media (max-width: 767px) {
  .multiBannerText h1 {
    font-size: 28px;
    line-height: 38px;
  }
}
@media (min-width: 768px) {
  .multiBannerText h1 {
    color: white;
  }
}
.multiBannerText p {
  margin-bottom: 50px;
}
@media (min-width: 768px) {
  .multiBannerText p {
    color: white;
  }
}
@media (min-width: 768px) {
  .multiBannerText .line {
    background: white;
  }
}
.multiBannerText .cta {
  background: #b9b28c;
  color: white;
  border-color: #b9b28c !important;
}
.multiBannerText .cta:hover,
.multiBannerText .cta:focus,
.multiBannerText .cta:active {
  background: #b9b28c !important;
  border-color: #b9b28c !important;
  color: white !important;
}

我想在名称附近做一个选定的案例。

3 个答案:

答案 0 :(得分:0)

你可以浏览android开发者块。如需参考,您可以使用this link

它将为您提供有关android中联系人的完整详细信息。

答案 1 :(得分:0)

您可以按照此代码作为参考

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_multiple_choice,listContent);         myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    myList.setAdapter(adapter);
    getChoice.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
            String selected = "";
            int cntChoice = myList.getCount();

            SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
            for(int i = 0; i < cntChoice; i++){ 
                if(sparseBooleanArray.get(i)) { 
                    selected += myList.getItemAtPosition(i).toString() + "\n";
                }
            }

            Toast.makeText(MainActivity.this, selected, Toast.LENGTH_LONG).show();
        }});

答案 2 :(得分:0)

当我点击按钮getChoice时,我想只接受numerotel 但现在需要名字和numerotel

像这样:

检索名称+号码的联系方式:

    private List<String> retrieveContacts(ContentResolver contentResolver)
{
    final Set<String> contacts = new HashSet<String>();
    final Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);

    if (cursor == null)
    {
        Log.e("retrieveContacts", "Cannot retrieve the contacts");
        return null;
    }

    if (cursor.moveToFirst() == true)
    {
        do
        {
            final long id = Long.parseLong(cursor.getString(cursor.getColumnIndex(ContactsContract.Data._ID)));
            final String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            final String numerotel = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            final int hasPhoneNumber = cursor.getInt(cursor.getColumnIndex(ContactsContract.Data.HAS_PHONE_NUMBER));

            if (hasPhoneNumber > 0)
            {
                contacts.add(name + " " + numerotel);
            }
        }
        while (cursor.moveToNext() == true);
    }

    if (cursor.isClosed() == false)
    {
        cursor.close();
    }

    final List<String> sortedContacts = new ArrayList<String>(contacts);
    Collections.sort(sortedContacts);

    return sortedContacts;
}

OnCreateView:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{ View rootView = inflater.inflate(R.layout.fragment_gestion_contact, container, false);

            final ListView list = (ListView) rootView.findViewById(android.R.id.list);
            final List<String> contacts = retrieveContacts(getActivity().getContentResolver());


            if (contacts != null) {
                list.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_multiple_choice, contacts));
                list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                getChoice = (Button) rootView.findViewById(R.id.getchoice);
                getChoice.setOnClickListener(new Button.OnClickListener(){



                    @Override

                    public void onClick(View v) {

                        // TODO Auto-generated method stub



                        String selected = "";



                        int cntChoice = list.getCount();

                        SparseBooleanArray sparseBooleanArray = list.getCheckedItemPositions();

                        for(int i = 0; i < cntChoice; i++){

                            if(sparseBooleanArray.get(i)) {

                                selected += list.getItemAtPosition(i).toString() + "\n";



                            }

                        }



                        Log.d("CONFIRMATION", selected);

                    }});
            }
            return rootView;
        }

我只想选择数字+ = list.getItemAtPosition(i).toString()+“\ n”;