不点击复选复选框..但检查条件

时间:2014-06-25 18:08:53

标签: android listview checkbox

我正在尝试检查某些条件上的复选框,该条件将在其他活动上执行,单击包含listview的复选框。我将点击我的listview包含复选框,将我带到其他活动,我将填写表格并返回到我的列表视图,其状态取决于我将选中复选框。为此,我使用静态arraylist来存储listview的数据,我获得了成功状态,并使用此arraylist复选框进行检查。这是我的代码:

   public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        final FinalSubmitItem rowItem = getItem(position);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {  
            convertView = inflater.inflate(R.layout.customview_finalsubmit_itemdetails, null);
            holder = new ViewHolder();  
            holder.txtItemType = (TextView) convertView.findViewById(R.id.ItemtypeFinal);
            holder.txtCountry = (TextView) convertView.findViewById(R.id.txtcountryFinal);    
            holder.txtSerial = (TextView) convertView.findViewById(R.id.txtItemNumberFinal);
            holder.chck = (CheckBox) convertView.findViewById(R.id.itemCheck);
            convertView.setTag(holder);
        } else{
            holder = (ViewHolder) convertView.getTag();
            }

            holder.txtItemType.setText(rowItem.getItemType());
            holder.txtCountry.setText(rowItem.getItemCountry());
            holder.txtSerial.setText(rowItem.getItemNo());              

                 if(position == 0){
                    holder.chck.setChecked(false);
                 }
                for(int i=0; i<checkedInfo.size(); i++){
                    if(rowItem.getItemNo().equals(checkedInfo.get(i).getItemSerialVerified())){
                        rowItem.setItemCheck(true);
                        }
                    }
                if(rowItem.isItemCheck()){
                    holder.chck.setChecked(true);
                }


            convertView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    String itemType = rowItem.getItemType();
                    String itemCountry = rowItem.getItemCountry();
                    String itemSerial = rowItem.getItemNo();
                    int itemTypeId = rowItem.getItemId();
                    int pickupId = rowItem.getPickupId();
                    Intent inn = new Intent(context, FinalSubmitDetails.class);
                    inn.putExtra("itemType", itemType);
                    inn.putExtra("itemCountry", itemCountry);
                    inn.putExtra("itemSerial", itemSerial);
                    inn.putExtra("itemId", itemTypeId);
                    inn.putExtra("pickupId", pickupId);
                    context.startActivity(inn);
                }
            } );
            notifyDataSetChanged();
        return convertView;
    }     

在其他活动中:

            if(response.contains("Success.")){
                serialNumberVerf = serialNumber;
                itemIdVerified = itemId;
                actionIdVerified = actionId;
                ItemCheckedInfo info = new ItemCheckedInfo();
                info.setActionIdVerified(actionIdVerified);
                info.setItemIdVerified(itemIdVerified);
                info.setItemSerialVerified(serialNumberVerf);
                checkedInfo.add(info);

            Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
            onBackPressed();

但是它没有用,我在一个声明了customview类的类中声明了这个arraylist。

2 个答案:

答案 0 :(得分:1)

我是这样做的。只需在getview()方法中编辑一行:

((Activity) context).startActivityForResult(inn, 1);

成功的确如此:

Intent returnIntent = new Intent();
returnIntent.putExtra("position", position);
setResult(RESULT_OK,returnIntent);
finish();

最后在主要活动中做到了这一点:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
            int positionResult=data.getIntExtra("position",0);
            itemInfo.get(positionResult).setItemCheck(true);
            item.notifyDataSetChanged();
        }
}
} 

答案 1 :(得分:0)

您需要覆盖setOnCheckedChangeListener

http://developer.android.com/reference/android/widget/CompoundButton.html#setOnCheckedChangeListener(android.widget.CompoundButton.OnCheckedChangeListener)

[编辑]

实际上我需要点击listview来执行这些事情。为什么我没有使用它
您可以为其覆盖onItemClickListener