public TextView mTextName; public TextView mTextId; public TextView mTextCourse; public Button mDelButton; } @Override
public int getCount() {
return mArrayitem.size() + 1; }
@Override
public Object getItem(int position) { return position + 1; }
@Override
public long getItemId(int position) { return position + 1; }
@Override
public View getView(int position, View convertview, ViewGroup group) { ViewHolder mViewHolder; final int id = position; if (convertview == null) {
convertview = mInflater.inflate(R.layout.content_main, null); mViewHolder = new ViewHolder();
mViewHolder.mDelButton = (Button) convertview .findViewById(R.id.item_del); mViewHolder.mTextId = (TextView) convertview .findViewById(R.id.item_num);
mViewHolder.mTextCourse = (TextView) convertview .findViewById(R.id.item_course); mViewHolder.mTextName = (TextView) convertview .findViewById(R.id.item_name);
mViewHolder.mTextClass = (TextView) convertview .findViewById(R.id.item_class); convertview.setTag(mViewHolder); } else {
mViewHolder = (ViewHolder) convertview.getTag(); }
if (id == 0) {
setFirst(mViewHolder); } else {
Item item = mArrayitem.get(id - 1);
mViewHolder.mTextCourse.setText(item.course); mViewHolder.mTextId.setText(item.id); mViewHolder.mTextClass.setText(item.grade); mViewHolder.mTextName.setText(item.name); mViewHolder.mDelButton
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { mArrayitem.remove(id - 1);
ItemAdapter.this.notifyDataSetChanged();
} });
mViewHolder.mDelButton.setTextSize(mViewHolder.mTextId .getTextSize() / 2); }
return convertview; }
public void setData(ArrayList
public void setFirst(ViewHolder mHolder) {
mHolder.mDelButton.setBackgroundColor(Color.TRANSPARENT);
mHolder.mDelButton.setTextSize(mHolder.mTextId.getTextSize() / 2); mHolder.mDelButton.setText(\功能\); mHolder.mTextCourse.setText(\课程\); mHolder.mTextId.setText(\学号\); mHolder.mTextClass.setText(\班级\); mHolder.mTextName.setText(\姓名\); } }
5. item代码:
package com.example.g.controltest;
/** * Created by g on 2016/4/26. */ public class Item {
public String course; public String id; public String grade; public String name;
public Item(String id, String name, String grade,String course) { super(); this.id = id; this.name = name; this.grade = grade; this.course = course; } }
6. activity_main.xml代码:
android:layout_width=\ android:layout_height=\ android:text=\ android:textSize=\ android:textColor=\ android:background=\/> android:layout_width=\ android:layout_height=\ android:orientation=\>\ android:layout_width=\ android:layout_height=\ android:paddingTop=\ android:paddingLeft=\ android:text=\姓名\ android:textColor=\ android:textSize=\/> android:textSize=\ android:id=\ android:layout_width=\ android:layout_height=\ android:layout_marginLeft=\/> android:layout_width=\ android:layout_height=\ android:orientation=\> android:layout_width=\ android:layout_height=\ android:paddingTop=\ android:paddingLeft=\ android:text=\学号\ android:textColor=\ android:textSize=\/> android:textSize=\ android:id=\ android:layout_width=\ android:layout_height=\ android:layout_marginLeft=\/> android:layout_width=\ android:layout_height=\ android:orientation=\> android:layout_width=\ android:layout_height=\ android:paddingTop=\ android:paddingLeft=\ android:text=\年级\ android:textColor=\ android:textSize=\/> android:textSize=\ android:id=\ android:layout_width=\ android:layout_height=\ android:layout_marginLeft=\/> android:layout_width=\ android:layout_height=\ android:orientation=\> android:layout_width=\ android:layout_height=\ android:paddingTop=\ android:paddingLeft=\ android:text=\班级\ android:textColor=\ android:textSize=\/> android:textSize=\ android:id=\ android:layout_width=\ android:layout_height=\ android:layout_marginLeft=\/> android:layout_width=\ android:layout_height=\ android:orientation=\>\ android:id=\ android:layout_width=\ android:layout_height=\ android:text=\课程\ android:textSize=\ android:paddingTop=\ android:paddingLeft=\ android:textColor=\ /> android:id=\ android:layout_width=\ android:layout_height=\ />