Android 更改Listview特定行的背景

Android 更改Listview特定行的背景,android,listview,Android,Listview,我正在开发一个包含项目列表的应用程序,在从列表视图中选择特定项目时,我想更改所选整行的背景色,但在实现此功能时,我的所有行背景色都已更改。请有人帮帮我。谢谢你。这是我的adapter.xml public class Adaptor_ListItem extends ArrayAdapter<MyItem> { public Context mContext; public ArrayList<MyItem> listItem;

我正在开发一个包含项目列表的应用程序,在从列表视图中选择特定项目时,我想更改所选整行的背景色,但在实现此功能时,我的所有行背景色都已更改。请有人帮帮我。谢谢你。这是我的adapter.xml

 public class Adaptor_ListItem extends ArrayAdapter<MyItem> {
        public Context mContext;
        public ArrayList<MyItem> listItem;
        public LayoutInflater inflater;
        public int position1=-1;

        public Adaptor_ListItem(Context context, int resource, List<MyItem> list,
                int selectedPos) {
            super(context, resource, list);
            // TODO Auto-generated constructor stub
            mContext = context;
            listItem = (ArrayList<MyItem>) list;
            inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            position1 = selectedPos;
        } // method ends

        public View getView(int pos, View convertView, ViewGroup parent) {
            View holder = convertView;
            if (holder == null) {
                holder = inflater.inflate(R.layout.adaptor_itemlist, null);
            }
            if (listItem.size() != 0) {

                TextView txtName = (TextView) holder
                        .findViewById(R.id.ListItem_txtName);
                TextView txtDays = (TextView) holder
                        .findViewById(R.id.ListItem_txtDays);
                TextView txtRecurring = (TextView) holder
                        .findViewById(R.id.ListItem_txtRecuring);
                MyItem objItem = listItem.get(pos);
                if (objItem != null) {
                    String strName = objItem.itemName;
                    String strRecurring = objItem.recurring + "";
                    String strDays = objItem.days;
                    int itemId = objItem.itemId;
                    // for checking which item has notified
                    if ((pos== position1) && holder!=null ) {
                        LinearLayout linearLayout = (LinearLayout) holder
                                .findViewById(R.id.linearListItem);
                        txtName.setBackgroundColor(Color.RED);
                        txtDays.setBackgroundColor(Color.RED);
                        txtRecurring.setBackgroundColor(Color.RED);
                        System.out.println("Adaptor_ListItem.getView()//// method block");
                    }
                    if (strDays == null) {
                        txtName.setText(strName);
                        txtDays.setText("0 Days");
                        txtRecurring.setText(strRecurring + "");
                    } else {
                        txtName.setText(strName);
                        txtDays.setText(strDays + " Days");
                        txtRecurring.setText(strRecurring + "");

                    }

                }
            }
            return holder;
        }// method ends
    } // final class ends

只需在adaptor_itemlist.xml文件中提到主布局的背景

为背景编写选择器:

item_selector.xml:

而不是:

txtName.setBackgroundColor(Color.RED);
txtDays.setBackgroundColor(Color.RED);
txtRecurring.setBackgroundColor(Color.RED);

在自定义适配器的getview中添加此行

listTextView.setTextColor(Color.parseColor("#aaaaaa"));

使用以下代码代替您的代码:

     public View getView(int pos, View convertView, ViewGroup parent) {
        View v = convertView;

        ViewHolder Holder;

    if (v == null) {
            v = inflater.inflate(R.layout.adaptor_itemlist, null);

            Holder = new ViewHolder();

            Holder.txtName = (TextView) v
                    .findViewById(R.id.ListItem_txtName);
            Holder.txtDays = (TextView) v
                    .findViewById(R.id.ListItem_txtDays);
            Holder.txtRecurring = (TextView) v
                    .findViewById(R.id.ListItem_txtRecuring);
            MyItem objItem = listItem.get(pos);
            v.setTag(Holder);
            }
      else
         holder=(ViewHolder)v.getTag();

      if (objItem != null) {
                String strName = objItem.itemName;
                String strRecurring = objItem.recurring + "";
                String strDays = objItem.days;
                int itemId = objItem.itemId;
                // for checking which item has notified
                if ((pos== position1)) {
                    LinearLayout linearLayout = (LinearLayout) v
                            .findViewById(R.id.linearListItem);
                    Holder.txtName.setBackgroundColor(Color.RED);
                    Holder.txtDays.setBackgroundColor(Color.RED);
                    Holder.txtRecurring.setBackgroundColor(Color.RED);
                    System.out.println("Adaptor_ListItem.getView()//// method block");
                }
        else
       {
                  // set Default Value
               }
                if (strDays.equals(null)) {
                    Holder.txtName.setText(strName);
                    Holder.txtDays.setText("0 Days");
                    Holder.txtRecurring.setText(strRecurring + "");
                } else {
                    Holder.txtName.setText(strName);
                    Holder.txtDays.setText(strDays + " Days");
                    Holder.txtRecurring.setText(strRecurring + "");

                }


        }
        return v;
    }// method ends
} // final class ends
而ViewHolder类别为:

 public static class ViewHolder()
 {
    public TextView txtName , txtDays, txtRecurring;
  }
这是适配器的getview方法中的以下行调用

yourView.setBackgroundColor(Color.parseColor(arrayColors[pos]));

当pos时,您需要将背景颜色设置回原始颜色=职位1。。或者if pos=position1语句的else子句。现在发生的事情是,一旦您将列表项设置为红色,它将被循环到另一行,但它将保持红色,因为它在新的getView调用中从未被取消设置

               int feedbackColor = Color.WHITE;  // or whatever your base color is
               if ((pos== position1) && holder!=null ) {
                    LinearLayout linearLayout = (LinearLayout) holder
                            .findViewById(R.id.linearListItem);
                    feedbackColor = Color.RED;

                }
                txtName.setBackgroundColor(feedbackColor);
                txtDays.setBackgroundColor(feedbackColor);
                txtRecurring.setBackgroundColor(feedbackColor);

实际上这不是我所需要的,Shayan pourvatan:实际上我是在为listview的特定项目添加通知,当收到通知时,我通过意图打开具有此listview的同一活动,并将所选行的位置添加到此意图,当此活动打开时,我获取此位置并将其传递给适配器构造函数,以便为此行设置一些背景色,我正在按照此条件ifpos==position1执行此操作,但它会更改所有行的背景色,这是我的问题,它应该只更改所选行的背景色。背景色在我的代码中正在更改,但不是针对单行,而是针对所有行更改,这就是我的实际问题,我的意思是说所有行都有不同的背景颜色。对吗?请获取颜色数组并根据您的位置将其传递到自定义适配器中。我所有行的背景颜色相同,但条件ifpos==position1变为真,不仅仅是特定的行背景颜色应该改变,你不认为这会在ArrayColor中得到索引吗?您在数组中只放置了三个项目,但pos可能大于3。这是基于要求数组中有多少颜色。这不是问题…任何人都可以理解我的应用程序需要的数组大小。?在动态列表中,您如何知道颜色的数量?pos值可以是0-list.size
int [] arrayColors={Color.GREEN,Color.BLUE,Color.Red}
yourView.setBackgroundColor(Color.parseColor(arrayColors[pos]));
               int feedbackColor = Color.WHITE;  // or whatever your base color is
               if ((pos== position1) && holder!=null ) {
                    LinearLayout linearLayout = (LinearLayout) holder
                            .findViewById(R.id.linearListItem);
                    feedbackColor = Color.RED;

                }
                txtName.setBackgroundColor(feedbackColor);
                txtDays.setBackgroundColor(feedbackColor);
                txtRecurring.setBackgroundColor(feedbackColor);