2012-07-21 3 views
1

Я хочу поместить контекстное меню в список. Это кажется очень простым, но я получаю интересное поведение. Список имеет два поля: текстовое и текстовое. Длительное нажатие открывает меню в EditText, но больше нигде в списке. Вот фрагменты кода.Контекстное меню андроида listview

public class ManageClass extends ListActivity { 

super.onCreate(savedInstanceState); 
    setContentView(R.layout.manageclass); 

     //Tons of not relevant stuff that I would be happy to provide. 
        pfdata = new PortfolioData(this); 
      try { 
     Cursor cursor = getClasses(); 
     showClasses(cursor); 

    } finally { 
     pfdata.close(); 
    } 
} 

    private Cursor getClasses() { 

    String sql = "select c._id, c.NAME as NAME , c.percentage as PERCENTAGE " 
      + "from asset_classes c;"; 

    SQLiteDatabase db = pfdata.getReadableDatabase(); 

    Cursor cursor = db.rawQuery(sql, null); 
    startManagingCursor(cursor); 
    return cursor; 

} 

private void showClasses(Cursor cursor) { 

    adapter = new MySimpleCursorAdapter(this, R.layout.classrow, cursor, 
      FROM, TO, t); 

    // Log.d("TEST", Integer.toString(adapter.getCount())); 

    setListAdapter(adapter); 
    adapter.notifyDataSetChanged(); 
       //I've tried moving this line around but it hasn't made a difference. 
    registerForContextMenu(getListView()); 
} 

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, 
    ContextMenuInfo menuInfo) { 


     menu.add(Menu.NONE, 0, 0, "A"); 
     menu.add(Menu.NONE, 1, 1, "B"); 
     menu.add(Menu.NONE, 2, 2, "C"); 

} 

Наконец, сам макет, который я признаю, является слишком сложным

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/editText1" 
     android:layout_width="300px" 
     android:layout_height="wrap_content" 
     android:text="@string/TCLabel" /> 

    <TextView 
     android:id="@+id/classtotalpercentage" 
     android:layout_width="109dp" 
     android:layout_height="wrap_content" 
     android:textSize="50px" /> 
</LinearLayout> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/AddClassLabel" 
     android:layout_width="200px" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.38" 
     android:text="@string/AddClassLabel" /> 

    <Button 
     android:id="@+id/addclass_button" 
     android:layout_width="120dp" 
     android:layout_height="wrap_content" 
     android:text="@string/addclassbutton_label" /> 
</LinearLayout> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/grey" 
    android:orientation="horizontal" 
    android:padding="10sp" > 

    <TextView 
     android:id="@+id/assetclassHeader" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/assetclassHeader" 
     android:width="200dp" /> 

    <TextView 
     android:id="@+id/percentageHeader" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/assetclassHeader" 
     android:text="@string/percentageHeader" 
     android:width="100dp" /> 
</LinearLayout> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:fadeScrollbars="false" 
     android:scrollbarAlwaysDrawVerticalTrack="true" 
     android:scrollbarStyle="outsideOverlay" 
     android:scrollbars="vertical" /> 

    <TextView 
     android:id="@android:id/empty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/no_list_data" /> 
</RelativeLayout> 

</LinearLayout> 

Я признателен за любые советы. Я бы хотел, чтобы меню отображалось в тексте, а не edittext, если это возможно.

Обновление. Я обнаружил, что это имеет какое-то отношение к EditText. Если я изменю его на текстовое представление или удалю его все вместе, то все будет работать так, как должно.

ответ

5

У меня была аналогичная проблема с флажками и выяснилось, что проблема была вызвана тем, что флажок получает фокус. Я изменил атрибут на: android: focusable = "false"

и он теперь работает правильно.

+0

Я ценю вашу помощь. Я уже изменил весь свой подход к этому экрану, поэтому я не могу проверить, что он позаботится о том, что я вижу. – Clavijo

+0

У меня была такая же проблема с HorizontalScrollView в элементе List; ваш ответ помог мне. Спасибо. – vivek

Смежные вопросы