Answer the question
In order to leave comments, you need to log in
How to add numbers from listview?
I have a list in which there is a textview where prices are indicated and I need to give a general price tag for all goods ... the list is taken from the database, what can be done there? I use Sugar ORM to simplify
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_korzina);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
summa = (TextView) findViewById(R.id.zakaz);
button = (ImageButton) findViewById(R.id.button);
button2 = (ImageButton) findViewById(R.id.button2);
button3 = (ImageButton) findViewById(R.id.button3);
menu = (ImageButton) findViewById(R.id.menu);
imageButton3 = (ImageButton) findViewById(R.id.imageButton3);
imageButton4 = (ImageButton) findViewById(R.id.imageButton4);
imageButton5 = (ImageButton) findViewById(R.id.imageButton5);
imageButton6 = (ImageButton) findViewById(R.id.imageButton6);
zaka = (Button) findViewById(R.id.zaka);
button.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
menu.setOnClickListener(this);
imageButton3.setOnClickListener(this);
imageButton4.setOnClickListener(this);
imageButton5.setOnClickListener(this);
imageButton6.setOnClickListener(this);
zaka.setOnClickListener(this);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ArrayList<Korz> mData = (ArrayList<Korz>) Korz.listAll(Korz.class);
mListView = (ListView) findViewById(R.id.listView);
mAdapter = new MyAdapater(this, R.layout.item_korz, R.layout.item_action_korz, mData);
mListView.setAdapter(mAdapter);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.akcii) {
Intent intent12 = new Intent(this, Akcii.class);
startActivity(intent12);
finish();
// Handle the camera action
} else if (id == R.id.oplata) {
Intent intent22 = new Intent(this, Oplata.class);
startActivity(intent22);
finish();
} else if (id == R.id.dostavka) {
Intent intent32 = new Intent(this, Dostavka.class);
startActivity(intent32);
finish();
} else if (id == R.id.onas) {
Intent intent42 = new Intent(this, O_nas.class);
startActivity(intent42);
finish();
} else if (id == R.id.inmoney) {
Intent intent52 = new Intent(this, InMoney.class);
startActivity(intent52);
finish();
} else if (id == R.id.zakaz) {
Intent intent62 = new Intent(this, Zakaz.class);
startActivity(intent62);
finish();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
Intent intent12 = new Intent(this, Cold.class);
startActivity(intent12);
finish();
break;
case R.id.button2:
List<Point> allPoints = Point.listAll(Point.class);
summa.setText(allPoints.toString());
break;
case R.id.button3:
Intent intent2 = new Intent(this, Izbrannoe.class);
startActivity(intent2);
finish();
break;
case R.id.menu:
Intent intent3 = new Intent(this, StartMenu.class);
startActivity(intent3);
finish();
break;
case R.id.zaka:
Point point = new Point( "point", 12);
point.save();
break;
case R.id.imageButton3:
Korz korz = new Korz("Запеченые с сыром", 219, getResources().getIdentifier(String.valueOf(R.drawable.zapssir), "drawable", getApplicationContext().getPackageName()), "копчёный угорь, крабовая палочка , огурец, тобика красная, сливочный сыр");
korz.save();
Intent intent = new Intent(this, Korzina.class);
startActivity(intent);
finish();
break;
default:
break;
}
}
class MyAdapater extends SwipeLayoutAdapterKorz<Korz> {
private List<Korz> _data;
ArrayList objects;
public MyAdapater(Activity context, int contentViewResourceId, int actionViewResourceId, List<Korz> objects) {
super(context, contentViewResourceId, actionViewResourceId, objects);
_data = objects;
}
//实现setContentView方法
@Override
public void setContentView(View contentView, int position, HorizontalScrollView parent) {
Korz p = ((Korz) getItem(position));
TextView pricecent = (TextView) contentView.findViewById(R.id.pricecent);
pricecent.setText(p.price*5 + "руб");
TextView tv1 = (TextView) contentView.findViewById(R.id.name);
tv1.setText(p.name);
ImageView tv3 = (ImageView) contentView.findViewById(R.id.imageView2);
int resID = getResources().getIdentifier(String.valueOf(p.image), "drawable", getApplicationContext().getPackageName());
tv3.setImageResource(resID);
TextView tv2 = (TextView) contentView.findViewById(R.id.textView);
tv2.setText(p.sostav);
}
//实现setActionView方法
@Override
public void setActionView(final View contentView, final int position, final HorizontalScrollView parent) {
Korz p = ((Korz) getItem(position));
final String name = p.name;
final int price = p.price;
final int image = getResources().getIdentifier(String.valueOf(p.image), "drawable", getPackageName());
final String sostav = p.sostav;
final TextView pnt = (TextView) contentView.findViewById(R.id.point);
contentView.findViewById(R.id.action).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Izb contact = new Izb(name, price, image, sostav);
contact.save();
Toast.makeText(getApplicationContext(), name + " добавлено в корзину", Toast.LENGTH_SHORT).show();
}
});
contentView.findViewById(R.id.plus).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView cnt=(TextView)findViewById(R.id.skolko);
CharSequence zz = cnt.getText(); // получем содержимое обьекта
int pz= new Integer(zz.toString()); // преобразовываем в число
pz++; // прибавляем 1
cnt.setText(Integer.toString(pz));
// получем содержимое обьекта
}
});
contentView.findViewById(R.id.minus).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView cnt=(TextView)findViewById(R.id.textView); //cnt - обьект "надпись"
CharSequence zz = cnt.getText(); // получем содержимое обьекта
int pz= Integer.valueOf(zz.toString()); // преобразовываем в число
pz--; // прибавляем 1
cnt.setText(Integer.toString(pz)); // преобразовываем в строку и возвращаем в обьект "надпись"
}
});
}
}
}
public class Korz extends SugarRecord {
int price;
int image;
String sostav;
@Unique
String name;
public Korz() {
}
public Korz(String name, int price, int image, String sostav ) {
this.name = name;
this.price = price;
this.image = image;
this.sostav = sostav;
}
@Override
public String toString() {
return "Korz{" +
"name='" + name + '\'' +
", mail='" + price + '\'' +
", image='" + image + '\'' +
'}';
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question