2015-05-03 34 views
0

我有一個customListView與複選框和textview和我woul喜歡總結被檢查的值,並顯示它在textview上。customListView的複選框和textview,如何總結textview的值檢查?

這是我的代碼:

row2.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="?android:attr/activatedBackgroundIndicator" 
    android:descendantFocusability="blocksDescendants" 
    android:padding="1dp" > 
    <CheckBox 
     android:id="@+id/cbBox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentBottom="true"> 
    </CheckBox> 
    <TextView 
     android:id="@+id/tv_qta1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@id/tv_grm" 
     android:gravity="center_vertical|right" 
     android:paddingRight="5dp" 
     android:text="@string/v10000" 
     android:textColor="@android:color/holo_red_dark" 
     android:textSize="@dimen/size_edit_M" 
     android:textStyle="bold" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="10dp" 
     android:layout_alignParentBottom="true" /> 
    <TextView 
     android:id="@+id/tv_qta2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical|right" 
     android:paddingRight="5dp" 
     android:text="@string/v10000" 
     android:textColor="#000000" 
     android:textSize="@dimen/size_edit_M" 
     android:textStyle="bold" 
     android:layout_alignTop="@+id/tv_qta1" 
     android:layout_toLeftOf="@+id/tv_qta1" 
     android:layout_toStartOf="@+id/tv_qta1" 
     android:layout_marginLeft="10dp" 
     android:layout_alignParentBottom="true" /> 

    <TextView 
     android:id="@+id/tv_grm" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/grammi" 
     android:textColor="#000000" 
     android:textSize="@dimen/size_edit_M" 
     android:layout_alignParentTop="true" 
     android:gravity="center_vertical" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 

    <TextView 
     android:id="@+id/tv_prodotto" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="2dp" 
     android:text="@string/prodotto" 
     android:textColor="#000000" 
     android:textSize="@dimen/size_edit_M" 
     android:textStyle="italic" 
     android:layout_toRightOf="@+id/cbBox" 
     android:layout_alignParentTop="true" 
     android:gravity="center_vertical" 
     android:layout_toLeftOf="@+id/tv_qta2" 
     android:layout_toStartOf="@+id/tv_qta2" 
     android:layout_alignParentBottom="true" /> 

</RelativeLayout> 

的int actyvity我創建listviecustom

 if(userAdapterRct1 == null){ 
        userAdapterRct1 = new ItemsListAdapterRct(getActivity().getBaseContext(), userArrayRct1); 
        userListRct1 = (ListView) rootView.findViewById(R.id.ListView01); 
        userListRct1.setItemsCanFocus(true); 
        userAdapterRct1.notifyDataSetChanged(); 
        userListRct1.setAdapter(userAdapterRct1); 
        userAdapterRct1.notifyDataSetChanged(); 
       }else 
        userAdapterRct1.notifyDataSetChanged(); 


    if(userAdapterRct1 != null){ 
       userListRct1.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> adapterView, View v,int position, long id) { 
         // TODO Auto-generated method stub 
         RelativeLayout listItem = (RelativeLayout) v; 
         TextView clickedItemView = (TextView) listItem.findViewById(R.id.tv_prodotto); 
         CheckBox cbView = (CheckBox) listItem.findViewById(R.id.cbBox); 

         cbView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
          //@Override 
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
           //Integer rct = (Integer) buttonView.getTag();  
           // create the suf of items checked 
          } 
         }); 
        } 
       }); 
      } 

如何創建項目的總和檢查?

ItemsListAdapterRct.java

class ItemsListAdapterRct extends BaseAdapter { 

    private LayoutInflater vi; 
    private ArrayList<User> data = new ArrayList<User>(); 
    Context context; 

     public ItemsListAdapterRct(Context context, /*int layoutResourceId,*/ ArrayList<User> data) { 
     vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     this.data = data; 
     this.context = context; 
    } 

    /** Add white line */ 
    public void addItem(User item) { 
     data.add(item); 
     notifyDataSetChanged(); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     User p = getProduct(position); 
     if (convertView == null) { 

      holder = new ViewHolder(); 
      convertView = vi.inflate(R.layout.row2, parent, false);//null); 
      holder.name = (TextView) convertView.findViewById(R.id.tv_prodotto); 
      holder.qty_ing = (TextView) convertView.findViewById(R.id.tv_qta1); 
      holder.qty_risc = (TextView) convertView.findViewById(R.id.tv_qta2); 
      holder.cbView = (CheckBox) convertView.findViewById(R.id.cbBox); 
      convertView.setTag(holder); 
     }else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     if(holder.name != null) { 
      holder.name.setText(data.get(position).getName()); 
     } 
     if(holder.qty_ing != null) { 
      holder.qty_ing.setText(String.valueOf(data.get(position).getQtyIng())); 
     } 
     if(holder.qty_risc != null) { 
      holder.qty_risc.setText(String.valueOf(data.get(position).getQtyRis())); 
     } 
     if(holder.cbView != null) { 
      holder.cbView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
       //@Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        getProduct((Integer) buttonView.getTag()).box = isChecked; 
// 
//     // aggiorno il db 
//     DatabaseHelper dbRcp = new DatabaseHelper(context, ImportFragment.DB_DATABASE_STORE); 
//     int pos = (Integer) buttonView.getTag(); 
//     Ricetta p = getProduct(pos); 
//     String box = "0"; 
//     if(p.box) box = "1"; 
//     long res = dbRcp.update_Riscala(p.id, p.name, "0", box); 
//     // Don't forget to close database connection 
//     dbRcp.closeDB(); 
       } 
      }); 

      //cbBuy.setOnCheckedChangeListener(myCheckChangList); 
      holder.cbView.setTag(position); 
      holder.cbView.setChecked(p.box); 
     } 

     return convertView; 
    } 
    User getProduct(int position) { 
     return ((User) getItem(position)); 
    } 
// /** Sort shopping list by name ascending */ 
// public void sortByNameAsc() { 
//  Comparator<User> comparator = new Comparator<User>() { 
// 
//   @Override 
//   public int compare(User object1, User object2) { 
//    return ((String) object1.getName()).compareTo((String) object2.getName()); 
//   } 
//  }; 
//  Collections.sort(data, comparator); 
//  notifyDataSetChanged(); 
// } 
// 
// /** Sort shopping list by name descending */ 
// public void sortByNameDesc() { 
//  Comparator<User> comparator = new Comparator<User>() { 
// 
//   @Override 
//   public int compare(User object1, User object2) { 
//    return ((String) object2.getName()).compareTo((String) object1.getName()); 
//   } 
//  }; 
//  Collections.sort(data, comparator); 
//  notifyDataSetChanged(); 
// } 
// 
// /** Sort shopping list by date ascending */ 
// public void sortByTipoAsc() { 
//  Comparator<User> comparator = new Comparator<User>() { 
// 
//   @Override 
//   public int compare(User object1, User object2) { 
//    return ((String) object1.getTipoIng()).compareTo((String) object2.getTipoIng()); 
//   } 
//  }; 
//  Collections.sort(data, comparator); 
//  notifyDataSetChanged(); 
// } 
// 
// /** Sort shopping list by date descending */ 
// public void sortByTipoDesc() { 
//  Comparator<User> comparator = new Comparator<User>() { 
// 
//   @Override 
//   public int compare(User object1, User object2) { 
//    return ((String) object2.getTipoIng()).compareTo((String) object1.getTipoIng()); 
//   } 
//  }; 
//  Collections.sort(data, comparator); 
//  notifyDataSetChanged(); 
// } 
    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return data.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return data.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    /** Helper class acting as a holder of the information for each row */ 
    private class ViewHolder { 
     public CheckBox cbView; 
     public TextView name; 
     public TextView qty_risc; 
     public TextView qty_ing; 
    } 
} 

我想總結紅色簽署的價值,並把結果在底部的TextView

view image

回答

0

嘗試Check List,它展示着同樣的情形[列表與檢查顯示檢查的總和]

+0

是的,完美! thaks –

0

在你ItemsListAdapterRct申報INT VAL

int totalChecked = 0; 

那麼你onCheckedChanged

if(isChecked){ 
    totalChecked+=1; 
}else if(totalChecked!=0){ 
    totalChecked-=1; 
} 

而這個功能寫信給你onCheckedChanged

public int getTotalChecked(){ 
    return totalChecked; 
} 

然後調用它,當你需要;

+0

確定,但可以顯示總在ListView行之外的另一個TextView的,我修改我的問題和更新。 –