我正在開發一個應用程序,它具有項目列表,並從列表視圖中選擇特定項目,我想更改所選整行的背景顏色,但在實現此操作時,我的所有行背景顏色改變。請有人幫助我。感謝Advace。這裏是我的Adaptor.xml更改列表視圖的特定行的背景
public class Adaptor_ListItem extends ArrayAdapter<MyItem> {
public Context mContext;
public ArrayList<MyItem> listItem;
public LayoutInflater inflater;
public int position1=-1;
public Adaptor_ListItem(Context context, int resource, List<MyItem> list,
int selectedPos) {
super(context, resource, list);
// TODO Auto-generated constructor stub
mContext = context;
listItem = (ArrayList<MyItem>) list;
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
position1 = selectedPos;
} // method ends
public View getView(int pos, View convertView, ViewGroup parent) {
View holder = convertView;
if (holder == null) {
holder = inflater.inflate(R.layout.adaptor_itemlist, null);
}
if (listItem.size() != 0) {
TextView txtName = (TextView) holder
.findViewById(R.id.ListItem_txtName);
TextView txtDays = (TextView) holder
.findViewById(R.id.ListItem_txtDays);
TextView txtRecurring = (TextView) holder
.findViewById(R.id.ListItem_txtRecuring);
MyItem objItem = listItem.get(pos);
if (objItem != null) {
String strName = objItem.itemName;
String strRecurring = objItem.recurring + "";
String strDays = objItem.days;
int itemId = objItem.itemId;
// for checking which item has notified
if ((pos== position1) && holder!=null) {
LinearLayout linearLayout = (LinearLayout) holder
.findViewById(R.id.linearListItem);
txtName.setBackgroundColor(Color.RED);
txtDays.setBackgroundColor(Color.RED);
txtRecurring.setBackgroundColor(Color.RED);
System.out.println("Adaptor_ListItem.getView()//// method block");
}
if (strDays == null) {
txtName.setText(strName);
txtDays.setText("0 Days");
txtRecurring.setText(strRecurring + "");
} else {
txtName.setText(strName);
txtDays.setText(strDays + " Days");
txtRecurring.setText(strRecurring + "");
}
}
}
return holder;
}// method ends
} // final class ends
實際上這不是我需要的, –
其中您正在更改..可以更新您的onItemClickListener()。 – user543
Shayan pourvatan:實際上我爲listview的特定項目添加通知,當接收到notifcation時,我打開具有此listview的同一活動,並通過intent添加選定行的位置,當此活動打開時,我獲得此位置並通過它適配器的構造函數設置一些背景顏色到這一行,我通過這種方式來處理if(pos == position1),但它改變了所有行的背景顏色,這是我的問題,它應該只改變背景顏色選定的行。 –