我的listView非常落后

时间:2014-08-08 04:46:58

标签: android listview android-listview android-edittext lag

我有一个MainActivity,CustomListAdapter,customlistItem.xml,mainlayout.xml。

我的ListView包含一个可编辑的EditText。

我正在尝试

  1. 如果editText中的文字被更改,
  2. 数据库更新,listView(listItem)更新。
  3. customListAdapter:

    public class subMyListAdapter extends BaseAdapter{
    
    Context context;
    LayoutInflater Inflater;
    ArrayList<subMyItem> arraySrc;
    int layout;
    EditText tvItem;
    UserHolder holder;
    myDBHelper myHelper;
    public subMyListAdapter(Context context, int layout, ArrayList<subMyItem> arraySrc, myDBHelper myHelper)
    {
        this.context = context;
        this.layout = layout;
        this.arraySrc = arraySrc;
        Inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        myHelper = this.myHelper;
    }
    
    
    public int getCount()
    {
        return arraySrc.size();
    }
    
    public String getItem(int position)
    {
        return arraySrc.get(position).list;
    }
    
    public long getItemId(int position)
    {
        return position;
    }
    
    
    
    public View getView(final int position, View conv, ViewGroup parent)
    {
        holder = null;
    
         if (conv == null) 
         {
           LayoutInflater inflater = ((Activity) context).getLayoutInflater();
           conv = inflater.inflate(layout, parent, false);
           holder = new UserHolder();
           holder.tvItem = (EditText)conv.findViewById(R.id.tvItem);
           conv.setTag(holder);
         }
         else 
         {
           holder = (UserHolder) conv.getTag();
         }
    
         if(holder == null)
         {
           holder = new UserHolder();
           holder.tvItem = (EditText)conv.findViewById(R.id.tvItem);
           conv.setTag(holder);
         }
    
         subMyItem user = arraySrc.get(position);
         holder.tvItem.setOnTouchListener(test);
         holder.tvItem.setTag(position);
         conv.setOnTouchListener(test);
    
        TextView count = (TextView)conv.findViewById(R.id.tvItemCount);
        count.setText(String.valueOf(arraySrc.size()));
        FrameLayout bg = (FrameLayout)conv.findViewById(R.id.bg);
    
    
        EditText tvItem = (EditText) conv.findViewById(R.id.tvItem);
        user = arraySrc.get(position);
        if(!tvItem.getText().toString().equals(user.list))
          tvItem.setText(user.list);
        tvItem.setTag(position);
    
        TextWatchert at = new TextWatchert();
        at.sqlId=user.id;
        at.position=position;
    
        tvItem.addTextChangedListener(at);
        return conv;
    }
    
    class TextWatchert implements TextWatcher
    {
        int sqlId;
        int position;
    
        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            if(openPositon != position)return;
    
            myHelper = new myDBHelper(context);
            SQLiteDatabase sqlDB = myHelper.getWritableDatabase();
            String update = "UPDATE clearTBL SET list='" + tvItem.getText().toString()+ "' WHERE id=" + sqlId + ";";
            try
            {
            sqlDB.execSQL(update);
            }
            catch (Exception e)
            {
              e.printStackTrace();
            }
    
            sqlDB.close();
            myListActivity.arrItem.get(position).list = s.toString();
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
    
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
    
        }
    
    }
    
    int openPositon = -1;
    
    View.OnTouchListener test=  new View.OnTouchListener() 
    {
        @Override
        public boolean onTouch(View view, MotionEvent event) 
        {
            if (view instanceof EditText) 
            {
                tvItem = (EditText) view;
                tvItem.setFocusable(true);
                tvItem.setFocusableInTouchMode(true);
                try
                {
                    openPositon = Integer.parseInt(tvItem.getTag().toString()); 
                }
                catch
                (Exception e)
                {}
    
            } else 
            {
                hideSoftKeyboard();
                view.requestFocus();
                UserHolder holder = (UserHolder) view.getTag();
                holder.tvItem.clearFocus();
                holder.tvItem.setMovementMethod(null);
                holder.tvItem.setFocusable(false);
                holder.tvItem.setFocusableInTouchMode(false);
                openPositon=-1;
            }
        return false;
        }
    };
    public void hideSoftKeyboard()
    {
        InputMethodManager imm = (InputMethodManager)context.getSystemService(context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(((Activity)context).getCurrentFocus().getWindowToken(), 0);
    }
     static class UserHolder 
     {
          EditText tvItem;
         }
    

    }

    我的代码工作正常。但是当我尝试编辑时,它太慢了,落后太多了。 滚动,触摸就可以了。只需编辑就需要很长时间。

    我认为addTextChangedListener代码存在问题。

    我如何减少滞后?

    修改

    好的,新代码是

    View.OnTouchListener test=  new View.OnTouchListener() 
    {
        @Override
        public boolean onTouch(View view, MotionEvent event) 
        {
            if (view instanceof EditText) 
            {
                 tVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
                 tVibrator.vibrate(50);
                tvItem = (EditText) view;
                tvItem.setFocusable(true);
                tvItem.setFocusableInTouchMode(true);
                try
                {
                    openPositon = Integer.parseInt(tvItem.getTag().toString()); 
                }
                catch
                (Exception e)
                {}
    
            } else 
            {
                hideSoftKeyboard();
                view.requestFocus();
                UserHolder holder = (UserHolder) view.getTag();
                holder.tvItem.clearFocus();
                holder.tvItem.setMovementMethod(null);
                holder.tvItem.setFocusable(false);
                holder.tvItem.setFocusableInTouchMode(false);
                openPositon=-1;
    
                try
                {
                    sPosition = tvItem.getTag().toString(); 
                }
                catch
                (Exception e)
                {}
                if(sPosition!=null)
                {
                    int pos = Integer.parseInt(sPosition);
                    myHelper = new myDBHelper(context);
                    SQLiteDatabase sqlDB = myHelper.getWritableDatabase();
                    subMyItem user = arraySrc.get(pos);
                    int a = user.id;
                    String update = "UPDATE clearTBL SET list='" + tvItem.getText().toString()+ "' WHERE id=" + a + ";";
                    try
                    {
                        sqlDB.execSQL(update);
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
    
                    sqlDB.close();
    
                    myListActivity.arrItem.get(pos).list = tvItem.toString();
                }
            }
        return false;
        }
    };
    

    现在所有代码都运行正常。谢谢大家!

1 个答案:

答案 0 :(得分:0)

您应该只在完成文本字段编辑后更新数据库。每当您进行文本更改时,您都会更新它。删除TextWatcher并在您对编辑感到满意后,将SQL移动到可以手动触发的位置。