Android esourceId,列出对象){ 超级(上下文、资源、textViewResourceId、对象); this.context=上下文; this.layoutResId=资源; } @凌驾 公共视图getView(int位置、视图转换视图、视图组父视图){ if(convertView==null) { LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务); convertView=充气机。充气(layoutResId,父项,false); } StringColorItem模型=this.getItem(位置); //处理按钮并添加onClickListeners 最终图像视图颜色图像=(图像视图)convertView.findViewById(R.id.colorpicker); 最终ImageView btndelete=(ImageView)convertView.findViewById(R.id.btndelete); //处理列表中的文本视图和显示字符串 TextView listItemText=(TextView)convertView.findViewById(R.id.txtCategory); listItemText.setText(model.getS()); setBackgroundColor(model.getC()); 返回视图; }
}问题在于适配器getview中的这一行Android esourceId,列出对象){ 超级(上下文、资源、textViewResourceId、对象); this.context=上下文; this.layoutResId=资源; } @凌驾 公共视图getView(int位置、视图转换视图、视图组父视图){ if(convertView==null) { LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务); convertView=充气机。充气(layoutResId,父项,false); } StringColorItem模型=this.getItem(位置); //处理按钮并添加onClickListeners 最终图像视图颜色图像=(图像视图)convertView.findViewById(R.id.colorpicker); 最终ImageView btndelete=(ImageView)convertView.findViewById(R.id.btndelete); //处理列表中的文本视图和显示字符串 TextView listItemText=(TextView)convertView.findViewById(R.id.txtCategory); listItemText.setText(model.getS()); setBackgroundColor(model.getC()); 返回视图; },android,listview,imageview,Android,Listview,Imageview,}问题在于适配器getview中的这一行 coloredimage.setBackgroundColor(selectedcolor); 调用adapter.notifyDatasetChanged()时,它将刷新listview中的所有视图。它会将所有图像的颜色设置为“选定颜色”。不要这样做,你必须记住每件物品的颜色。您可以通过将一个列表传递给同时保存文本和颜色的适配器来实现这一点 创建实体 Public class ListItemRowEntity { public String
coloredimage.setBackgroundColor(selectedcolor);
调用adapter.notifyDatasetChanged()时,它将刷新listview中的所有视图。它会将所有图像的颜色设置为“选定颜色”。不要这样做,你必须记住每件物品的颜色。您可以通过将一个列表传递给同时保存文本和颜色的适配器来实现这一点
创建实体
Public class ListItemRowEntity
{
public String CategoryText;
public int SelectedCOlor;
}
把这个传给你的适配器
public MyCustomAdapter(List<ListItemRowEntity> list, Context context) {
this.list = list;
this.context = context;
}
每当listview中发生更改时,请在“活动”中更新此列表,并致电notifydataset。@downvoter-您能告诉我您为什么进行了向下投票吗?在看到这一点的14个人中,没有任何评论,但你只是在没有任何建议或问题的情况下投了反对票,这是不公平的。在适配器中,你正在使用成员变量设置图像颜色image.setBackgroundColor(selectedcolor);这就是为什么它会变得problem@Doraemon-它向我显示了一个错误,无法在原语类型int上调用get(int),这里是coloredimage.setBackgroundColor(selectedcolor.get(position));然后将其转换为int;coloredimage.setBackgroundColor((int)selectedcolor.get(position))@Doraemon-仍然是相同的,我正在使用你提到的getters和setters方法。你能告诉我如何将其设置为行的特定位置吗element@coder你的问题现在解决了吗?如果更新没有帮助,您可以进一步询问,以便我改进我的答案。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.category_row, parent,false);
}
//Handle buttons and add onClickListeners
final ImageView coloredimage = (ImageView)view.findViewById(R.id.colorpicker);
final ImageView btndelete = (ImageView)view.findViewById(R.id.btndelete);
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.txtCategory);
listItemText.setText(list.get(position));
coloredimage.setBackgroundColor(selectedcolor.get(position));
btndelete.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something
list.remove(position); //or some other task
notifyDataSetChanged();
}
});
return view;
}
adapter.notifyDataSetChanged();
coloredimage.setBackgroundColor(selectedcolor);
public class StringColorItem {
private String s;
private int c;
public StringColorItem(String s, int c) {
this.s = s;
this.c = c;
}
//generate getters and setters
}
public class StringColorArrayAdapter extends ArrayAdapter<StringColorItem>{
private Context context;
private int layoutResId;
public StringColorArrayAdapter(Context context, int resource,
int textViewResourceId, List<StringColorItem> objects) {
super(context, resource, textViewResourceId, objects);
this.context = context;
this.layoutResId = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(layoutResId, parent, false);
}
StringColorItem model = this.getItem(position);
//Handle buttons and add onClickListeners
final ImageView coloredimage = (ImageView)convertView.findViewById(R.id.colorpicker);
final ImageView btndelete = (ImageView)convertView.findViewById(R.id.btndelete);
//Handle TextView and display string from your list
TextView listItemText = (TextView)convertView.findViewById(R.id.txtCategory);
listItemText.setText(model.getS());
coloredimage.setBackgroundColor(model.getC());
return convertView;
}
coloredimage.setBackgroundColor(selectedcolor);
Public class ListItemRowEntity
{
public String CategoryText;
public int SelectedCOlor;
}
public MyCustomAdapter(List<ListItemRowEntity> list, Context context) {
this.list = list;
this.context = context;
}
listItemText.setText(list.get(position).CategoryText);
coloredimage.setBackgroundColor(list.get(position).CategoryText.SelectedColor);