我在這裏與層次結構稍微掙扎。我想要在listView中使用id爲delete_img的每個ImageButton視圖的引用。 imagebutton在行佈局xml中通過XML添加。在列表視圖中獲取對視圖的引用
本質上我想能夠設置每行內的某個元素的可見性,但我無法弄清楚如何得到那種參考。有沒有其他的方法來做到這一點?方法deleteShow()是我試圖得到它迄今爲止,但它顯然是錯誤的,因爲我試圖設置可視性時得到一個空指針。
NotesFragment
public class NotesFragment extends ListFragment {
private CommentsDataSource datasource;
private View v = null;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Cursor theNotes = (Cursor) returnNotes();
String[] projection = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_COMMENT,
MySQLiteHelper.COLUMN_COMMENTNAME,
MySQLiteHelper.COLUMN_FOLDERFK };
int[] to = new int[] { R.id.id_txt, R.id.content_txt, R.id.title_text };
@SuppressWarnings("deprecation")
SimpleCursorAdapter sca = new SimpleCursorAdapter(getActivity(),
R.layout.notes_list_layout, theNotes, projection, to);
setListAdapter(sca);
View v = inflater.inflate(R.layout.notesfragment, container, false);
deleteShow();
return v;
}
@Override
public void onListItemClick(ListView parent, View v, int position, long id) {
Intent intentView = new Intent(getActivity().getApplicationContext(),
ViewNote.class);
intentView.putExtra("id", id);
startActivity(intentView);
}
public Cursor returnNotes() {
Cursor theNotesCursor = null;
datasource = new CommentsDataSource(getActivity());
datasource.open();
theNotesCursor = datasource.getAllCommentsAsCursor();
return theNotesCursor;
}
public void deleteShow() {
ImageButton b = (ImageButton) getActivity().findViewById(R.id.delete_img);
b.setVisibility(View.INVISIBLE);
}
public void onPause() {
super.onPause();
datasource.close();
}
}
好吧,我有點懷疑這一點。讓我說清楚。據我瞭解,不可能獲得對具有id(suchandsuch)的列表行中的每個元素的引用,並且單獨獲取每個元素,並且由於listView的工作方式,聽起來像是一場噩夢。 理論上,因爲我當前的按鈕是一個XML元素。我可以將它從XML中刪除,將它添加回每個列表行中編程生成的引用,當我需要讓它們出現/消失後使用? – lorless 2013-04-10 18:24:59
如果你列出的是一個固定的長度也許。假設你可以正確地獲得對這些按鈕的引用,你需要維護一個單獨的列表來跟蹤這些引用。什麼是觸發他們被刪除的情況?也許我可以建議一個更簡單的方法? – Rarw 2013-04-10 19:27:09
該按鈕在每行中的一組文本視圖之後存在。本質上它是一個刪除按鈕,允許用戶刪除與該行相關的信息。我希望能夠隨意顯示/隱藏刪除按鈕,因爲我不希望用戶不小心點擊刪除按鈕。 (加上它看起來很糟糕)。 – lorless 2013-04-10 19:45:26