2012-11-29 53 views
1

我使用自定義列表視圖來顯示來自sqlite數據庫的數據。我的列表項目 有textviews,按鈕,複選框。請給我建議基於布爾值的複選框應該是選中/取消該適配器將執行以下任務,列表視圖與複選框和按鈕使用Android中的陣列適配器

  1. 所有的字符串值更新equalesnt textviews
  2. 當我點擊按鈕,它會帶我到新的屏幕,並通過按鈕的ID。

    請告訴我現在該做什麼。新的想法也受到歡迎。

    按照我的代碼。

    public class GuestListAdapter extends BaseAdapter implements OnClickListener{ 
         Context context; 
         ArrayList<String> arrayListFirstName; 
         ArrayList<String> arrayListLastName; 
         ArrayList<String> arrayListGuests; 
         ArrayList<String> arrayCustomOne; 
         ArrayList<String> arrTicketId; 
         private ArrayList<Boolean> checks=new ArrayList<Boolean>(); 
         public GuestListAdapter(Context mcontext, ArrayList<String> arrListFN, 
           ArrayList<String> arrListLN, ArrayList<String> arrListGuest, 
           ArrayList<String> arrTicketID,ArrayList<String> arrCustOne) { 
          arrayListFirstName = arrListFN; 
          arrayListLastName = arrListLN; 
          arrayListGuests = arrListGuest; 
          arrayCustomOne = arrCustOne; 
          context = mcontext; 
    
          for (int i = 0; i < arrayListFirstName.size(); i++) { 
           checks.add(i, false); 
          } 
         } 
    
         @Override 
         public int getCount() { 
          // TODO Auto-generated method stub 
          return arrayListFirstName.size(); 
         } 
    
         @Override 
         public Object getItem(int position) { 
          // TODO Auto-generated method stub 
          return arrayListFirstName.get(position); 
         } 
    
         @Override 
         public long getItemId(int position) { 
          // TODO Auto-generated method stub 
          return position; 
         } 
    
         @Override 
         public View getView(int position, View view, ViewGroup parent) { 
          // TODO Auto-generated method stub 
          if (view == null) { 
           LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
           view = inflater.inflate(R.layout.guest_list_item, parent, false); 
          } 
    
          final String dataModel = (String) arrayListFirstName.get(position); 
    
          TextView txtFirstName = (TextView) view 
            .findViewById(R.id.txt_firstname); 
          txtFirstName.setText(arrayListFirstName.get(position)); 
          TextView txtLastName = (TextView) view.findViewById(R.id.txt_lastname); 
          txtLastName.setText(arrayListLastName.get(position)); 
          TextView txtGuestCount = (TextView) view 
            .findViewById(R.id.txt_guest_count); 
          txtGuestCount.setText(arrayListGuests.get(position)); 
          Button btnInfo = (Button) view.findViewById(R.id.btn_info); 
          // btnInfo.setId(position); 
          CheckBox checkBoxCheckins = (CheckBox) view 
            .findViewById(R.id.checkBoxIsCheckedIn); 
          checkBoxCheckins.setTag(Integer.valueOf(position)); 
    
          btnInfo.setOnClickListener(new OnClickListener() { 
    
           @Override 
           public void onClick(View v) { 
            // int id = Integer.parseInt(v.getTag().toString()); 
    
            // Log.d("", "Clicked ID "+id); 
            Intent intent = new Intent(context, InfoScreen.class); 
            context.startActivity(intent); 
            System.out.println("Adap button **********"); 
    
           } 
          }); 
          checkBoxCheckins 
            .setOnCheckedChangeListener(new OnCheckedChangeListener() { 
    
             @Override 
             public void onCheckedChanged(CompoundButton buttonView, 
               boolean isChecked) { 
              System.out.println("Adap checked **********"); 
              Integer integ = (Integer)buttonView.getTag(); 
    
               checks.set(integ.intValue(), false); 
             } 
            }); 
          if (checks.get(position)) { 
           checkBoxCheckins.setChecked(checks.get(position)); 
            // do some stuff if wanted.. 
           } else { 
            checkBoxCheckins.setChecked(checks.get(position)); 
            // do some stuff if wanted.. 
           } 
          return view; 
         } 
    
         @Override 
         public void onClick(View view) { 
          // TODO Auto-generated method stub 
          Integer index = (Integer)view.getTag(); 
          boolean state = checks.get(index.intValue()); 
    
          checks.set(index.intValue(), !state); 
         } 
    
        } 
    
    
    this is my setter class 
    
    public class GuestList { 
    
        String mFirstName; 
        String mLastName; 
        String mTicketI; 
        String mGuestCount; 
    
        public String getmFirstName() { 
         return mFirstName; 
        } 
    
        public void setmFirstName(String mFirstName) { 
         this.mFirstName = mFirstName; 
        } 
    
        public String getmLastName() { 
         return mLastName; 
        } 
    
        public void setmLastName(String mLastName) { 
         this.mLastName = mLastName; 
        } 
    
        public String getmTicketI() { 
         return mTicketI; 
        } 
    
        public void setmTicketI(String mTicketI) { 
         this.mTicketI = mTicketI; 
        } 
    
        public String getmGuestCount() { 
         return mGuestCount; 
        } 
    
        public void setmGuestCount(String mGuestCount) { 
         this.mGuestCount = mGuestCount; 
        } 
    
    } 
    

    在列表視圖類的ArrayList發送這樣,

    cursorAdapter = new GuestListAdapter(GuestListScreen.this, 
          arrFirstName, arrLastName, arrGuests, arrTicketId, arrCustomOne); 
        listViewGuests.setAdapter(cursorAdapter); 
    

回答

0

你應該使用BaseAdapter這一點。創建一個類調用MyAdapter並擴展BaseAdater。在該類中重寫所需的方法。並在類的getView方法中膨脹你的xml文件並根據該位置放置數據。

如果值來自數據庫,然後採取光標並迭代它,並獲得所需的值。

希望這有助於

+0

HI我說我的代碼,請現在change.Here我想改變基礎上,真或假的複選框什麼告訴我。 – Dhamodharan

+0

添加將數據提供給此適配器的代碼。像光標或其他東西 –

相關問題