2015-12-24 105 views
1

我正在構建一個應用程序,其中有一個listview(待辦事項列表),此列表視圖正在從SQLite數據庫填充。這裏是適配器:Android ListView項目屬性在滾動時更改

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; 
       } 
      }); 

基於狀態值從數據庫返回並添加到ArrayList 狀態,任務被視爲完成未決。如果完成,edittext會變成綠色,並且會穿過,如果沒有,edittext會變成紅色。

出錯的事情是當滾動listview時,所有的edittext都是通過被擊中,請幫忙嗎?

+0

是什麼'Status'(注意是大寫首字母),什麼是'Status.add(位置, 「0」 );'應該這樣做? –

+0

status是一個使用SQLite數據庫填充每個任務狀態的ArrayList。 Status.add(position,「0」);在添加新任務時使用 –

+0

對我來說,您似乎更喜歡嘗試更新現有的itme狀態。此外,這個大寫狀態是單身還是什麼。這不是在適配器中使用的實例,它似乎是 –

回答

2

你需要糾正你的getView部分

在第一次使用這個

//Remove View vi=convertView; 

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

感謝他的工作 –

+0

@MonzerYaghi我的樂趣。 –