适配器的Getview方法。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_items, parent, false);
tvName = (TextView) rowView.findViewById(R.id.tvName);
tvPrice = (TextView) rowView.findViewById(R.id.tvPrice);
id = Integer.parseInt(values.get(position));
tvName.setText(database.getItemName(id));
spQuantity = (Spinner) rowView.findViewById(R.id.spQuantity);
spQuantity.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long arg3) {
// TODO Auto-generated method stub
String selectedQuantity = parent.getItemAtPosition(position)
.toString();
int selectedPrice = database.getPrice(id, selectedQuantity);
Log.d("selectedPrice",""+selectedPrice); //showing correct value
tvPrice.setText("Rs. " + selectedPrice); //not working
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
quantityList = database.getQuantityList(id);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context,
R.layout.spinner_item_black, quantityList);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spQuantity.setAdapter(dataAdapter);
buttonAddtoCart = (LinearLayout) rowView
.findViewById(R.id.buttonAddtoCart);
buttonAddtoCart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (database.checkDuplicateCart(id)) {
Toast.makeText(context, "Already added to cart",
Toast.LENGTH_SHORT).show();
} else {
database.addtoCart(id);
Toast.makeText(context, "Added to cart", Toast.LENGTH_SHORT)
.show();
}
}
});
return rowView;
}
Log显示从数据库中获取的正确值。其他一切都很好。但是TextView没有改变它的价值。 listview的TextView的最后一行只是第一次改变它的值。 但是当我们等待一段时间(大约30秒)时,文本视图会发生变化。为什么这需要这么多时间?
答案 0 :(得分:0)
有时候listview中的Spinner将无法正常工作,尝试将所选值保存在局部变量中并在文本视图中设置值,请尝试我的下面的答案 让我知道它是否正常工作
//必须只初始化一次
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//尝试在getView中每次初始化TextView。我已尝试使用Base适配器工作正常
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String Spinnerselecteditem;
View rowView = inflater.inflate(R.layout.list_items, parent, false);
TextView tvName = (TextView) rowView.findViewById(R.id.tvName);
TextView tvPrice = (TextView) rowView.findViewById(R.id.tvPrice);
id = Integer.parseInt(values.get(position));
tvName.setText(database.getItemName(id));
Spinner spQuantity = (Spinner) rowView.findViewById(R.id.spQuantity);
quantityList = database.getQuantityList(id);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context,
R.layout.spinner_item_black, quantityList);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spQuantity.setAdapter(dataAdapter);
spQuantity.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long arg3) {
// TODO Auto-generated method stub
String selectedQuantity = parent.getItemAtPosition(position)
.toString();
int selectedPrice = database.getPrice(id, selectedQuantity);
Log.d("selectedPrice",""+selectedPrice); //showing correct value
Spinnerselecteditem = String.ValueOf(selectedPrice); //save it in a local variable
tvPrice.setText("Rs. " + Spinnerselecteditem);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
LinearLayout buttonAddtoCart = (LinearLayout) rowView
.findViewById(R.id.buttonAddtoCart);
buttonAddtoCart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (database.checkDuplicateCart(id)) {
Toast.makeText(context, "Already added to cart",
Toast.LENGTH_SHORT).show();
} else {
database.addtoCart(id);
Toast.makeText(context, "Added to cart", Toast.LENGTH_SHORT)
.show();
}
}
});
return rowView;
}