注意:
1 class ViewHolder
2 convertView = mInflater.inflate(R.layout.item_task, null);
3 convertView.setTag(holder);
4 holder = (ViewHolder) convertView.getTag();
package com.epapandroid.adapter;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.epapandroid.activity.R;
import com.epapandroid.domain.Dayplanandrange;
import com.epapandroid.service.DayPlanAndRangeService;
public class TaskAdapter extends BaseAdapter{
public List<Dayplanandrange> mDayplanandranges;
private LayoutInflater mInflater;
private Context tempApplicationcontext;
public TaskAdapter(Context applicationcontext) {
this.mInflater = LayoutInflater.from(applicationcontext);
tempApplicationcontext=applicationcontext;
getData();
}
private void getData(){
DayPlanAndRangeService dayPlanAndRangeService=new DayPlanAndRangeService(tempApplicationcontext);
mDayplanandranges = dayPlanAndRangeService.getCursorRange();
}
public class ViewHolder {
public TextView UPSTATUE;
public TextView DAYRANGEID;
public TextView DAYPLANID;
public TextView GROUPNO;
public TextView MEMBERSNAME;
public TextView STARTDATE;
public TextView ENDDATE;
public TextView SITENAME;
public TextView PILLARNO;
}
public int getCount() {
return mDayplanandranges.size();
}
public Object getItem(int position) {
return mDayplanandranges.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item_task, null);
holder.UPSTATUE = (TextView) convertView.findViewById(R.id.UPSTATUE);
holder.DAYRANGEID = (TextView) convertView.findViewById(R.id.DAYRANGEID);
holder.DAYPLANID = (TextView) convertView.findViewById(R.id.DAYPLANID);
holder.GROUPNO = (TextView) convertView.findViewById(R.id.GROUPNO);
holder.MEMBERSNAME = (TextView) convertView.findViewById(R.id.MEMBERSNAME);
holder.STARTDATE = (TextView) convertView.findViewById(R.id.STARTDATE);
holder.ENDDATE = (TextView) convertView.findViewById(R.id.ENDDATE);
holder.SITENAME = (TextView) convertView.findViewById(R.id.SITENAME);
holder.PILLARNO = (TextView) convertView.findViewById(R.id.PILLARNO);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String upstatueStr = "";
if(0 == mDayplanandranges.get(position).getUpstatue()){
upstatueStr = "未巡视";
}else if (1 == mDayplanandranges.get(position).getUpstatue()){
upstatueStr = "正在巡视";
}else if(2 == mDayplanandranges.get(position).getUpstatue()){
upstatueStr = "巡视结束";
}
holder.UPSTATUE.setText(upstatueStr);
holder.DAYRANGEID.setText(String.valueOf(mDayplanandranges.get(position).getDayrangeID()));
holder.DAYPLANID.setText(String.valueOf(mDayplanandranges.get(position).getDayplanID()));
holder.GROUPNO.setText(String.valueOf(mDayplanandranges.get(position).getGroupno()));
holder.MEMBERSNAME.setText(mDayplanandranges.get(position).getMembersname());
holder.STARTDATE.setText(mDayplanandranges.get(position).getStartdate());
holder.ENDDATE.setText(mDayplanandranges.get(position).getEnddate());
holder.SITENAME.setText(mDayplanandranges.get(position).getSiteName());
holder.PILLARNO.setText(String.valueOf(mDayplanandranges.get(position).getPillarNO()));
holder.DAYRANGEID.setVisibility(View.GONE);
holder.DAYPLANID.setVisibility(View.GONE);
if(mDayplanandranges.get(position).getUpstatue()==1)
{
// 正在巡视
convertView.setBackgroundResource(R.color.blue);
}
else if(mDayplanandranges.get(position).getUpstatue()==2)
{
//巡视结束
convertView.setBackgroundResource(R.color.gray);
}
else {
convertView.setBackgroundResource(R.color.white);
}
return convertView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/UPSTATUE"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="3"
android:gravity="center"
android:text="UPSTATUE"
android:textColor="@color/black"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
/>
<TextView
android:id="@+id/DAYRANGEID"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0"
android:gravity="center"
android:text="DAYRANGEID"
android:textColor="@color/black"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/DAYPLANID"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0"
android:gravity="center"
android:text="DAYPLANID"
android:textColor="@color/black"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/GROUPNO"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:gravity="center"
android:text="GROUPNO"
android:textColor="@color/black"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/MEMBERSNAME"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="6"
android:gravity="center"
android:text="MEMBERSNAME"
android:textColor="@color/black"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/STARTDATE"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="5"
android:gravity="center"
android:text="STARTDATE"
android:textColor="@color/black"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/ENDDATE"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="5"
android:gravity="center"
android:text="ENDDATE"
android:textColor="@color/black"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/SITENAME"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="6"
android:gravity="center"
android:text="SITENAME"
android:textColor="@color/black"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
/>
<TextView
android:id="@+id/PILLARNO"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="5"
android:gravity="center"
android:text="PILLARNO"
android:textColor="@color/black"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"/>
</LinearLayout>