Java 如何从我的ListView中引用元素内部的视图?
因此,我为我的ListView创建了一个自定义适配器,它有三个视图-TextView、ImageView和一个基本视图:Java 如何从我的ListView中引用元素内部的视图?,java,android,listview,adapter,Java,Android,Listview,Adapter,因此,我为我的ListView创建了一个自定义适配器,它有三个视图-TextView、ImageView和一个基本视图: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_p
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/AllNotesFragment">
<TextView
android:id="@+id/addNoteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="25dp"
android:textSize="18sp"
android:gravity="center"
android:fontFamily="@font/ukij_qolyazma"
android:text="+new"
/>
<ImageButton
android:id="@+id/deleteNoteImageButton"
android:layout_width="50dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="0dp"
android:layout_marginEnd="30dp"
android:textSize="24sp"
android:gravity="center"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:fontFamily="@font/ukij_qolyazma"
/>
<View
android:id="@+id/underlineView"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginHorizontal="30dp"
android:layout_marginTop="70dp"
android:background="@color/colorMainDark" />
<ListView
android:id="@+id/notesListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="75dp"
android:layout_marginBottom="0dp">
</ListView>
把我的一堆笔记都填上了
notes = (ArrayList<Note>) db.getAllNotesForDay(NotesForDayActivity.getRememberDay(),
NotesForDayActivity.getRememberMonth(),
NotesForDayActivity.getRememberYear());
for (Note note : notes) {
noteTitles.add(note.getTitle());
}
NotesListAdapter adapter = new NotesListAdapter(((NotesForDayActivity) getActivity()).getContext(), notes);
notesListView.setAdapter(adapter);
notes=(ArrayList)db.getAllNotesForDay(NotesForDayActivity.getRememberDay(),
NotesForDayActivity.GetMemberMonth(),
NotesForDayActivity.getMemberyear());
用于(注释:注释){
添加(note.getTitle());
}
NotesListAdapter=新的NotesListAdapter(((NotesForDayActivity)getActivity()).getContext(),notes);
notesListView.setAdapter(适配器);
使用我的自定义适配器:
public class NotesListAdapter extends BaseAdapter {
private static final String TAG = "NotesListAdapter";
public static Context context;
private RelativeLayout notesListRelativeLayout;
private TextView noteTitleTextView;
private ImageView tickImage;
private View underlineView;
private List<Note> notes;
// !
private View listItemsView;
public NotesListAdapter(Context context, List<Note> notes) {
this.notes = notes;
this.context = context;
}
@Override
public int getCount() {
return NotesForDayActivity.getCountNotes();
}
@Override
public Object getItem(int position) {
return notes.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
listItemsView = convertView;
if (listItemsView == null) {
listItemsView = LayoutInflater.from(NotesForDayActivity.context).inflate(R.layout.notes_list_layout, null);
}
underlineView = listItemsView.findViewById(R.id.underlineView);
notesListRelativeLayout = (RelativeLayout) listItemsView.findViewById(R.id.notesListRelativeLayout);
noteTitleTextView = (TextView) listItemsView.findViewById(R.id.noteTitleTextView);
tickImage = (ImageView) listItemsView.findViewById(R.id.tickImageView);
noteTitleTextView.setText(notes.get(position).getTitle());
return listItemsView;
}
public类NotesListAdapter扩展了BaseAdapter{
私有静态最终字符串标记=“NotesListAdapter”;
公共静态语境;
私人相对人注意事项相对布置;
私有文本视图noteTitleTextView;
私有图像查看图像;
私有视图;
私人名单说明;
// !
私有视图列表项视图;
公共注释StatAdapter(上下文、列表注释){
this.notes=注释;
this.context=上下文;
}
@凌驾
public int getCount(){
return NotesForDayActivity.getCountNotes();
}
@凌驾
公共对象getItem(int位置){
返回注释。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
listItemsView=convertView;
如果(listItemsView==null){
listItemsView=LayoutFlater.from(notesfordayaActivity.context)。充气(R.layout.notes\u list\u layout,null);
}
underlineView=listItemsView.findViewById(R.id.underlineView);
notesListRelativeLayout=(RelativeLayout)listItemsView.findViewById(R.id.notesListRelativeLayout);
noteTitleTextView=(TextView)listItemsView.findViewById(R.id.noteTitleTextView);
tickImage=(ImageView)listItemsView.findViewById(R.id.tickImageView);
noteTitleTextView.setText(notes.get(position.getTitle());
返回列表项视图;
}
我可以使用ListView侦听器访问ListView中的项目,但我不知道如何访问ListView中该特定项目中的视图
因此,如果我将OnClick设置为这样:
notesListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
notesListView.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
}
});
如何引用该项目中的三个视图之一。例如,我需要设置imageview的可见性:
非常感谢您的帮助。请尝试此操作,McClickListener将返回该视图,以便您可以通过该视图进行访问
notesListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageView tickImage = view.findViewById(R.id. tickImageView);
if(tickImage!=null){
tickImage.setVisibility(View.GONE);
}
}
});
notesListView.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
ImageView tickImage=view.findViewById(R.id.tickImageView);
if(tickImage!=null){
tickImage.setVisibility(View.GONE);
}
}
});
notesListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageView tickImage = view.findViewById(R.id. tickImageView);
if(tickImage!=null){
tickImage.setVisibility(View.GONE);
}
}
});