【发布时间】:2014-04-14 09:46:25
【问题描述】:
我正在动态更改 listviewAdapter 行选定项。在适配器中 selectedItem 默认为 -1。
public static int selectedItem = -1; // no item selected by default
并且在适配器getview方法中调用highlightItem方法。
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.catalogue_row, null);
holder = new ViewHolder();
highlightItem(selectedItem,position,vi);
这是我的 highlightItem 方法。
private static void highlightItem(int selectedItem,int position, View result) {
System.out.println("selected item "+selectedItem);
if(position == selectedItem) {
// you can define your own color of selected item here
ViewHolder.lyCatalogueRow.setBackgroundColor(Color.parseColor(SharePreferenceController.getListViwHightlightColor()));
} else {
// you can define your own default selector here
ViewHolder.lyCatalogueRow.setBackgroundColor(Color.parseColor(SharePreferenceController.getListViwBackgroundColor()));
}
}
当我点击列表视图项时
private OnItemClickListener itemlistener = new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// TODO Auto-generated method stub
int item_position = position -1;
System.out.println("selectedItemposition "+item_position);
CatalogueEfficientAdapter.selectedItem=item_position;
catalogueEfficientAdapter.notifyDataSetChanged();
}
};
我的数组列表包含三个项目。当我单击第三项时,它被突出显示。现在,如果我单击第二个元素,则仅应突出显示第二个元素,但突出显示第三行。如何仅选择和突出显示所选项目
更新 问题是未更新的 highlightItem。
【问题讨论】:
-
你可以在日志中打印值
position和selectedItem并在此处发布...
标签: android