1

我正在開發一個todo應用程序。在這個應用程序中,待辦事項作爲列表項添加到列表視圖中。每個列表項(對應於每個待辦事項)具有包含多個視圖元素的相對佈局。我爲這個listview註冊了一個上下文菜單。但問題是,當我長按列表項目時,它不會被解僱。以下是代碼塊。請幫助..上下文菜單沒有爲列表視圖中的複合列表項觸發android

列表活動(列表視圖顯示待辦事項):

公共類ToDoManagerActivity擴展ListActivity {

private static final int ADD_TODO_ITEM_REQUEST = 0; 
private static final String FILE_NAME = "myToDos.txt"; 
private static final String TAG = "Lab-UserInterface"; 

// IDs for menu items 
private static final int MENU_DELETE = Menu.FIRST; 
private static final int MENU_DUMP = Menu.FIRST + 1; 
private static final int MENU_ADD = Menu.FIRST + 2; 

ToDoListAdapter mAdapter; 
TextView footerView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Create a new TodoListAdapter for this ListActivity's ListView 
    mAdapter = new ToDoListAdapter(getApplicationContext()); 


    // TODO - Attach the adapter to this ListActivity's ListView 
    getListView().setAdapter(mAdapter); 

    // register for context menu 
    registerForContextMenu(getCurrentFocus()); 

} 



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

    MenuInflater contextMenu = getMenuInflater(); 
    contextMenu.inflate(R.menu.menu_context, menu); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    super.onCreateOptionsMenu(menu); 

    menu.add(Menu.NONE, MENU_DELETE, Menu.NONE, "Delete all"); 
    menu.add(Menu.NONE, MENU_DUMP, Menu.NONE, "Dump to log"); 
    menu.add(Menu.NONE, MENU_ADD, Menu.NONE, "Add ToDo"); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case MENU_DELETE: 
     mAdapter.clear(); 
     return true; 
    case MENU_DUMP: 
     dump(); 
     return true; 
    case MENU_ADD: 
     Intent mAddToDo = new Intent(ToDoManagerActivity.this, 
       AddToDoActivity.class); 
     startActivityForResult(mAddToDo, ADD_TODO_ITEM_REQUEST); 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 

}}

的列表項的內容編排如下:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/RelativeLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/titleView" 
    android:textColor="#000000" 
    android:textStyle="italic" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:textAppearance="?android:attr/textAppearanceLarge" > 
</TextView> 

<TextView 
    android:id="@+id/StatusLabel" 
    android:textColor="#000000" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/statusCheckBox" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="17dp" 
    android:text="@string/done_string" > 
</TextView> 

<CheckBox 
    android:id="@+id/statusCheckBox" 
    android:textColor="#000000" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="52dp" 
    android:layout_toRightOf="@+id/StatusLabel" > 
</CheckBox> 

<TextView 
    android:id="@+id/PriorityLabel" 
    android:textColor="#000000" 
    android:textStyle="bold" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/statusCheckBox" 
    android:layout_alignTop="@+id/StatusLabel" 
    android:layout_toLeftOf="@+id/priorityView" 
    android:text="@string/priority_string" > 
</TextView> 

<TextView 
    android:id="@+id/priorityView" 
    android:textColor="#000000" 
    android:layout_width="50dip" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/statusCheckBox" 
    android:layout_alignParentRight="true" 
    android:layout_alignTop="@+id/StatusLabel" > 
</TextView> 

<TextView 
    android:id="@+id/DateLabel" 
    android:textColor="#000000" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/statusCheckBox" 
    android:text="@string/date_string" > 
</TextView> 

<TextView 
    android:id="@+id/dateView" 
    android:textColor="#000000" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/DateLabel" 
    android:layout_toRightOf="@+id/DateLabel" > 
</TextView> 

</RelativeLayout> 

上下文菜單xml如下:

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

<item 
    android:id="@+id/rajit_me" 
    android:title="Rajit"/> 
<item 
    android:id="@+id/mou_me" 
    android:title="Mou"/> 
<item 
    android:id="@+id/suha_me" 
    android:title="Suha"/> 
<item 
    android:id="@+id/samah_me" 
    android:title="samah"/> 
</menu> 

回答

1

我已經解決了這個問題:就以下兩個屬性添加到列表中的項目清單觀點:

<CheckBox 
android:id="@+id/statusCheckBox" 
android:textColor="#000000" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentTop="true" 
android:layout_marginTop="52dp" 
android:layout_toRightOf="@+id/StatusLabel" 

<!-- add these two attributes to disable the focus of the check box --> 
android:focusable="false" 
android:focusableInTouchMode="false" 
> 
</CheckBox> 
相關問題