简体   繁体   中英

Android listview items proprieties change when scrolling

I'm building an app in which there is a listview (To-Do-List), This listview is being filled from SQLite DB. Here is the adapter :

public class ToDoListAdapter extends BaseAdapter implements OnClickListener{

        private Context activity;
        List<String> content,status;
        private float x1,x2;
        static final int MIN_DISTANCE = 150;

        private LayoutInflater inflater=null;
        public ImageLoader imageLoader; 

        public ToDoListAdapter(Context a, List<String> n,List<String> fid) {
            activity = a;
            content=n;
            status=fid;


            inflater = (LayoutInflater)activity.
                                getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            // Create ImageLoader object to download and show image in list
            // Call ImageLoader constructor to initialize FileCache
            imageLoader = new ImageLoader(activity.getApplicationContext(),"p");
        }

        public int getCount() {
            return content.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        /********* Create a holder Class to contain inflated xml file elements *********/
        class ViewHolder{

            public EditText text;

        }

        @Override
        public int getViewTypeCount() {

            if (getCount() != 0)
                return getCount();

            return 1;
        }

//      @Override
//      public int getViewTypeCount() {
    //
//       return getCount();
//      }

        public View getView(final int position, View convertView, ViewGroup parent) {

            View vi=convertView;
            final ViewHolder holder;

            if(convertView==null){

                /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
                vi = inflater.inflate(R.layout.todolistview_row, null);

                /****** View Holder Object to contain tabitem.xml file elements ******/

                holder = new ViewHolder();
                holder.text = (EditText) vi.findViewById(R.id.listcontent);



               /************  Set holder with LayoutInflater ************/
                vi.setTag( holder );
            }
            else 
                holder=(ViewHolder)vi.getTag();


            holder.text.setText(content.get(position));
            holder.text.setTypeface(todofont);
            holder.text.setSelection(holder.text.getText().length());
//          
//          
            final Typeface font = Typeface.createFromAsset(vi.getContext().getAssets(),"fonts/MAXWELL REGULAR.ttf");
//          holder.text.setTypeface(font);


            if(status.get(position).equals("1"))
            {
                holder.text.setBackgroundColor(Color.parseColor("#0b9a0a"));
                holder.text.setPaintFlags(holder.text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            }
            if(status.get(position).equals("0"))
            {
                holder.text.setBackgroundColor(Color.parseColor("#fa2007"));
            }


            holder.text.setOnLongClickListener(new OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    // TODO Auto-generated method stub

                    showdialog(holder.text,"edittext","yes",position);


                //  Toast.makeText(getBaseContext(), Status.get(position),Toast.LENGTH_LONG).show();

                    return false;
                }
            });


            holder.text.setOnKeyListener(new OnKeyListener() {

                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    // TODO Auto-generated method stub
                    //k=0;
                    if(keyCode == 66)
                    {

                        String task=holder.text.getText().toString().replace("\n","");

                        k++;
                        if(k>1)
                        {

                            content.set(position,  holder.text.getText().toString().replace("\n",""));
                            Status.add(position,"0");
                            //ids.add(position,ids.get(position));

                            //content.add(0,additem.getText().toString());
                            //Status.add(0,"0");
                            adapter.notifyDataSetChanged();
//                          additem.setText("");
//                          additem.setVisibility(View.GONE);   

                            db.openDataBase();
                            db.update_table("todolist","Task", task,"_id",ids.get(position),"","");
                            db.close();


                            k=0;

                            hideSoftKeyboard();
                        }
                        else
                        {

                        }
                        //Toast.makeText(getBaseContext(), "Enter Clicked",Toast.LENGTH_LONG).show();

                    }
                    return false;
                }
            });

Based on the status value returned from the database and added to the ArrayList status , the task is considered as completed or pending . if completed the edittext is colored green and striked through , if not the edittext is colored red.

The thing that's going wrong is that when scrolling the listview, all the edittexts are being striked through , any help please ?

You need to rectify your getView section

At first Use this

//Remove View vi=convertView; 

   convertView=null; 
   if(convertView==null)
    {
    // Do your staff
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM