动态更改listview行中的imageview

时间:2015-06-10 11:15:05

标签: android listview imageview

我有一个自定义行的列表视图,其中包含一个ImageView和一个按钮的textview。

这是我的布局:

enter image description here

现在如果我添加一些文字并选择添加文字旁边的颜色并点击“+”,文本和所选颜色应该添加到列表视图中。

仍然在这里一切正常。

但现在我的问题是每次选择颜色并点击“+”时,所有列表行的颜色都在变化。

我只想更改新添加的文字。

谁能说我哪里出错了?

这是我的适配器:

  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();
        }
    });

}

这就是我现在所得到的:

enter image description here

3 个答案:

答案 0 :(得分:1)

问题是getView()不仅在添加新的listitem时被调用,而且如果再次呈现listitem,则总是被调用,例如,如果你调用

adapter.notifyDataSetChanged();

因此,您可以通过在getView()中调用此方法来设置所有列表项的颜色:

coloredimage.setBackgroundColor(selectedcolor);

您必须在代码中删除此行!

现在的问题是: 你如何设置listitems的颜色? 要解决这个问题,您应该考虑以下事项: 您在哪里存储有关哪种颜色属于listitem的信息? 你的listitems只是字符串! 你需要这样的对象:

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
}

这可能看起来很多,但如果您想使用它们,您必须将颜色信息存储在任何地方,对吧? 如果您还有其他问题,请发表评论:)

编辑:如果您使用上面提到的StringColorItem-Class,您应该为该类使用适配器,如下所示:

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;
}

}

答案 1 :(得分:0)

在您的活动和适配器中取Array<Integer> selectedcolor;

并在颜色选择事件中添加所选颜色

colorPickerDialog.setOnColorSelectedListener(new OnColorSelectedListener() {

        @Override
        public void onColorSelected(int color) {
            //Toast.makeText(AddTask.this, "selectedColor : " + color, Toast.LENGTH_SHORT).show();
        selectedcolor.add(color);
        coloredimage.setBackgroundColor(selectedcolor);

        }
    });

并从selectedcolor列表中设置颜色并在getView方法中设置

 @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; 
        }

答案 2 :(得分:0)

问题在于适配器getview中的这一行

coloredimage.setBackgroundColor(selectedcolor);

当您调用adapter.notifyDatasetChanged()时,它将刷新列表视图中的所有视图。它会将所有图像的颜色设置为所选颜色。而不是这样做,你必须记住每个项目的颜色。您可以通过将列表传递给同时包含文本和颜色的适配器来完成此操作。

创建实体

Public class ListItemRowEntity
{
   public String CategoryText;
   public int SelectedCOlor;
}

将其传递给适配器

 public MyCustomAdapter(List<ListItemRowEntity> list, Context context) { 
            this.list = list; 
            this.context = context; 
        } 

在getView中

listItemText.setText(list.get(position).CategoryText);  
coloredimage.setBackgroundColor(list.get(position).CategoryText.SelectedColor);

每当您的列表视图中发生更改时,请在您的活动中更新此列表并调用notifydataset。