2013-06-09 3 views
1

ListView에는 ContextMenu이 내장되어 있습니다.이 메뉴를 사용하면 TextView에있는 항목 속성의 이름을 바꿀 수 있습니다.액티비티/프래그먼트에서 어댑터 속성 변경

그러나 아래로 스크롤하여 다시 돌아 오면 이전 이름이 다시 나타납니다.

이유 : adapter이 업데이트되지 않습니다.

예 : adapter에는 각 행에 대해 개체가 있습니다 : Review. 그런 다음 name이라는 속성이 있습니다.

그래서 getView()에서 내가 할 :

사용자가 업데이트 할 경우 지금
Review r = getItem(position); 
name = r.name; 
myTextView.setText(name); 

해당합니다 (ContextMenu 통해) Fragment, 내가 을이 속성 (이름)에 액세스 할 방법 adapter/개체를 업데이트에서 TextView 그 같은 위치?

+1

'ContextMenu'의 콜백에서 http://developer.android.com/reference/android/widget/AdapterView.AdapterContextMenuInfo.html을 검색 할 수 있습니다. 'position' 매개 변수를 사용하고 그 위치에 대해 (어댑터에 전달 된) 목록에서'Review' 항목을 업데이트하고'notifyDataSetChanged()'를 호출하십시오. – Luksprog

답변

1

listView에서 contextMenu가 만들어진 위치를 얻으려면 다음을 시도하십시오. 이 코드에서

public boolean onContextItemSelected(MenuItem item) { 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
    int position = (int) info.id; 
    // This is the position in the ListView for which contextMenu was created. 
    // get and update your object and notify the adapter. 
    Review r = myListView.getAdapter().getItem(position); 
    r.name = "whatever new thing"; 
    myListView.getAdapter(). notifyDataSetChanged(); 
} 

우리는 새 값으로 AdapterReview 객체를 업데이트하고 일어난 변화의 어댑터를 통지한다. 그러면 목록이 새로 고쳐지고 방금 제공된 새 값이 사용됩니다.

+0

완벽; 그게 내가 필요한거야. – KickingLettuce