I have a listview with custom row which contains textview an ImageView and a button.
This is my layout:
Now If I add some text and select the color beside add text and click "+" the text and the color selected should be added to the listview.
Still here everything works fine.
But now my problem is each time I select the color and click "+" the color is changing for all the list rows.
I want only to change for the newly added text.
can anyone say me where am I going wrong?
This is my adapter:
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;
}
}
This is my Activity:
@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();
}
});
}
This is what I'm getting now:
The problem is that getView() is not only called if a new listitem is added, but always if the listitem is rendered again, for example if you call
So, you're setting the color of all listitems by calling this method in getView():
You've got to remove this line in your code!
The question is now: HOW do you set the color of the listitems? To solve this, you should think about the following: WHERE do you store the info about which color belongs to a listitem? Your listitems are just Strings! You would need objects like this:
This may look like a lot of work, but you'll have to store the information of the colors anywhere if you want to use them, right? If you've got further questions, comment :)
Edit: If you use the StringColorItem-Class I mentioned above you should use an Adapter for that class, like this:
}