动态更改列表视图行中的图像视图

Change imageview in listview row dynamically

我有一个带有自定义行的列表视图,其中包含文本视图、图像视图和按钮。

这是我的布局:

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

这里一切正常。

但现在我的问题是每次我 select 颜色并单击“+”时所有列表行的颜色都会改变。

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

谁能告诉我哪里错了?

这是我的适配器:

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

这是我的 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();
        }
    });

}

这是我现在得到的:

Array<Integer> selectedcolor; 放入您的 activity 和适配器中

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

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

问题是 getView() 不仅在添加新的列表项时被调用,而且在再次呈现列表项时也总是被调用,例如,如果您调用

adapter.notifyDataSetChanged();

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

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
}

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

编辑:如果您使用我上面提到的 StringColorItem-Class,您应该为此使用适配器 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;
}

}

问题出在适配器 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);

每当您的列表视图发生变化时,请在您的 activity 中更新此列表并调用 notifydataset。