2012-02-25 30 views
5

Làm cách nào để đặt tiêu đề của menu ngữ cảnh từ mục Listview đã chọn? Dưới đây là hoạt động chính của tôi .Đặt tiêu đề của menu ngữ cảnh từ mục Listview được chọn

public class OListActivity extends ListActivity { 
...... 
...... 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     registerForContextMenu(getListView()); 
     ...... 
...... 
     MatrixCursor cursor; 
     cursor = NameManager.getnameList(); 
     startManagingCursor(cursor); 
     String[] from = { "name", "info", "status", BaseColumns._ID }; 
     int[] to = { R.id.name, R.id.info, R.id.status }; 
     SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
       R.layout.row, cursor, from, to); 
     setListAdapter(adapter); 
    } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
      ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 
     menu.setHeaderTitle("Menu");// TODO Change to name of selected listview item. 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.context_menu, menu); 
    } 
..... 
..... 

Tôi cần đặt menu.setHeaderTitle thành R.id.name. Tôi nhận thức được một similer question nhưng nó không đề cập đến việc xử lý một phức tạp ListView với nhiều lần xem văn bản.

Trả lời

13

Sử dụng các tham số ContextMenuInfo từ phương pháp onCreateContextMenu():

@Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
      ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 
     AdapterView.AdapterContextMenuInfo info; 
     try { 
      // Casts the incoming data object into the type for AdapterView objects. 
      info = (AdapterView.AdapterContextMenuInfo) menuInfo; 
     } catch (ClassCastException e) { 
      // If the menu object can't be cast, logs an error. 
      Log.e(TAG, "bad menuInfo", e); 
      return; 
     } 
     Cursor cursor = (Cursor) getListAdapter().getItem(info.position); 
     if (cursor == null) { 
      // For some reason the requested item isn't available, do nothing 
      return; 
     } 

     // if your column name is "name" 
     menu.setHeaderTitle(cursor.getString(cursor.getColumnIndex("name"))); 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.context_menu, menu); 
    } 
+0

Điều đó có hiệu quả, cảm ơn bạn rất nhiều. Bằng cách này tôi đã phải thay đổi để 'menu.setHeaderTitle (cursor.getString (1))'. –

+0

@BinoyBabu Bạn đúng phương thức 'getString()' yêu cầu một 'int' và không phải là một' Chuỗi'. Tôi mệt mỏi và tôi đang nghĩ đến phương thức 'cursor.getString (cursor.getColumnIndex (" name "));'. – Luksprog

+0

Đừng lo lắng về điều đó. Bạn là anh hùng của tôi anyway. Ngủ một chút nhé? –

0

Tôi biết điều này là khá một bài cũ và là câu trả lời đúng là tốt. Tuy nhiên, trong khi sử dụng này ngày hôm nay tôi đã xem qua một cái gì đó tôi sẽ muốn thêm.

Thông số ContextMenuInfo được sử dụng để tìm vị trí mục chính xác đã khởi chạy ContextMenu tức là mục adpater của chúng tôi.

Do đó, nó có thể trở lại một mục kiểu định nghĩa trong phương pháp getItem() của Adapter sử dụng vị trí đó info.position, như ở trên phương pháp GetItem() trả về một con trỏ đối tượng.

(Trong trường hợp của tôi nó trở lại một lớp Model và sau đó tôi nhận ra rằng để thiết lập Tiêu đề thông qua menu.setHeaderTitle() tôi có thể vượt qua các phương pháp của tôi rằng mô hình của tôi hỗ trợ như model.getItamName())

Ngoài ra, hãy nhớ nếu AdapterView của bạn có chứa bất kỳ tiêu đề bạn sẽ phải loại trừ chúng trong khi tìm nạp vị trí bằng cách sử dụng menuInfo. Giống như,

Cursor cursor = (Cursor) getListAdapter().getItem(info.position - yourList.getHeaderViewsCount()); 

Hy vọng điều này sẽ giúp ai đó. :)

Các vấn đề liên quan