Android Open Source - Pairs Choose Contact Activity






From Project

Back to project page Pairs.

License

The source code is released under:

MIT License

If you think the Android project Pairs listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.pairs;
/* w w  w . j  av  a  2 s .c  o m*/
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class ChooseContactActivity extends Activity implements OnClickListener
{
  private List<CheckBox> checkboxs = new ArrayList<CheckBox>();
  ArrayList<String> names = new ArrayList<String>();
  ArrayList<String> numbers = new ArrayList<String>();
  SharedPreferences sharedPreferences;
  SharedPreferences.Editor editor;

  //????????????????????????????????????????????????
  @Override
  public void onClick(View v)
  {
    for (int i = 0; i < names.size(); i++)
      editor.putBoolean(names.get(i).toString(), checkboxs.get(i).isChecked());
    editor.commit();
    finish();
  }
  
  //?????????????????????????????????????????????????????
  @Override
  public void onStop()
  {
    for (int i = 0; i < names.size(); i++)
      editor.putBoolean(names.get(i).toString(), checkboxs.get(i).isChecked());
    editor.commit();
    super.onStop();
  }

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    //???SharedPreferences?????????
    sharedPreferences = getSharedPreferences("pairsContact", Activity.MODE_PRIVATE);
    editor = sharedPreferences.edit();
    //????????
    names.clear();
    
    //???Cursor????????????????????????????????????names???????????
    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    while (cursor.moveToNext())
      names.add(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
    cursor.close();
    
    //???????????????????????names??????????????????A-Z????
    Comparator cmp = new ChineseCharComp();
    Collections.sort(names,cmp);
    
    super.onCreate(savedInstanceState);
    
    //????xml????????????????
    LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.choose_contact, null);
    for (int i = 0; i < names.size(); i++)
    {
      LinearLayout checkboxLinearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.contactline, null);
      //????Android????CheckBox??????????????????
      CheckBox s = (CheckBox) checkboxLinearLayout.findViewById(R.id.showcheck);

      //????????????????????????????????????????????????????????????????????????????????????
      s.setChecked(sharedPreferences.getBoolean(names.get(i), false));
      checkboxs.add(s);  
      
      checkboxs.get(i).setText(names.get(i));
      
      linearLayout.addView(checkboxLinearLayout, i);
    }
    
    //??LinearLayout???????????ScrollView????????????????LinearLayout???????????????????????????????????????????
    ScrollView s = new ScrollView(this);
    s.addView(linearLayout);
    setContentView(s);
    
    //??save???
    Button save = (Button) findViewById(R.id.save);
    save.setOnClickListener(this);
  }
}

//??????????????????????????ChineseCharComp?????????????????????????????
class ChineseCharComp implements Comparator
{
  public int compare(Object o1, Object o2)
  {
    Collator myCollator = Collator.getInstance(java.util.Locale.CHINA);
    if (myCollator.compare(o1, o2) < 0)
      return -1;
    else if (myCollator.compare(o1, o2) > 0)
      return 1;
    else
      return 0;
  }
}




Java Source Code List

com.example.pairs.AlarmActivity.java
com.example.pairs.ChooseContactActivity.java
com.example.pairs.Function.java
com.example.pairs.HelpActivity.java
com.example.pairs.Help.java
com.example.pairs.MainActivity.java
com.example.pairs.OurPage.java
com.example.pairs.Ours.java
com.example.pairs.PhotoActivity.java
com.example.pairs.SMSReceiver.java
com.example.pairs.SetContactActivity.java