1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| package com.example.toby.myapplication;
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.TextView;
import java.util.List;
/** * author : toby * e-mail : 13128826083@163.com * time : 2018/6/20 * desc : */
public class MyAdapter extends BaseAdapter {
private Context context; private List<String> dataList; private OnItemClicked onItemClicked;
public MyAdapter(Context context, List<String> dataList, OnItemClicked onItemClicked) { this.context = context; this.dataList = dataList; this.onItemClicked = onItemClicked; }
@Override public long getItemId(int position) { return position; }
@Override public int getCount() { return dataList.size(); }
@Override public Object getItem(int position) { return dataList.get(position); }
@Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.item_list_view_layout, null); viewHolder = new ViewHolder(); viewHolder.layoutItem = convertView.findViewById(R.id.layoutItem); viewHolder.txtView = convertView.findViewById(R.id.txtView); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.txtView.setText(dataList.get(position)); viewHolder.layoutItem.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onItemClicked.showMessage(position); } }); return convertView; }
protected static class ViewHolder { LinearLayout layoutItem; TextView txtView; }
}
|