0

我有这样的代码:

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);
    }
}

编译成功..但我逼近了。你能帮我吗?多谢,伙计!

4

2 回答 2

0

是的:为 listView 做一个 setOnItemClickListener 并在那个点击方法中从 Activity 类调用 openContextMenu(view)

于 2012-10-02T14:40:21.330 回答
0

您设置了点击监听器ListView,而不是列表项。使用setOnItemClickListenerAdapterView.OnItemClickListener

于 2012-10-01T12:46:13.570 回答