我有一个列表视图。每个列表视图都包含微调器,我想获得每个微调器的值。但它不起作用。如果列表视图不可滚动,则可以。
private void checkPrice(){
float total = 0;
Log.d(TAG,"This is qty: "+ quantity);
for (int i=0; i < mProductItems.size(); i++){
View view = mListView.getChildAt(i);
float eachTotal = 0;
float discountAmount = 0;
// Get Quantity
Spinner getQtyView = (Spinner) view.findViewById(R.id.spQty);
String getQty = getQtyView.getSelectedItem().toString();
Integer qty = Integer.parseInt(getQty);
// Get Line item amount view
TextView txvAmount = (TextView) view.findViewById(R.id.txvAmount);
// Get product by view position
OEDataRow eachProduct = (OEDataRow) mProductItems.get(i);
Integer eachPrice = eachProduct.getInt("retail_price");
// Check Discount
if (eachProduct.getBoolean("promotion_disc")){
float promo = (float) eachProduct.getInt("promotion_amount")/100;
Log.d(TAG,"this is promo: "+promo);
discountAmount = (float)( eachPrice * qty ) * promo;
}else {
discountAmount = 0;
}
eachTotal = (float)( eachPrice * qty ) - discountAmount;
txvAmount.setText(String.format("%.2f", eachTotal));
Log.d(TAG, "Each Total: " + eachTotal);
total = eachTotal + total;
Log.d(TAG,"Total: "+ total);
}
btnCheckout.setText("Checkout: "+String.format("%.2f", total));
}
感谢您的时间。
答案 0 :(得分:0)
我遇到了同样的问题。我会这样解决的。试试这个。这只是示例活动。
public class MainActivity extends Activity {
SpinnerAdapter ratingAdapter;
ListView listView;
List<String> listString;
ArrayAdapter<String> dataAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listString = new ArrayList<String>();
for(int listCount = 0; listCount < 20 ; listCount++){
listString.add("ListCount"+listCount);
}
List<String> spinnerData = new ArrayList<String>();
for (int i = 0; i < listString.size(); i++) {
spinnerData.add("MyTest="+i);
}
dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, spinnerData);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fillAdapter();
}
private void fillAdapter() {
ratingAdapter = new SpinnerAdapter(getApplicationContext());
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(ratingAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class ViewHolder {
Spinner spinner;
}
class SpinnerAdapter extends ArrayAdapter {
Context context;
HashMap<Integer,Integer> selectedItems = new HashMap<Integer, Integer>();
public SpinnerAdapter(Context context) {
super(context, R.layout.item_list_spinner_adaoter, listString);
this.context = context;
}
public int getCount() {
return listString.size();
}
public Object getItem(int position) {
return listString.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item_list_spinner_adaoter, null);
holder.spinner = (Spinner) convertView.findViewById(R.id.spinner1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.spinner.setAdapter(dataAdapter);
if ( selectedItems.get( position ) != null ) {
holder.spinner.setSelection( selectedItems.get( position ) );
}
holder.spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
selectedItems.put( position, arg2 );
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
return convertView;
}
}
}
在activity_main.xml中:
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:stackFromBottom="false"
android:transcriptMode="disabled" >
</ListView>
</LinearLayout>
In item_list_spinner_adaoter.xml:
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />