0

After adding the removal on long click part, i tried to add a call option the same way, but it didn't work.

I wrote the code here in the second box (first box is without the call option changes that didn't work) and highlighted the changes I tried with // signs.

This is the code: (i will add my attempts of creating the long click removal if you want)

    public static final int PICK_CONTACT = 1;
private ArrayList<String> contactList;
 private ArrayAdapter<String> arrayAdapter;

  @Override
  public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_work__contacts);

Button btnPickContact = (Button) findViewById(R.id.btnPickContact);
btnPickContact.setOnClickListener(new View.OnClickListener() {
    public void onClick(View _view) {
        Intent intent = new Intent(Intent.ACTION_PICK,
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
        startActivityForResult(intent, PICK_CONTACT);
    }
});

//you may fill it here e.g. from your db
contactList=new ArrayList<String>();

arrayAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, contactList);

final ListView lv = (ListView) findViewById(R.id.ContactListView);
lv.setAdapter(arrayAdapter);
  lv.setLongClickable(true);
        lv.setClickable(true);

lv.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int pos, long id) {
            arrayAdapter.notifyDataSetChanged();

            arrayAdapter.remove(arrayAdapter.getItem(pos));



}



   @SuppressWarnings("deprecation")
   @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {
case (PICK_CONTACT): {
    if (resultCode == Activity.RESULT_OK) {
        Uri contentUri = data.getData();
        //Phone Name
        Cursor c = managedQuery(contentUri, null, null, null, null);
        c.moveToFirst();
        String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
        //Phone Number
        String contactId = contentUri.getLastPathSegment();
        Cursor cursor = getContentResolver().query(Phone.CONTENT_URI,
                null, Phone._ID + "=?", new String[] { contactId },
                null);// < - Note, not CONTACT_ID!
        startManagingCursor(cursor);
        Boolean numbersExist = cursor.moveToFirst();
        int phoneNumberColumnIndex = cursor
                .getColumnIndex(Phone.NUMBER);
        String phoneNumber = "";
        while (numbersExist) {
            phoneNumber = cursor.getString(phoneNumberColumnIndex);
            phoneNumber = phoneNumber.trim();
            numbersExist = cursor.moveToNext();
        }
        stopManagingCursor(cursor);
        //Set
        arrayAdapter.add(name + "-" + phoneNumber);
        arrayAdapter.notifyDataSetChanged();
    }
    break;
}
}

} }

public static final int PICK_CONTACT = 1;
private ArrayList<String> contactList;
private ArrayAdapter<String> arrayAdapter;
   // private ArrayList<Integer> contactList2;
   // private ArrayAdapter<Integer> arrayAdapter2;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_work__contacts);

    Button btnPickContact = (Button) findViewById(R.id.btnPickContact);
    btnPickContact.setOnClickListener(new View.OnClickListener() {
        public void onClick(View _view) {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(intent, PICK_CONTACT);
        }
    });

    //you may fill it here e.g. from your db
    contactList=new ArrayList<String>();

  //  contactList2=new ArrayList<Integer>();

    arrayAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, contactList);

 //   arrayAdapter2 = new ArrayAdapter<Integer>(this,
//            android.R.layout.simple_list_item_1, contactList2);



    final ListView lv = (ListView) findViewById(R.id.ContactListView);
    lv.setAdapter(arrayAdapter);
    lv.setLongClickable(true);
    lv.setClickable(true);

   // final ListView lv2 = (ListView) findViewById(R.id.ContactListView);
  //  lv.setAdapter(arrayAdapter2);
   // lv.setLongClickable(true);
  //  lv.setClickable(true);

    lv.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int pos, long id) {
            arrayAdapter.notifyDataSetChanged();

            arrayAdapter.remove(arrayAdapter.getItem(pos));



            Log.v("long clicked","pos: " + pos);

            return true;
        }
    });



   // lv2.setOnItemClickListener(new OnItemClickListener() {
    //  public void onItemClick(AdapterView<?> parent, View view,
        //      int position, long id) {
          //  
            //String url = "tel:"+position;
           // Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
        //  startActivity(intent);
    //      
        //}
    //});



}



@SuppressWarnings("deprecation")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case (PICK_CONTACT): {
        if (resultCode == Activity.RESULT_OK) {
            Uri contentUri = data.getData();
            //Phone Name
            Cursor c = managedQuery(contentUri, null, null, null, null);
            c.moveToFirst();
            String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
            //Phone Number
            String contactId = contentUri.getLastPathSegment();
            Cursor cursor = getContentResolver().query(Phone.CONTENT_URI,
                    null, Phone._ID + "=?", new String[] { contactId },
                    null);// < - Note, not CONTACT_ID!
            startManagingCursor(cursor);
            Boolean numbersExist = cursor.moveToFirst();
            int phoneNumberColumnIndex = cursor
                    .getColumnIndex(Phone.NUMBER);
            String phoneNumber = "";
           // Integer iInteger = new Integer(phoneNumberColumnIndex);
            while (numbersExist) {
                phoneNumber = cursor.getString(phoneNumberColumnIndex);
                phoneNumber = phoneNumber.trim();
                numbersExist = cursor.moveToNext();

            }
            stopManagingCursor(cursor);
            //Set
            arrayAdapter.add(name + " - " + phoneNumber);
            arrayAdapter.notifyDataSetChanged();
           // arrayAdapter2.add(iInteger);
          //  arrayAdapter2.notifyDataSetChanged();



        }
        break;
    }

    }



}

}

Anybody has an idea why it didn't work ?

Thank you

Noam
  • 1
  • 2
  • Check this post: http://stackoverflow.com/questions/8846707/how-to-implement-a-long-click-listener-on-a-listview . Inside your onLongClickListener you can create a dialog with delete button on it, and since you already know the position you can remove it from your arraylist, then call notifyDataChange on your adapter. – Can Gokdere Jan 21 '14 at 01:19
  • Thank you it is working. I also need a method to call a number that is clicked from the listview. Can I do this with the same method ? Or I will need to create another activity ? – Noam Jan 21 '14 at 13:14
  • Sure you can, depending on long click or normal click, you can also set listView.onItemClickListener() then call the number based on position – Can Gokdere Jan 21 '14 at 14:38
  • I tried to do it with listView.onItemClickListener() but I think it didnt work because the items in the list are name + numbers (contacts names + their numbers) and not only numbers you can call. Can you please try to solve this ? – Noam Jan 21 '14 at 19:09
  • What I would do is to create a secondary arraylist which includes only numbers for respective positions on list. Then onItemClickListener() you can get corresponding number from secondaryArrayList.get(position) then call it – Can Gokdere Jan 21 '14 at 21:39
  • I gave it a try but it didn't work for me. I edited the main post with the new information and code, I hope you can help me please. Thank you – Noam Jan 22 '14 at 00:41
  • Thank you for your help, I manged to create the on click call function. The last thing I need to do in this activity is to find a way to save the data like Shared Preferences. What is the best way to save an arrayadapter listview ? – Noam Jan 23 '14 at 19:10

1 Answers1

0

I think the best way is to implement a long click listener as you already did.

After that, I suggest you to show a message dialog for user to confirm. I share you a method to do this :

private AlertDialog showCustomDialog(final Activity act, CharSequence title, CharSequence message, CharSequence buttonYes, CharSequence buttonNo, final int selectedIndex) {
    AlertDialog.Builder customDialog = new AlertDialog.Builder(act);
    customDialog.setTitle(title);
    customDialog.setMessage(message);
    customDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            picURL.remove(selectedIndex);
        }
    });
    customDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
        }
    });
    return customDialog.show();
}

Last thing : I see you're removing from the adapter, but not in the list. Maybe this is what causing your error ?

Community
  • 1
  • 1
Galabyca
  • 153
  • 1
  • 14