List View部分相关代码
package db;标签:viewHolder,app5,note,item,convertView,简单,import,notes,记事本 From: https://www.cnblogs.com/yansans/p/17232218.html
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.text002.R;
import java.util.ArrayList;
import java.util.List;
public class NoteAdapter extends BaseAdapter {
Context context;
List<NoteBean> notes = new ArrayList<>();
public NoteAdapter(Context context, List<NoteBean> notes){
this.context = context;
this.notes = notes;
}
//用于获取数据集的数量大小
@Override
public int getCount() {
return notes.size();
}
//用于返回指定位置position的那一个item
@Override
public Object getItem(int position) {
return notes.get(position);
}
//获取item的下标
@Override
public long getItemId(int position) {
return position;
}
//用于获取item的布局
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null){
//实例化
viewHolder = new ViewHolder();
convertView = View.inflate(context, R.layout.item_note,null);
viewHolder.item_note_title = convertView.findViewById(R.id.item_note_title);
viewHolder.item_note_key = convertView.findViewById(R.id.item_note_key);
viewHolder.item_note_contents = convertView.findViewById(R.id.item_note_contents);
viewHolder.item_note_daytime = convertView.findViewById(R.id.item_note_daytime);
//用于传输viewHolder
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
//获取当前item的数据
NoteBean note = notes.get(position);
//对viewHolder进行赋值
viewHolder.item_note_title.setText(note.getTitle());
viewHolder.item_note_key.setText(note.getKeywords());
viewHolder.item_note_contents.setText(note.getContents());
viewHolder.item_note_daytime.setText(note.getDaytime());
return convertView;
}
//static静态内部类没有对外部有引用,避免内存泄露
static class ViewHolder{
TextView item_note_title, item_note_daytime, item_note_contents, item_note_key;
}
}