2017-01-15 3 views
1

を選択することで、すべてのチェックボックスを選択するには、私は、すべての連絡先]チェックボックスを選択する必要があり、これを選択することで、私はすべての選択した電話リストの連絡先の下に取得したい、私はそれをどのように行うことができますか?/どのように1

誰が助けてくださいすることができます

私は、単一の接触のためのコードの下に働いている

01を作業単一選択単一チェックボックスの

enter image description here コードの下にアプリのビューを参照してください、私はすべての 喜ばを選択したいを選択

あなたはArrayList<CheckBox> array_checkBoxを作成する必要があり、常にCheckBoxが作成されていることを、あなたはarray_checkBoxでこのCheckBoxを追加する必要がメインコード

public class TabFragment1 extends Fragment implements CallBack { 

Context context = null; 
ContactsAdapter objAdapter; 
private ArrayList<ContactObject> contactList = new ArrayList<ContactObject>(); 
private ListView listView; 
private ContactsAdapter adapter; 
private Button doneBtn , sendBtn; 
ProgressDialog pDialog; 
TextView buttonAdd; 
LinearLayout container; 
HorizontalScrollView mHorizontalScrollView; 
EditText textIn; 
ContactObject mContactObject; 
CallBack mCallBack; 
ToggleButton toggleButton; 
CheckBox select_all; 
private ArrayAdapter arrayAdapter; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
//  return super.onCreateView(inflater, container, savedInstanceState); 
    return inflater.inflate(R.layout.activity_main, container, false); 


} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    context = getActivity(); 
    mCallBack = this; 
    pDialog = new ProgressDialog(context); 
    pDialog.setMessage("Loading..."); 
    pDialog.show(); 
    listView =(ListView) view.findViewById(R.id.list); 
//  totalSmsCount = (TextView) view.findViewById(R.id.TotalMsgSent); 
    sendBtn = (Button) view.findViewById(R.id.submitBtn); 
//  totalSmsCount.setText("Total message sent 100/1000"); 
    mHorizontalScrollView = (HorizontalScrollView)view.findViewById(R.id.container); 
    container = (LinearLayout)view.findViewById(R.id.linearcontainer); 
    textIn = (EditText)view.findViewById(R.id.textin); 
    buttonAdd = (Button)view.findViewById(R.id.add); 
    select_all = (CheckBox) view.findViewById(R.id.chkall); 
    // toggleButton=(ToggleButton)view.findViewById(R.id.toggleButton); 


    addContactsInList(); 

    buttonAdd.setOnClickListener(new View.OnClickListener(){ 

     @Override 
     public void onClick(View arg0) { 

      String msg1 = textIn.getText().toString().trim(); 
      if (!msg1.equals("") && msg1.length() == 10) { 

       LayoutInflater layoutInflater = 
         (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       final View addView = layoutInflater.inflate(R.layout.add_contact, null); 
       TextView textOut = (TextView)addView.findViewById(R.id.textout); 
       textOut.setText(textIn.getText().toString().trim()); 

       Button buttonRemove = (Button)addView.findViewById(R.id.remove); 
       buttonRemove.setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         ((LinearLayout) addView.getParent()).removeView(addView); 
         TextView textOut = (TextView)addView.findViewById(R.id.textout); 


         for (ContactObject bean : ContactsListClass.manualList) { 
          Log.e("remove","bean.getNumber()="+bean.getNumber()); 
          Log.e("remove","TextView.getNumber()="+textOut.getText().toString()); 

          String str1= bean.getNumber(); 
          String str2= textOut.getText().toString(); 
          if(str1.equals(str2)) 
          { 
           Log.e("remove","Before="+ContactsListClass.manualList.size()); 
           Log.e("remove", bean.getNumber()); 
           ContactsListClass.manualList.remove(bean); 
           Log.e("remove", "After=" + ContactsListClass.manualList.size()); 
           break; 
          } 


         } 

        } 
       }); 

       container.addView(addView); 

       mContactObject = new ContactObject(); 
       mContactObject.setNumber(textIn.getText().toString().trim()); 
       mContactObject.setName(""); 
       ContactsListClass.manualList.add(mContactObject); 
       textIn.setText(""); 
       mHorizontalScrollView.postDelayed(new Runnable() { 
        public void run() { 
         mHorizontalScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT); 
        } 
       }, 100L); 

      } else { 

       new AlertDialog.Builder(context) 
         .setTitle("Alert") 
         .setMessage("Enter valid number.") 
         .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           // continue with delete 

          } 
         }) 

         .setIcon(android.R.drawable.ic_dialog_alert) 
         .show(); 
      } 

     }}); 
    sendBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Log.i("Selected contact","size="+ ContactsListClass.phoneList.size()); 

       StringBuffer sb = new StringBuffer(); 

       for (ContactObject bean : ContactsListClass.phoneList) { 

        if (bean.isSelected()) { 
         sb.append(bean.getName()); 
         sb.append(","); 

         Log.i("Selected contact", sb.toString()); 
         contactList.add(bean); 


        } 
       } 
      for (ContactObject bean : ContactsListClass.manualList) { 


        contactList.add(bean); 

      } 
      if(contactList.size() > 0 ) { 
       container.removeAllViews(); 
       Intent i = new Intent(context, WriteMessage.class); 
//    i.putExtra("Contact_list", contactList); 
       context.startActivity(i); 
      }else{ 

       new AlertDialog.Builder(context) 
         .setTitle("No Contact") 
         .setMessage("Please select atleast one contact.") 
         .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           // continue with delete 
          } 
         }) 
         .setIcon(android.R.drawable.ic_dialog_alert) 
         .show(); 
      } 

     } 
    }); 

} 

@Override 
public void onResume() { 
    super.onResume(); 
    contactList.clear(); 
    if(objAdapter!= null) { 
     objAdapter.notifyDataSetChanged(); 
    } 
    for (ContactObject bean : ContactsListClass.manualList) { 

     LayoutInflater layoutInflater = 
       (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     final View addView = layoutInflater.inflate(R.layout.add_contact, null); 
     TextView textOut = (TextView)addView.findViewById(R.id.textout); 
     textOut.setText(bean.getNumber()); 

     Button buttonRemove = (Button)addView.findViewById(R.id.remove); 
     buttonRemove.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       ((LinearLayout) addView.getParent()).removeView(addView); 
       TextView textOut = (TextView) addView.findViewById(R.id.textout); 


       for (ContactObject bean : ContactsListClass.manualList) { 
        Log.e("remove", "bean.getNumber()=" + bean.getNumber()); 
        Log.e("remove", "TextView.getNumber()=" + textOut.getText().toString()); 

        String str1 = bean.getNumber(); 
        String str2 = textOut.getText().toString(); 
        if (str1.equals(str2)) { 
         Log.e("remove", "Before=" + ContactsListClass.manualList.size()); 
         Log.e("remove", bean.getNumber()); 
         ContactsListClass.manualList.remove(bean); 
         Log.e("remove", "After=" + ContactsListClass.manualList.size()); 
         break; 
        } 


       } 

      } 
     }); 

     container.addView(addView); 




    } 
    mHorizontalScrollView.postDelayed(new Runnable() { 
     public void run() { 
      mHorizontalScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT); 
     } 
    }, 100L); 
} 



private void getSelectedContacts() { 
    // TODO Auto-generated method stub 

    StringBuffer sb = new StringBuffer(); 

    for (ContactObject bean : ContactsListClass.phoneList) { 

     if (bean.isSelected()) { 
      sb.append(bean.getName()); 
      sb.append(","); 
     } 
    } 

    String s = sb.toString().trim(); 

    if (TextUtils.isEmpty(s)) { 
     Toast.makeText(context, "Select atleast one Contact", 
       Toast.LENGTH_SHORT).show(); 
    } else { 

     s = s.substring(0, s.length() - 1); 
     Toast.makeText(context, "Selected Contacts : " + s, 
       Toast.LENGTH_SHORT).show(); 

    } 

} 

private void addContactsInList() { 
    // TODO Auto-generated method stub 

    Thread thread = new Thread() { 
     @Override 
     public void run() { 

      showPB(); 

      try { 
//     ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.READ_CONTACTS},1); 
       Cursor phones = getActivity().getContentResolver().query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, null, null, null); 

       try { 
        ContactsListClass.phoneList.clear(); 
       } catch (Exception e) { 

       } 

       while (phones.moveToNext()) { 
        String phoneName = phones 
          .getString(phones 
              .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
        String phoneNumber = phones 
          .getString(phones 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); 

        String phoneImage = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.PHOTO_URI)); 


        ContactObject cp = new ContactObject(); 



        cp.setName(phoneName); 
        cp.setNumber(phoneNumber); 
        cp.setImage(phoneImage); 

        ContactsListClass.phoneList.add(cp); 

       } 
       phones.close(); 



       getActivity().runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         // TODO Auto-generated method stub 
//       llContainer.addView(lv); 
         objAdapter = new ContactsAdapter(getActivity(), 
           ContactsListClass.phoneList,mCallBack); 
         listView.setAdapter(objAdapter); 
         objAdapter.notifyDataSetChanged(); 
         pDialog.hide(); 
        } 
       }); 

       Collections.sort(ContactsListClass.phoneList, 
         new Comparator<ContactObject>() { 
          @Override 
          public int compare(ContactObject lhs, 
               ContactObject rhs) { 
           return lhs.getName().compareTo(
             rhs.getName()); 
          } 
         }); 



       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

        @Override 
        public void onItemClick(AdapterView<?> parent, 
              View view, int position, long id) { 

         CheckBox chk = (CheckBox) view 
           .findViewById(R.id.contactcheck); 
         ContactObject bean = ContactsListClass.phoneList 
           .get(position); 
         if (bean.isSelected()) { 
          bean.setSelected(false); 
          chk.setChecked(false); 

         } else { 
          bean.setSelected(true); 
          chk.setChecked(true); 


         } 

        } 
       }); 


      } catch (Exception e) { 

       e.printStackTrace(); 

      } 

      hidePB(); 

     } 
    }; 
    thread.start(); 

} 

void showPB() { 
    getActivity().runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
     } 
    }); 
} 

void hidePB() { 
    getActivity().runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
     } 
    }); 
} 
public void onEventCheck(ContactObject mContactObject) 
{ 
    setContact(mContactObject); 
} 
void setContact(ContactObject mContactObject) 
{ 
    Log.e("remove","inside setContact"); 
    LayoutInflater layoutInflater = 
      (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final View addView = layoutInflater.inflate(R.layout.add_contact, null); 
    TextView textOut = (TextView)addView.findViewById(R.id.textout); 
    textOut.setText(mContactObject.getNumber()); 

    Button buttonRemove = (Button)addView.findViewById(R.id.remove); 
    buttonRemove.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      ((LinearLayout) addView.getParent()).removeView(addView); 
      TextView textOut = (TextView)addView.findViewById(R.id.textout); 


      for (ContactObject bean : ContactsListClass.manualList) { 
       Log.e("remove","bean.getNumber()="+bean.getNumber()); 
       Log.e("remove","TextView.getNumber()="+textOut.getText().toString()); 

       String str1= bean.getNumber(); 
       String str2= textOut.getText().toString(); 
       if(str1.equals(str2)) 
       { 
        Log.e("remove","Before="+ContactsListClass.manualList.size()); 
        Log.e("remove", bean.getNumber()); 
        ContactsListClass.manualList.remove(bean); 
        Log.e("remove", "After=" + ContactsListClass.manualList.size()); 
        break; 
       } 


      } 

     } 
    }); 

    container.addView(addView); 
    mHorizontalScrollView.postDelayed(new Runnable() { 
     public void run() { 
      mHorizontalScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT); 
     } 
    }, 100L); 

} 

} 
+0

http://stackoverflow.com/questions/24690605/selecting-all-items-in-a-listview-on-checkbox-select –

答えて

0

。例については

public class MainActivity extends AppCompatActivity { 

private CheckBox main_Checkbox; 
private CheckBox checkBox1; 
private CheckBox checkBox2; 
private CheckBox checkBox3; 
private ArrayList<CheckBox> array_checkBox = new ArrayList<>(); 

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

    main_Checkbox = (CheckBox) findViewById(R.id.mainCheckBox); 
    checkBox1 = (CheckBox) findViewById(R.id.checkBox1); 
    checkBox2 = (CheckBox) findViewById(R.id.checkBox2); 
    checkBox3 = (CheckBox) findViewById(R.id.checkBox3); 

    //Add a checkBox in the array (do it for each new CheckBox) 
    array_checkBox.add(checkBox1); 
    array_checkBox.add(checkBox2); 
    array_checkBox.add(checkBox3); 

    main_Checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      //if the Main CheckBox is checked, check all CheckBoxs 
      if (isChecked){ 

       for (int j = 0; j < array_checkBox.size(); j++){ 
        array_checkBox.get(j).setChecked(true); 
       } 
       //Else, uncheck all CheckBoxs 
      }else{ 

       for (int j = 0; j < array_checkBox.size(); j++){ 
        array_checkBox.get(j).setChecked(false); 
       } 
      } 
     } 
    }); 

} 
関連する問題