【发布时间】:2015-06-10 11:15:04
【问题描述】:
我有一个带有自定义行的列表视图,其中包含 textview、ImageView 和一个按钮。
这是我的布局:
现在如果我添加一些文本并选择添加文本旁边的颜色并单击“+”文本和选择的颜色应该添加到列表视图中。
这里一切正常。
但现在我的问题是每次选择颜色并单击“+”时,所有列表行的颜色都会发生变化。
我只想更改新添加的文本。
谁能告诉我我哪里出错了?
这是我的适配器:
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
public MyCustomAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int pos) {
return list.get(pos);
}
@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);
btndelete.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something
list.remove(position); //or some other task
notifyDataSetChanged();
}
});
return view;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
}
这是我的活动:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_category);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00897B")));
getActionBar().setTitle(Html.fromHtml("<font color='#FFFFFF'>Register User</font>"));
coloredimage = (ImageView) findViewById(R.id.colorpicker);
colorPickerDialog = new ColorPickerDialog();
colorPickerDialog.initialize(R.string.dialog_title, new int[] { Color.CYAN, Color.LTGRAY, Color.BLACK, Color.BLUE, Color.GREEN, Color.MAGENTA, Color.RED, Color.GRAY, Color.YELLOW }, Color.YELLOW, 3, 2);
findViewById(R.id.colorpicker).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
colorPickerDialog.show(getSupportFragmentManager(), "colorpicker");
}
});
/** Reference to the button of the layout main.xml */
/** Setting a custom layout for the list activity */
lv = (ListView) findViewById(R.id.list);
list = new ArrayList<String>();
//instantiate custom adapter
list.add("Inbox");
list.add("Personal");
adapter = new MyCustomAdapter(list, this);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
colorPickerDialog.setOnColorSelectedListener(new OnColorSelectedListener() {
@Override
public void onColorSelected(int color) {
//Toast.makeText(AddTask.this, "selectedColor : " + color, Toast.LENGTH_SHORT).show();
selectedcolor=color;
coloredimage.setBackgroundColor(selectedcolor);
}
});
btnadd = (ImageView) findViewById(R.id.btnaddcategory);
/** Defining a click event listener for the button "Add" */
btnadd.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
txtcategoryname = (EditText) findViewById(R.id.txtaddcategory);
list.add(txtcategoryname.getText().toString());
txtcategoryname.setText("");
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}
这就是我现在得到的:
【问题讨论】:
-
@downvoter- 你能告诉我你为什么投反对票吗?在看到此内容的 14 人中没有发表任何评论,但您只是在没有任何不公平的建议或问题的情况下投了反对票。
-
在适配器中,您使用成员变量来设置图像颜色 colouredimage.setBackgroundColor(selectedcolor);这就是它出现问题的原因
标签: android listview imageview