내 코드에 두 가지 오류가 있습니다. 해결 방법을 모르겠습니다.통화가 들어있는 문자열을 추가 할 수 없습니다.
표시하고 해결 방법을 알려주십시오. 내가하지만, 다른 오류가 표시 될 수 있습니다로를 해결하기 위해 노력
public class Food {
String price = null;
String name = null;
boolean selected = false;
public Food(String price, String name, boolean selected) {
super();
this.price = price;
this.name = name;
this.selected = selected;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
public class MainActivity extends Activity {
MyCustomAdapter dataAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Generate list View from ArrayList
displayListView();
checkButtonClick();
}
private void displayListView() {
//Array list of foods
ArrayList<Food> foodList = new ArrayList<Food>();
Food food = new Food("15 SAR", "Chicken Meal", false);
foodList.add(food);
food = new Food("10 SAR", "Sliced Chicken", false);
foodList.add(food);
food = new Food("20 SAR", "Sandwich Chicken", false);
foodList.add(food);
food = new Food("10 SAR", "Hot Chicken", false);
foodList.add(food);
food = new Food("6 SAR", "Grilled potatoes", false);
foodList.add(food);
food = new Food("2 SAR", "Pepsi", false);
foodList.add(food);
food = new Food("17 SAR", "Fish Meal", false);
foodList.add(food);
//create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this,
R.layout.food_info, foodList);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
private class MyCustomAdapter extends ArrayAdapter<Food> {
private ArrayList<Food> foodList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Food> foodList) {
super(context, textViewResourceId, foodList);
this.foodList = new ArrayList<Food>();
this.foodList.addAll(foodList);
}
private class ViewHolder {
TextView price;
CheckBox name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.food_info, null);
holder = new ViewHolder();
holder.price = (TextView) convertView.findViewById(R.id.price);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Food food = foodList.get(position);
holder.price.setText(" (" + food.getPrice() + ")");
holder.name.setText(food.getName());
holder.name.setChecked(food.isSelected());
holder.name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
food.setSelected(b);
}
});
holder.name.setTag(food);
return convertView;
}
}
private void checkButtonClick() {
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.invoic_page);
int totalPrice = 0;
for (Food f : foodList) {
if (f.isSelected) {
totalPrice += f.getPrice();
}
//totalPrice variable now has the total of all selected items
}
}
});
}
}
: 여기
는 내 응용 프로그램의 코드입니다 코드의 다른 장소 그래서 나는 최고의 솔루션을 원한다.
만들 변수 최종 초에 사용 정수 .parseInt (yourstring); –
깜박이는 텍스트 커서를 오류 위에 놓고 ALT + ENTER를 누르십시오 -> ENTER – Linxy
이것은 다중 통화 시스템입니까? 그렇지 않은 경우 가격에 문자열을 사용하고 각 문자열에서 통화를 반복 할 필요가 없습니다. – weston