listView 的 adapter
ListView代码
<ListView android:id="@+id/areaList" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" />
areaList.setAdapter(adapter);
adapter 的 xml文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="match_parent" android:padding="@dimen/item_padding" android:src="@mipmap/ic_item_project" /> //一张照片而已 <TextView android:id="@+id/tvFileName" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center_vertical" android:padding="@dimen/item_padding" android:textSize="@dimen/item_new_project_file_name_text_size" /></LinearLayout>adapter代码
package com.zsch.forestinventory.adapter;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;import com.zsch.forestinventory.R;import java.util.List;/** * Created by 11827 on 2017/11/8. * 显示本地项目的适配器 */public class NewProjectFilesAdapter extends BaseAdapter { private Context context; private List<String> fileNames; public NewProjectFilesAdapter(Context context, List<String> fileNames) { this.context = context; this.fileNames = fileNames; } @Override public int getCount() { return fileNames.size(); } //看看 总共有多少项目 @Override public String getItem(int position) { //获得 点击的 是第几个 return fileNames.get(position); } @Override public long getItemId(int position) { return position; } // 通过 点击的 获得 id 去操作 @Override 重要在这 public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { //convertView是一个作为缓存的view,通过使用这个缓存可以替换掉用Inflater加载组件这一步。 convertView = View.inflate(context, R.layout.item_new_project_file_name, null); //为 adapter对应的的xml文件 holder = new ViewHolder(); holder.tvFileName = convertView.findViewById(R.id.tvFileName); convertView.setTag(holder); //Tag经常用来存储一些view的数据 } else { holder = (ViewHolder) convertView.getTag(); } holder.tvFileName.setText(fileNames.get(position)); //設置显示的内容 return convertView; } class ViewHolder { TextView tvFileName; }}
来源:https://www.cnblogs.com/dushutai/p/12659400.html