Last active
December 15, 2015 23:59
-
-
Save leonguyen/5344253 to your computer and use it in GitHub Desktop.
Android Lab: List View with ContextMenu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.androidlab; | |
| import android.app.ListActivity; | |
| import android.content.Context; | |
| import android.os.Bundle; | |
| import android.view.ContextMenu; | |
| import android.view.MenuInflater; | |
| import android.view.MenuItem; | |
| import android.view.View; | |
| import android.view.ContextMenu.ContextMenuInfo; | |
| import android.widget.AdapterView.AdapterContextMenuInfo; | |
| import android.widget.AdapterView; | |
| import android.widget.ArrayAdapter; | |
| import android.widget.ListView; | |
| import android.widget.TextView; | |
| import android.widget.Toast; | |
| public class MainActivity extends ListActivity { | |
| String item; | |
| static final String[] arrOS = new String[] { "Android", "iPhone", | |
| "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", | |
| "Max OS X", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", | |
| "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2", | |
| "Android", "iPhone", "WindowsMobile" }; | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.textView, arrOS)); | |
| ListView listView = getListView(); | |
| listView.setTextFilterEnabled(true); | |
| registerForContextMenu(listView); | |
| listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { | |
| @Override | |
| public boolean onItemLongClick(AdapterView<?> arg0, View view, | |
| int arg2, long arg3) { | |
| // TODO Auto-generated method stub | |
| TextView txtId = (TextView) view.findViewById(R.id.textView); | |
| item = txtId.getText().toString(); | |
| return false; | |
| }}); | |
| } | |
| @Override | |
| public void onCreateContextMenu(ContextMenu menu, View v, | |
| ContextMenuInfo menuInfo) { | |
| // TODO Auto-generated method stub | |
| super.onCreateContextMenu(menu, v, menuInfo); | |
| MenuInflater inflater = getMenuInflater(); | |
| inflater.inflate(R.menu.context_menu, menu); | |
| } | |
| @Override | |
| public boolean onContextItemSelected(MenuItem item) { | |
| // TODO Auto-generated method stub | |
| super.onContextItemSelected(item); | |
| switch (item.getItemId()) { | |
| case R.id.mnu_del: | |
| Toast.makeText(this, "This is delete: " + item, Toast.LENGTH_LONG).show(); | |
| return true; | |
| default: | |
| return super.onContextItemSelected(item); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment