1

我正在使用自定义列表视图来显示来自 sqlite 数据库的数据。我的列表项有文本视图、按钮、复选框。请向我推荐将执行以下任务的适配器,

  1. 所有字符串值都在 equalesnt 文本视图中更新
  2. 根据布尔值,复选框应选中/取消选中。
  3. 当我单击按钮时,它会将我带到新屏幕并传递按钮的 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的listview类中,

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

1 回答 1

0

您应该为此使用 BaseAdapter。创建一个名为 MyAdapter 的类并扩展 BaseAdater。在该类中覆盖所需的方法。并在 Class 的 getView 方法中膨胀您的 xml 文件并根据位置放置数据。

如果值来自数据库,则获取 Cursor 并对其进行迭代并获得所需的值。

希望这可以帮助

于 2012-11-29T14:57:24.500 回答