针对项目需要要进行自定义适配器来设计,主要是因为的是在扫描本地文件,要创建对象来接受文件的name和文件的path,如果使用SimpleAdapter显然是不满足要求的,这里传递的List集合形式不对,所以这里使用自定义适配器来完成。
public voidinitData(){
final List<VideoModel> videoList=getVideoFile(allVideoList, Environment.getExternalStorageDirectory());
MyAdapter myAdapter=new MyAdapter($.getContext(),R.layout.line,videoList);
videoListView.setAdapter(myAdapter);
videoListView.setOnItemClickListener(newAdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,int position,longid)
{
String filepath = videoList.get(position).getVideoPath();
Intent intent = new Intent();
intent.putExtra("filepath",filepath);
intent.setClass($.getContext(), PlayActivity.class);
startActivity(intent);
}
});
}
classMyAdapter extends BaseAdapter{
private Context context;//上下文
private int resourceId;//列表项的布局文件
private List<VideoModel>list3;
public MyAdapter(Context context,int resourceId, List<VideoModel> list3) {
this.context= context;
this.resourceId= resourceId;
this.list3= list3;
}
@Override
public int getCount() {
int count=0;
if(list3!=null){
return list3.size();
}
return count;
}
@Override
public Object getItem(intposition) {
return list3.get(position);
}
@Override
public long getItemId(intposition) {
return position;
}
@Override
public View getView(intposition, ViewconvertView, ViewGroup parent) {
View view=null;
if(convertView!=null){
view=convertView;
}else{
view = LayoutInflater.from(context).inflate(resourceId, parent,false);
}
VideoHolder holder =(VideoHolder) view.getTag();
if(holder==null){
holder=new VideoHolder();
holder.textView=(TextView)view.findViewById(R.id.file_name);
holder.image=(ImageView)view.findViewById(R.id.icon);
view.setTag(holder);
}
holder.textView.setText(list3.get(position).getVideoName().toString());
holder.image.setImageResource(R.drawable.file);
return view;
}
class VideoHolder{
TextView textView;
ImageView image;
}
}