我有一个ListView,我想从SQLite中获取我保存的选项并将其显示为突出显示的选项,以便用户更新他的选择。
到目前为止,我在List中得到了我的值,我在listview中得到了相应的位置:
int ItemPos = mAdapter.getPosition(Alist.get(i).toString());
mAdapter.toggleSelection(ItemPos);
我的适配器如下:
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
{
holder = new ViewHolder();
view = inflater.inflate(R.layout.list_item, null);
holder.itemName = (TextView) view.findViewById(R.id.txtAnswer);
view.setTag(holder);
} else
{
holder = (ViewHolder) view.getTag();
}
holder.itemName.setText(list.get(position));
return view;
}
public void toggleSelection(int position){
selectView(position, !mSelectedItemsIds.get(position));
}
public void selectView(int position, boolean value) {
if (value) {
mSelectedItemsIds.put(position, value);
holder.itemName.setBackgroundColor(Color.parseColor("#FJFJFJ"));
}
else{
mSelectedItemsIds.delete(position);
}
notifyDataSetChanged();
}
当我运行时,我得到了
Attempt to read from field 'android.widget.TextView.AnswerListAdapter$ViewHolder.itemName' on a null object reference
如何更改商品的颜色?
谢谢
答案 0 :(得分:0)
在适配器传递布尔数据数组中,并在活动中设置其值代替以下两行
int ItemPos = mAdapter.getPosition(Alist.get(i).toString());
mAdapter.toggleSelection(ItemPos);
修改getView,如下所示
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
{
holder = new ViewHolder();
view = inflater.inflate(R.layout.list_item, null);
holder.itemName = (TextView) view.findViewById(R.id.txtAnswer);
view.setTag(holder);
} else
{
holder = (ViewHolder) view.getTag();
}
if(data.get(position))
holder.itemName.setBackgroundColor(Color.parseColor("#FJFJFJ"));
else
holder.itemName.setBackgroundColor(Color.parseColor("set normal color other wise you will face problem on scrolling"));
holder.itemName.setText(list.get(position));
return view;
}