2016-04-22 14 views
0

Ich habe eine Kontaktlistenaktivität, möchte jedoch einige Funktionen hinzufügen, sodass beim Klicken auf einen Kontakt die Telefonnummer des Anrufers angezeigt wird. Ich habe diesen Code-Schnipsel versucht, aber es passiert nichts, wenn ich auf einen Kontakt tippen:Rufnummer beim Klicken auf Listenansicht Element

view.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent callIntent = new Intent(Intent.ACTION_CALL); 
       callIntent.setData(Uri.parse("tel:" + currentContact.getPhone())); 
       if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { 
        return; 
       } 
       try{ 
        ((Activity) context).startActivity(callIntent); 
       }catch (Exception e){ 
        e.printStackTrace(); 
       } 

Alle Kontaktaktivitätscode:

public class ContactList extends ActionBarActivity { 

Spinner spinner, spinnerFilter; 
EditText nameTxt, phoneTxt, emailTxt, addressTxt; 
ImageView contactImageimgView, btnSort; 
List<Contact> Contacts = new ArrayList<Contact>(); 
ListView contactListView; 
//Uri imageUri = null; 
AlertDialog alertDialog; 

Bitmap photoTaken; 
String userId; 

boolean isReverseEnabledPriority = false, isReverseEnabledName = false; 
//ArrayList<Contact> contacts; 


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

    SQLiteHandler db = new SQLiteHandler(getApplicationContext()); 
    HashMap<String, String> user = db.getUserDetails(); 
    userId = user.get("uid"); 

    nameTxt = (EditText) findViewById(R.id.txtName); 
    phoneTxt = (EditText) findViewById(R.id.txtPhone); 
    emailTxt = (EditText) findViewById(R.id.txtEmail); 
    addressTxt = (EditText) findViewById(R.id.txtAddress); 
    contactListView = (ListView) findViewById(R.id.listView); 
    contactImageimgView = (ImageView) findViewById(R.id.imgViewContactImage); 
    spinner = (Spinner) findViewById(R.id.spinner); 
    spinnerFilter = (Spinner) findViewById(R.id.spinnerFilter); 
    btnSort = (ImageView) findViewById(R.id.btnSort); 

    TabHost tabHost = (TabHost) findViewById(R.id.tabHost); 

    tabHost.setup(); 

    TabHost.TabSpec tabSpec = tabHost.newTabSpec("Creator"); 
    tabSpec.setContent(R.id.tabCreator); 
    tabSpec.setIndicator("add contact"); 
    tabHost.addTab(tabSpec); 

    tabSpec = tabHost.newTabSpec("List"); 
    tabSpec.setContent(R.id.tabContactList); 
    tabSpec.setIndicator("contact list"); 
    tabHost.addTab(tabSpec); 

    getContacts(ContactList.this); 

    btnSort.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String filter = spinnerFilter.getSelectedItem().toString(); 

      if(filter.equals("Priority")){ 

       Collections.sort(Contacts, new Comparator<Contact>() { 
        public int compare(Contact v1, Contact v2) { 
         return v1.getPriorityVal().compareTo(v2.getPriorityVal()); 
        } 
       }); 
       if(isReverseEnabledPriority) 
        Collections.reverse(Contacts); 
       isReverseEnabledPriority = !isReverseEnabledPriority; 
       populateList(); 

      }else if(filter.equals("Name")){ 

       Collections.sort(Contacts, new Comparator<Contact>() { 
        public int compare(Contact v1, Contact v2) { 
         return v1.getName().compareTo(v2.getName()); 
        } 
       }); 
       if(isReverseEnabledName) 
        Collections.reverse(Contacts); 
       isReverseEnabledName = !isReverseEnabledName; 
       populateList(); 

      }else{ 
       Toast.makeText(ContactList.this, "Please selectr a method to sort..!", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 


    final Button addBtn = (Button) findViewById(R.id.btnAdd); 
    addBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      Contact contact = new Contact(photoTaken, nameTxt.getText().toString(), phoneTxt.getText().toString(), emailTxt.getText().toString(), addressTxt.getText().toString(), spinner.getSelectedItem().toString(), userId); 
      Contacts.add(contact); 
      saveContact(contact, ContactList.this); 
     } 
    }); 


    nameTxt.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence charSequence, int start, int before, int count) { 
      addBtn.setEnabled(!nameTxt.getText().toString().trim().isEmpty()); 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }); 

    contactImageimgView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(Intent.createChooser(intent, "Select Contact Image"), 1); 

     } 
    }); 
} 

void getContacts(final Context context){ 
    new Thread(){ 
     private Handler handler = new Handler(); 
     private ProgressDialog progressDialog; 

     @Override 
     public void run(){ 
      handler.post(new Runnable() { 
       @Override 
       public void run() { 
        progressDialog = ProgressDialog.show(context, null, "Please wait..", false); 
       } 
      }); 

      try { 
       Contacts = ContactsController.getAllContactsOfUser(userId); 

       populateList(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      handler.post(new Runnable() { 
       @Override 
       public void run() { 
        if (progressDialog != null && progressDialog.isShowing()) { 
         progressDialog.dismiss(); 
        } 
       } 
      }); 

     } 
    }.start(); 
} 

void saveContact(final Contact contact, final Context context){ 
    new Thread(){ 
     private Handler handler = new Handler(); 
     private ProgressDialog progressDialog; 
     boolean result = false; 
     int id; 

     @Override 
     public void run(){ 
      handler.post(new Runnable() { 
       @Override 
       public void run() { 
        progressDialog = ProgressDialog.show(context, null, "Saving Contact..", false); 
       } 
      }); 

      try { 
       id = ContactsController.saveContact(contact); 
       if(id > 0){ 
        contact.setId(id); 
        result = ContactsController.uploadImage(contact); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      handler.post(new Runnable() { 
       @Override 
       public void run() { 
        if (progressDialog != null && progressDialog.isShowing()) { 
         progressDialog.dismiss(); 
        } 

        if(id == 0){ 
         Toast.makeText(context, "Contact is not saved..!", Toast.LENGTH_SHORT).show(); 
        } 

        if(!result){ 
         Toast.makeText(context, "Image upload failed..!", Toast.LENGTH_SHORT).show(); 
        }else{ 
         populateList(); 
         Toast.makeText(getApplicationContext(), nameTxt.getText().toString() + " has been added to your Contacts!", Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }); 

     } 
    }.start(); 
} 

public void onActivityResult(int reqCode, int resCode, Intent data) { 
    if (resCode == RESULT_OK) { 
     if (reqCode == 1) { 
      Uri imageUri = data.getData(); 
      try { 
       photoTaken = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      contactImageimgView.setImageURI(data.getData()); 
     } 
    } 
} 

private void populateList() { 
    ArrayAdapter<Contact> adapter = new ContactListAdapter(); 
    contactListView.setAdapter(adapter); 
} 

private class ContactListAdapter extends ArrayAdapter<Contact> { 
    public ContactListAdapter() { 
     super(ContactList.this, R.layout.listview_item, Contacts); 
    } 


    @Override 
    public View getView(int position, View view, ViewGroup parent) { 
     if (view == null) 
      view = getLayoutInflater().inflate(R.layout.listview_item, parent, false); 

     final Contact currentContact = Contacts.get(position); 

     try { 
      TextView name = (TextView) view.findViewById(R.id.contactName); 
      name.setText(currentContact.getName()); 
      TextView phone = (TextView) view.findViewById(R.id.phoneNumber); 
      phone.setText(currentContact.getPhone()); 
      TextView email = (TextView) view.findViewById(R.id.emailAddress); 
      email.setText(currentContact.getEmail()); 
      TextView address = (TextView) view.findViewById(R.id.cAddress); 
      address.setText(currentContact.getAddress()); 
      TextView cPriority = (TextView) view.findViewById(R.id.cPriority); 
      cPriority.setText(currentContact.getPriority()); 
      ImageView ivContactImage = (ImageView) view.findViewById(R.id.ivContactImage); 
      if(currentContact.getImage() != null) 
       ivContactImage.setImageBitmap(currentContact.getImage()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     ImageView btnDeleteContact = (ImageView) view.findViewById(R.id.btnDeleteContact); 
     btnDeleteContact.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       final AlertDialog.Builder builder = new AlertDialog.Builder(ContactList.this); 
       builder.setTitle("Confirm Delete"); 
       builder.setMessage("Are you sure?"); 
       builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         deleteContact(currentContact, ContactList.this); 
         alertDialog.dismiss(); 
        } 
       }); 
       builder.setNegativeButton("No", null); 
       alertDialog = builder.create(); 
       alertDialog.show(); 
      } 
     }); 

     return view; 
    } 

    @Override 
    public int getCount() { 
     return Contacts.size(); 
    } 

    void deleteContact(final Contact contact, final Context context){ 
     new Thread(){ 
      Handler handler = new Handler(); 
      ProgressDialog progressDialog; 
      Boolean result = false; 

      @Override 
      public void run(){ 
       handler.post(new Runnable() { 
        @Override 
        public void run() { 
         progressDialog = ProgressDialog.show(context, null, "Deleting Contact..", false); 
        } 
       }); 

       try { 
        result = ContactsController.deleteContact(contact); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

       handler.post(new Runnable() { 
        @Override 
        public void run() { 
         if (progressDialog != null && progressDialog.isShowing()) { 
          progressDialog.dismiss(); 
         } 

         if(!result){ 
          Toast.makeText(context, "Contact delete failed..!", Toast.LENGTH_SHORT).show(); 
         }else{ 
          Contacts.remove(contact); 
          populateList(); 
          Toast.makeText(getApplicationContext(), nameTxt.getText().toString() + " has been deleted from your Contacts!", Toast.LENGTH_SHORT).show(); 
         } 
        } 
       }); 
      } 
     }.start(); 
    } 
} 
} 
+0

Mögliche Duplikat [Android programmatisch eine Telefonnummer wählen] (http://stackoverflow.com/questions/22372561/android-dial-a-phone-number-programmatically) – NoChinDeluxe

+0

@NoChinDeluxe, das nicht funktioniert – Jay

Antwort

0

Haben Sie diesen Code versuchen ??

Uri number = Uri.parse("tel:123456789"); 
Intent callIntent = new Intent(Intent.ACTION_DIAL, number); 
startActivity(callIntent); 
+0

Jeder Kontakt hat eine andere Telefonnummer, daher kann ich seine Telefonnummer nicht manuell eingeben – Jay

Verwandte Themen