我有这样的代码:
public class ListConActivity extends Activity {
private String[] Distro = { "Ubuntu", "Arch Linux", "Mandriva",
"Open Suse", "IGOS Nusantara", "Linux Mint", "Debian", "Fedora",
"CrunchBang", "Backtrack", "Puppy Linux", "OpenBSD", "Slackware",
"BlankOn", "CentOS" };
private String[] pilihan_menu = { "Tambah Data", "Edit Data", "Hapus Data",
"Kirim Data" };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Arrays.sort(Distro);
ListView list = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Distro);
list.setAdapter(adapter);
registerForContextMenu(list);
}
public void onCreateContextMenu(ContextMenu menu, View tampil,
ContextMenuInfo menuInfo) {
if (tampil.getId() == R.id.list) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
menu.setHeaderTitle(Distro[info.position]);
for (int i = 0; i < pilihan_menu.length; i++) {
menu.add(Menu.NONE, i, i, pilihan_menu[i]);
}
}
}
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
String aksi = pilihan_menu[item.getItemId()];
String nama_pilihan = Distro[info.position];
String isi = String.format("Anda melakukan operasi %s pada pilihan %s",
aksi, nama_pilihan);
Toast.makeText(this, isi, Toast.LENGTH_LONG).show();
return true;
}
}
它的工作完美地显示了长按一下的上下文菜单。我更改了代码来实现onclick,只需点击一下即可打开上下文菜单。
public class ListConActivity extends Activity implements OnClickListener {
private String[] Distro = { "Ubuntu", "Arch Linux", "Mandriva",
"Open Suse", "IGOS Nusantara", "Linux Mint", "Debian", "Fedora",
"CrunchBang", "Backtrack", "Puppy Linux", "OpenBSD", "Slackware",
"BlankOn", "CentOS" };
private String[] pilihan_menu = { "Tambah Data", "Edit Data", "Hapus Data",
"Kirim Data" };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Arrays.sort(Distro);
ListView list = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Distro);
list.setAdapter(adapter);
registerForContextMenu(list);
list.setOnClickListener(this);
}
public void onCreateContextMenu(ContextMenu menu, View tampil,
ContextMenuInfo menuInfo) {
if (tampil.getId() == R.id.list) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
menu.setHeaderTitle(Distro[info.position]);
for (int i = 0; i < pilihan_menu.length; i++) {
menu.add(Menu.NONE, i, i, pilihan_menu[i]);
}
}
}
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
String aksi = pilihan_menu[item.getItemId()];
String nama_pilihan = Distro[info.position];
String isi = String.format("Anda melakukan operasi %s pada pilihan %s",
aksi, nama_pilihan);
Toast.makeText(this, isi, Toast.LENGTH_LONG).show();
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
openContextMenu(v);
}
}
编译成功..但我强行关闭。你能帮我吗?谢谢老兄!
答案 0 :(得分:0)
您已为ListView
设置了点击侦听器,而不是列出项目。使用setOnItemClickListener
和AdapterView.OnItemClickListener
答案 1 :(得分:0)
是啊:为listView&amp;做一个setOnItemClickListener在该click方法中,从Activity类
调用openContextMenu(view)