本文转自:http://www.cnblogs.com/dyllove98/archive/2013/07/31/3228601.html
<ListView android:id="@+id/MeterReadingList" android:layout_width="fill_parent" android:layout_height="fill_parent" android:divider="@color/gray" android:dividerHeight="1dp" > </ListView>
public class MyCustomAdapter extends BaseAdapter { private LayoutInflater customInflater; private List<ReadyTask> list; private int layoutID; public class ViewHolder { TextView m_order; TextView m_MeterID; TextView m_RFID; TextView m_Area; TextView m_clientName; TextView m_clientAddress; } public MyCustomAdapter(LayoutInflater customInflater, List<ReadyTask> list, int layoutID) { this.customInflater =customInflater; this.list = list; this.layoutID = layoutID; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub final ReadyTask rTask = (ReadyTask)getItem(position); ViewHolder viewHolder = null; if (convertView == null) { convertView = customInflater.inflate(layoutID, null); viewHolder = new ViewHolder(); viewHolder.m_order=(TextView) convertView.findViewById(R.id.m_order); viewHolder.m_MeterID=(TextView) convertView.findViewById(R.id.m_MeterID); viewHolder.m_RFID=(TextView) convertView.findViewById(R.id.m_RFID); viewHolder.m_Area=(TextView) convertView.findViewById(R.id.m_Area); viewHolder.m_clientName=(TextView) convertView.findViewById(R.id.m_clientName); viewHolder.m_clientAddress=(TextView) convertView.findViewById(R.id.m_clientAddress); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); // Log.d("MyCustomAdapter", "旧的convertView,position=" + position); } if (list!=null&&list.size()>0){ viewHolder.m_order.setText(String.valueOf(position+1)); viewHolder.m_MeterID.setText(PublicConstant.MeterID+rTask.MeterID); viewHolder.m_RFID.setText(PublicConstant.RFID+rTask.RFID); viewHolder.m_Area.setText(PublicConstant.Area+rTask.Area); viewHolder.m_clientName.setText(PublicConstant.ClientName+rTask.ClientName); viewHolder.m_clientAddress.setText(PublicConstant.ClientAddress+rTask.ClientAddress); } if (position == selectItem) { convertView.setBackgroundColor(Color.CYAN); } else { convertView.setBackgroundColor(Color.TRANSPARENT); } return convertView; } public void setSelectItem(int selectItem) { this.selectItem = selectItem; } private int selectItem=-1; }
ListViewMeterReadinglist=(ListView) findViewById(R.id.MeterReadingList); customAdapter = new MyCustomAdapter(this, readyTaskList, R.layout.listview_item); MeterReadinglist.setAdapter(customAdapter);
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingBottom="2dp" android:paddingTop="2dp" > <TextView android:id="@+id/m_order" android:layout_width="36sp" android:layout_height="fill_parent" android:gravity="center" android:paddingRight="2dp" android:textSize="18sp" android:textStyle="bold" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="@color/gray" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="7" android:orientation="vertical" android:paddingBottom="2dp" android:paddingTop="2dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:layout_marginTop="1dp" android:orientation="horizontal" > <TextView android:id="@+id/m_RFID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="16sp" android:textStyle="bold" > </TextView> <TextView android:id="@+id/m_MeterID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="16sp" > </TextView> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:layout_marginTop="1dp" android:orientation="horizontal" > <TextView android:id="@+id/m_Area" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/m_clientName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/m_clientAddress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" /> </LinearLayout> </LinearLayout> <ImageView android:id="@+id/RightOrWrong" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:contentDescription="TODO" /> </LinearLayout> </LinearLayout>
customAdapter.setSelectItem(CURRENT_POSITION); customAdapter.notifyDataSetInvalidated();
我们可以看到setSelectItem是我们第二步自定义适配器里面的方法,用于获得当前的选中的Item项,然后接着调用notifyDataSetInvalidated();就行了,有人可能会发现此处不是用的notifyDataSetChanged(),的确这里我们需要的是对控件改变进行通知,而不是对其中的内容发生改变通知,详细可以了解notifyDataSetInvalidated()与notifyDataSetChanged()的相同不同点。
至次,整个逻辑完成,我们可以灵活控制ListView的某一项高亮显示