0

Here control is not entering in ContextMenu and not print toast. i don't know why? and what i am missing here, simply i want to show context menu on long press, i tries it many days but still going to fail to solve it. i tried many method from yesterday but still contextmenu not working please help me what i am missing here.

public class MainActivity extends AppCompatActivity {

ListView listView;
Button sync;
ProgressDialog progressDialog;
String name, phone;

ArrayList<Contact_list> listitem;


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



    listitem = new ArrayList<Contact_list>();

    listView = (ListView) findViewById(R.id.listViewID);
    registerForContextMenu(listView);

    sync= (Button) findViewById(R.id.syncID);
    sync.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // GET CONTACTS DATA


            GetContactsIntoArrayList();


            listView.setAdapter(new Custom_adapter(MainActivity.this, listitem));

            Toast.makeText(MainActivity.this, "import", Toast.LENGTH_SHORT).show();
        }
    });
}

public void GetContactsIntoArrayList(){
    Cursor cursor;
    cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);

    while (cursor.moveToNext()) {

        name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

        phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));


        listitem.add(new Contact_list(name,phone));



        listView.setAdapter(new Custom_adapter(MainActivity.this, listitem));

    }

    cursor.close();

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View v,
                                       int index, long arg3) {
            // TODO Auto-generated method stub

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


            //Log.d("tag", "message");
            String str=listView.getItemAtPosition(index).toString();

            Log.d("long click sucessfull: " ,str);
            return true;
        }
    });

}
 //Here control is not entering in Contextmenu why?
 @Override
 public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo)
   {

       super.onCreateContextMenu(menu, v, menuInfo);

    Toast.makeText(MainActivity.this, "Entering Context Menu",  Toast.LENGTH_SHORT).show();

       getMenuInflater().inflate(R.menu.main, menu);

       menu.setHeaderTitle("Select The Action");
       menu.add(0, v.getId(), 0, "Call");
       menu.add(0, v.getId(), 0, "Send SMS");

}


@Override
public boolean onContextItemSelected(MenuItem item)
{


    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();


        return true;


    }


}
sajid Hussain
  • 385
  • 1
  • 6
  • 15
  • you override the `listView adapter` because you call `listView.setAdapter`multiple times. – wake-0 Feb 02 '17 at 08:33
  • You need to use `registerForContextMenu` and `setOnItemLongClickListener` together? – K Neeraj Lal Feb 02 '17 at 08:34
  • please write me some code i am new here @KevinWallis – sajid Hussain Feb 02 '17 at 08:36
  • yes i write both together but still no show, control is not enter in context menu, i dont know why? @KNeerajLal – sajid Hussain Feb 02 '17 at 08:37
  • Please don't repeat questions. Simply editing your original post with any new information you have will bump it to the top of the active queue. – Mike M. Feb 02 '17 at 08:39
  • my question is not solve still @MikeM. – sajid Hussain Feb 02 '17 at 08:45
  • Yes, I understand that, but there's no need to post it multiple times. All you have to do is edit your original post with any new information you have, any new code you've tried, or an explanation of why the offered answers aren't working, and your question will be bumped to the top of the active queue. The only thing multiple copies does is introduce extra useless stuff that makes searching the site that much harder. – Mike M. Feb 02 '17 at 08:48
  • if you are android developer then please give me your skype contact name i will contact you when i will stuck, I am new to android thanks @MikeM. – sajid Hussain Feb 02 '17 at 08:54

0 Answers0