该视图是疯狂的,我使用ListView.而且所有观点都会改变 [英] The views are crazy, I use ListView. And all views change

查看:59
本文介绍了该视图是疯狂的,我使用ListView.而且所有观点都会改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,每个视图都有不同的背景 当我上下滚动时,所有颜色都变得混乱,所有CheckBoxes标记都会改变,而当我擦除零件时,则会擦除其他内容并改变颜色.

I have a code that every view will have a different background When I scroll up and down then all the colors get confused and all the CheckBoxes markings change and when I erase a part, Then something else is erased and changed color.

告诉我是否需要完整的代码

tell me if you need The full code

这是更改背景颜色"的代码:

this the code of Changed Background color:

View v = List.getChildAt(index -
            List.getFirstVisiblePosition());

    if(v == null)
        return;
    ConstraintLayout LayoutT = v.findViewById(R.id.LayoutT);
    LayoutT.setBackgroundColor(Color.RED);

此删除代码:

 CheckBox currentCheckBox = (CheckBox) view.findViewById(mCheckBoxView);
    currentCheckBox.setChecked(mCheckBoxStates[csr.getPosition()]);
    currentCheckBox.setTag(new Long(csr.getLong(csr.getColumnIndex(DatabaseHandler.KEY_ID))));

    currentCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        final int position = fcsr.getPosition();
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mCheckBoxStates[position] = ((CheckBox) buttonView).isChecked();
            int restore_cursor_position = fcsr.getPosition();
            fcsr.moveToPosition(position);
            fcsr.moveToPosition(restore_cursor_position); //restore the Cursor's position
        }
    }); 
      another class:
 for (long id : mCheckedIdList) {

                mDBHandler.deleteRecord(id);

            }
            db.beginTransaction();
            db.setTransactionSuccessful();
            db.endTransaction();

            mCsr = mDBHandler.getAllRecords();
            mMCA.swapCursor(mCsr);

编辑

我一开始尝试做您建议的事情.效果很好,但是当我将其调整为代码时,只有一种颜色出现,它会根据最后出现的颜色或应该出现的第一种颜色发生变化

I tried to do what you suggested at first. And it worked perfectly but when I adjusted it to my code then only one color appears, it changes depending on the last color or the first one that should appear

 @Override
public View getView(int position, View view, ViewGroup parent) {
    View convertview = super.getView(position, view,parent);

    parent.setBackgroundColor(0xFF555555);
  //  LinearLayout ll = (LinearLayout) 
 convertview.findViewById(R.id.item_layout);

    int[] colours = new int[]{ 
            ContextCompat.getColor(mContext, R.color.Cyan)
  ,    ContextCompat.getColor(mContext, R.color.LowRed)
  };
    if (convertview != null) {
        DatabaseHandler db = new DatabaseHandler(mContext);
        int Count = List.getCount();
        String[] Status = new String[Count];

        for (int i = 0; i < Count; ) {
            Cursor c = db.getCursorStatus(i);
            if (c != null) {
                while (c.moveToNext()) {
                    Status[i] = c.getString(0);

                }
            }

            String mStatus[] = new String[3];
        mStatus[0] = "Monetary_Expenditure";
        mStatus[1] = "Financial income";
        mStatus[2] = "Cancellation";


        boolean first = Status[i] != null && Status[i].equals(mStatus[1]);
        boolean second = Status[i] != null && Status[i].equals(mStatus[0]);

        if (Status[i] != null && Status[i].equals(mStatus[1])) {
            convertview.setBackgroundColor(colours[1]);
        } else if (Status[i] != null && Status[i].equals(mStatus[0])) {
            convertview.setBackgroundColor(colours[0]);
        }
        else {convertview.setBackgroundColor(0xFF555555);}

        i++;
    }
   }
    return convertview;
  }

修改2

我更改了:

 if (convertview != null) {
    Cursor csr = getCursor(); 
    String status = csr.getString(0);
    int colourindex = 0;
    if (status.equals("Financial income") {
        colourindex = 1;
    }
    if (status.equals("Cancellation") {
        colourindex = 2;
    }
    convertview.setBackgroundColor(colours[colourindex]);       
}

收件人:

       if (convertview != null) {
        Cursor csrF = getCursor();

        DatabaseHandler db = new DatabaseHandler(mContext);
         i++;
        Cursor csr = db.getCursorStatus(i);
        String status = csr.getString(0);
        int colourindex = 0;
        if (status.equals("Financial income")) {
            colourindex = 1;
        }
        if (status.equals("Cancellation")) {
            colourindex = 2;
        }
        convertview.setBackgroundColor(colours[colourindex]);
 }

因为光标没有转到右行

我收到此错误:android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1

编辑3

 Cursor csr = getCursor();
        String status="";
        while (csr.moveToNext()) {
            status = csr.getString(4);
        }
        int colourindex = 3;
        if ("Monetary Expenditure".equals(status)){
        colourindex = 0;
        }
        else if ("Financial income".equals(status)) {
            colourindex = 1;
        }
        else if ("Cancellation".equals(status)) {
            colourindex = 2;
        }
        convertview.setBackgroundColor(colours[colourindex]);
        Log.i("Debug", "colour index is: "+colourindex);

推荐答案

在自定义适配器中,重写getView()方法并在该方法中设置颜色.

In your Custom Adapter override the getView() method and set the colour in that method.

签名是:-public View getView(int position, View view, ViewGroup parent).

  • position是要处理的视图在列表中的位置.
  • 视图是视图(布局)
  • 父级是ListView
  • 应返回修改后的视图.

一个更改背景颜色的示例(每x行是不同的颜色,其中x是颜色的数量):-

An example, that changes the background colour (every x rows is a different colour where x is the number of colours) :-

@Override
public View getView(int position, View view, ViewGroup parent) {
    View convertview = super.getView(position, view,parent);

    parent.setBackgroundColor(0xFF555555);
    //LinearLayout ll = (LinearLayout) convertview.findViewById(R.id.item_layout);

    int[] colours = new int[]{0xFFFF55FF,0xBBFF55FF,0x77FF55FF};
    if (convertview != null) {
        convertview.setBackgroundColor(colours[position % colours.length]);
        //ll.setBackgroundColor(colours[position % colours.length]);
    }
    return convertview;
}

  • parent.setBackgroundColor(0xFF555555);设置ListView的背景颜色(深灰色).
  • 注释掉的行是获取布局的一种替代方法,但这不是必需的
  • 在此示例中,颜色是相同的,但是透明度增加了,因此可以显示背景色(ListView/父级).
    • parent.setBackgroundColor(0xFF555555); sets the background colour (darkish grey) of the ListView.
    • Commented out lines are an alternative means to get get the layout but aren't required
    • In this example the colours are the same but the transparency is increases thus letting the background (ListView/parent) colour to show.
    • 结果(根据对上一个问题的答案中提供的代码进行修改):-

      The result (as per modification of the code provided in the answer provided to your previous question) :-

      您可以替代重写bindView方法,因为该方法已将View传递给它,尽管它没有传递位置.您也可以将两者结合使用.

      You could alternately override the bindView method, as that has the View passed to it, although it does not have the position passed. You could also use a combination of both.

      这是一个更完整的自定义光标适配器",它使用从数据库中提取的用户定义颜色来使用bindView相应地为每个项目着色.但是,这会更改可绘制对象的背景颜色.它用于信用卡管理(仍在进行中),因此ListView中的每个项目看起来都像一张信用卡. :-

      Here's a completer Custom Cursor Adapter that uses user defined colours extracted from the database to colour each item accordingly using bindView. However, this changes the drawable's background colour. It's for Credit Card management (work still in progress ), thus each item in the ListView looks like a Credit Card. :-

      public class CardListViewCursorAdapter extends CursorAdapter {
      
          Context mContext;
          Cursor mCurssor;
      
          CardListViewCursorAdapter(Context context, Cursor csr) {
              super(context, csr, 0);
              mContext = context;
      
          }
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              return super.getView(position, convertView, parent);
      
          }
      
          @Override
          public View newView(Context context, Cursor csr, ViewGroup viewGroup) {
      
              return LayoutInflater.from(context).inflate(
                      R.layout.standard_cards_listview,
                      viewGroup,
                      false
              );
          }
      
      
          @Override
          public void bindView(View view, Context context, Cursor csr) {
      
              TextView mCardName = view.findViewById(R.id.scl_cardname);
              TextView mCardNotes = view.findViewById(R.id.scl_cardnotes);
              LinearLayout mCardLayout = view.findViewById(R.id.scl_cardlayout);
              Drawable mDrawable = mCardLayout.getBackground();
      
              int cardcolour = (int) CardManageActivity.DEFAULT_CARDCOLOUR;
      
              long extracted_cardcolour = csr.getLong(
                      csr.getColumnIndex(
                              DBCardsTableConstants.CARDCOLOUR.getDBColumnName()
                      )
              );
              if ((extracted_cardcolour & DBCardsTableConstants.CARDSCOLOUR_FLAG) > 0) {
                  cardcolour = (int) extracted_cardcolour;
              }
      
              setShapeBackGroundColour(mDrawable, cardcolour);
      
              mCardName.setText(
                      csr.getString(
                              csr.getColumnIndex(
                                      DBCardsTableConstants.CARDNAME.getDBColumnName())
                      )
              );
              mCardNotes.setText(
                      csr.getString(
                              csr.getColumnIndex(
                                      DBCardsTableConstants.CARDNOTES.getDBColumnName()
                              )
                      )
              );
          }
      
          private void setShapeBackGroundColour(Drawable drawable, int colour) {
              if (drawable instanceof ShapeDrawable) {
                  ShapeDrawable sd = (ShapeDrawable) drawable;
                  sd.getPaint().setColor(colour);
              }
              if (drawable instanceof GradientDrawable) {
                  GradientDrawable gd = (GradientDrawable) drawable;
                  gd.setColor(colour);
              }
              if (drawable instanceof ColorDrawable) {
                  ColorDrawable cd = (ColorDrawable) drawable;
                  cd.setColor(colour);
              }
          }
      }
      

      这看起来像:-

      我按照您说的那样编辑了代码,尝试按照您的建议进行操作 第一的.它运行完美,但是当我将其调整为我的代码时 仅显示一种颜色,它会根据最后一种颜色或 应该出现的第一个为什么我尝试的是问题?和 非常感谢你的一切

      I edited the code as you said I tried to do what you suggested at first. And it worked perfectly but when I adjusted it to my code then only one color appears, it changes depending on the last color or the first one that should appear And why what I tried was problems? And thank you very much for everything

      我相信您的问题是,在getView(对于列表中的每个项目(分别为bindView)单独调用)视图是列表中该位置的视图的情况下,您正在遍历所有项目/视图,并基本上将所有项目更改为与最后一个项目相同.

      I believe that your problem is that within getView (which is called individually for each item in the list (as is bindView) the View being the View for that position in the list) you are the looping through all the items/views and basically changing ALL items to be as per the last item.

      即对于至少10个项目,getView将被调用(至少)10次.每次调用它都是针对要处理的单个视图/项目.

      i.e. getView will be called (at least) 10 times for a list of 10 items. Each time it is called it is for the individual view/item being processed.

      我相信您需要更改:-

      if (convertview != null) {
              DatabaseHandler db = new DatabaseHandler(mContext);
              int Count = List.getCount();
              String[] Status = new String[Count];
      
              for (int i = 0; i < Count; ) {
                  Cursor c = db.getCursorStatus(i);
                  if (c != null) {
                      while (c.moveToNext()) {
                          Status[i] = c.getString(0);
      
                      }
                  }
      
                  String mStatus[] = new String[3];
              mStatus[0] = "Monetary_Expenditure";
              mStatus[1] = "Financial income";
              mStatus[2] = "Cancellation";
      
      
              boolean first = Status[i] != null && Status[i].equals(mStatus[1]);
              boolean second = Status[i] != null && Status[i].equals(mStatus[0]);
      
              if (Status[i] != null && Status[i].equals(mStatus[1])) {
                  convertview.setBackgroundColor(colours[1]);
              } else if (Status[i] != null && Status[i].equals(mStatus[0])) {
                  convertview.setBackgroundColor(colours[0]);
              }
              else {convertview.setBackgroundColor(0xFF555555);}
      
              i++;
          }
         }
      

      收件人:-

          if (convertview != null) {
              Cursor csr = getCursor(); 
              String status = csr.getString(0);
              int colourindex = 0;
              if (status.equals("Financial income") {
                  colourindex = 1;
              }
              if (status.equals("Cancellation") {
                  colourindex = 2;
              }
              convertview.setBackgroundColor(colours[colourindex]);       
          }
      

      • 因此,您可以使用CursorAdapters getCursor()方法获取光标,然后将光标放置在相应位置.
      • 然后,您提取要为其构建视图的列表中项目的状态,即,列表中的每个项目都被调用getView().因此,如果列表中有10个项目,则至少调用10次.您似乎认为它只被调用过一次.
      • 根据状态(列0)确定颜色索引(0,1或2).
      • 根据列表中特定项目的状态设置背景颜色.
        • So you get the cursor using the CursorAdapters getCursor() method, the cursor will be positioned accordingly.
        • You then extract the status for the item in the List for which the View is being built i.e. getView() is called for every item in the list. So if you have 10 items in the List getView() is called at least 10 times. You appear to consider that it is called just once.
        • The colour index (0,1 or 2) is determined according to the status (column 0).
        • The background colour is set according to the status for the specific Item in the list.
        • 这篇关于该视图是疯狂的,我使用ListView.而且所有观点都会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆