A TextView change applies to multiple TextViews using ListView and ArrayAdapter

前端 未结 1 1169
一个人的身影
一个人的身影 2020-12-22 05:48

I\'ve started working on a small project not to long ago, the main goal is to forge a way for me to keep track of my actions during the course of 100 weeks.

I\'m sti

相关标签:
1条回答
  • 2020-12-22 05:58

    Ok, your background is shuffling because when you scroll your ListView getView() is called and it consider your current position of TextView(as current view) and set background on it as it detect setBackground() method at onClick listener on it..

    First I recommend to create a Adapter class extends ArrayAdapter<?>

    Solution 1 :

    Use setTag() at onClick listener on your text view like..

    text.setTag(position);
    

    and above it use getTag() and put condition

    if(holder.text.getTag().equals(position)){
        holder.text.setBackgroundColor(Color.BLUE);
    }else{
        holder.text.setBackgroundColor(Color.WHITE);
    }
    

    Solution 2 :

    Added this to onCreate method

        ArrayList<String> _array = new ArrayList<String>();
        for(int i=0 ; i <1000; i ++){                       // 1000 value
            _array.add(i+"");                
        }
        list.setAdapter(new  MainAdapter(this, _array));        // pass you list here
    

    ArrayAdapter class :

    public class MainAdapter extends ArrayAdapter<String> {
    
        ArrayList<String> _st = new ArrayList<String>();
        ArrayList<Integer> check = new ArrayList<Integer>();
        Context _context;
        public MainAdapter(Context context,ArrayList<String> _st) {
            super(context,R.layout.main, _st);        // your inflate layout
            this._context = context;
            this._st = _st;
    
        }
        @Override
        public int getCount() {
            return _st.size();
        }
        @Override
        public long getItemId(int position) {
            return 0;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
    
            //---//
    
            // check if current position is there in arraylist
            if(checking(position)){
                holder.text.setBackgroundColor(Color.BLUE);
            }else{
                holder.text.setBackgroundColor(Color.WHITE);
            }
            holder.text.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // set background and put value in array list
                    holder.text.setBackgroundColor(Color.BLUE);
                    check.add(position);
    
                }
            });
            return convertView;
        }
    
        // this will check whether current position is there is array list or not and if it there it will break loop and return true
        public boolean checking(int position){
            boolean fine = false;
            for(int i=0; i<check.size();i++){
                if(position == check.get(i)){
                    fine = true;
                    break;
                }
            }
            return fine;
        }       
      }
      public class ViewHolder{
          TextView text;
      }
    }
    

    I don't how much I am ethical in this code...but as you have specified that you have 100 value.I have tested it on 1000 value and it worked

    I am not expert so let me know if I am wrong somewhere

    Hope it works !!!

    0 讨论(0)
提交回复
热议问题