我在这里与层次结构稍微挣扎。我想要在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