如何将多个功能/选项发送到多个列表项?

时间:2015-08-05 12:02:33

标签: java android android-intent android-listview javadoc

在我的Profile.java类中,我有listview,我在上下文菜单中注册了它,并在profile.xml文件中传递了两个名为General和Silent的项目。我想要的是,当用户点击常规时,它必须只显示激活选项,当用户选择静音时,它应显示停用。激活和取消激活选项位于我的context_menu.xml文件中。

我正在给你我所有的文件。

Profile.java

Activity activity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile);

         activity = this;
        //List Items

          ListView lv  = (ListView) findViewById(android.R.id.list);

        registerForContextMenu(lv);

        setListAdapter(new ArrayAdapter<String>(this, 
                android.R.layout.simple_list_item_1,
                getResources().getStringArray(R.array.profile)));




    }



    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub

        //menu.add("hello");
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);


        }

是profile.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="profile">
        <item name="General">General</item>
        <item name="Silent">Silent</item>



    </string-array>

</resources>

Context_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:title="@string/Activate" android:id="@+id/item1"/>


        <item android:title="@string/Deactivate" android:id="@+id/item2"/>

    </menu>

1 个答案:

答案 0 :(得分:0)

yourList.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
        final int position, long id) {
        your code
    }
}