Reputation: 23
I am new at programming I need your help. I have a listview with three textviews which are populated by user, one of them is numeric. What I want is to show total value of all those textviews under the listview in another textview. It should be updated every time when users add or delete item. I have found some code about how to sum this, but I am not sure where to place it and when to call that metod because user populates lv and it is empty at the beginning. And this thing with updating sum makes it even harder for me.
Here is adapter:
Public class MyAdapter extends BaseAdapter {
private ArrayList<MyItem> mItem;
public MyAdapter(ArrayList<MyItem> items)
{
this.mItem = items;
}
@Override
public int getCount() {
return this.mItem.size();
}
@Override
public Object getItem(int position) {
return mItem.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder myVH;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.items_lv, parent, false);
myVH = new ViewHolder(convertView);
convertView.setTag(myVH);
} else {
myVH = (ViewHolder) convertView.getTag();
}
MyItem item = this.mItem.get(position);
myVH.tvSurname.setText(item.getgSurname());
myVH.tvName.setText(item.getgName());
myVH.tvNumber.setText(item.getgNumber());
return convertView;
}
public void addnewItem (MyItem item) {
this.mItem.add(item);
this.notifyDataSetChanged();
}
public void deleteAt(int position) {
this.mItem.remove(position);
this.notifyDataSetChanged();
}
private class ViewHolder {
public TextView tvSurname, tvName, tvNumber;
public ViewHolder(View itemView) {
tvSurname = (TextView) itemView.findViewById(R.id.tvaSurname);
tvName = (TextView) itemView.findViewById(R.id.tvaName);
tvNumber = (TextView) itemView.findViewById(R.id.tvaNumberitems);
}
}
}
Here is MainActivity where I want to show sum, under the listview
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final String OUTPUT_SURNAME = "surname";
public static final String OUTPUT_NAME = "name";
public static final String OUTPUT_NUMBER = "number";
public static final int REQUEST_CODE = 1;
ListView lvPeople;
MyAdapter myAdapter;
Button bAdd;
TextView tvSum, tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SetUpUI();
}
private void SetUpUI() {
lvPeople = (ListView) findViewById(R.id.lvPeople);
bAdd = (Button) findViewById(R.id.bAdd);
tvSum = (TextView) findViewById(R.id.tvSum);
tvResult = (TextView) findViewById(R.id.tvResult);
bAdd.setOnClickListener(this);
ArrayList<MyItem> items = Database.getInstance(this).getAllItems();
myAdapter = new MyAdapter(items);
lvPeople.setAdapter(myAdapter);
lvPeople.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
dialogBuilder.setMessage("Do you want to delete person?");
dialogBuilder.setCancelable(true);
dialogBuilder.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Database.getInstance(getApplicationContext()).deletePerson((MyItem) myAdapter.getItem(position));
myAdapter.deleteAt(position);
dialog.cancel();
}
});
dialogBuilder.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
return true;
}
});
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this, AddPerson.class);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String surname = data.getStringExtra(OUTPUT_SURNAME);
String name = data.getStringExtra(OUTPUT_NAME);
String number = data.getStringExtra(OUTPUT_NUMBER);
MyItem item = new MyItem(surname, name, number);
Database.getInstance(getApplicationContext()).addPerson(item);
myAdapter.addnewItem(new MyItem(surname, name, number));
}
}
}
Here is the idea of how should it look
Upvotes: 1
Views: 618
Reputation: 18568
You need to sum the values up in the adapter, maybe a class member private int total
would do…
Public class MyAdapter extends BaseAdapter {
private ArrayList<MyItem> mItem;
private int total;
public MyAdapter(ArrayList<MyItem> items) {
this.mItem = items;
// on creation and after setting the items, sum their values up
this.total = sumValuesUp();
}
...
public void addnewItem (MyItem item) {
// add the value of the newly added item
this.total += item.getValue();
this.mItem.add(item);
this.notifyDataSetChanged();
}
public void deleteAt(int position) {
// subtract the value on deletion
this.total -= this.mItem.get(position).getValue();
this.mItem.remove(position);
this.notifyDataSetChanged();
}
private int sumValuesUp() {
int sum = 0;
for (MyItem myItem : mItem) {
sum += mItem.getValue();
}
return sum;
}
public int getTotal() {
return total;
}
...
}
And then set a listener to the ListView (or an EventHandler) and get the total in case the ListView gets manipulated.
Upvotes: 0
Reputation: 100
try something like this:
public void onButtonClick(View view){
int sum = 0;
for(i=0;i<= listView.getCount; i++){
TextView textViewNumber = (TextView)view.findViewById(R.id.idOfUrTextViewOnWhichUrAreDisplayingNumericalValue);
int value = Integer.parseInt(textViewNumber.getText().toString());
sum = sum + value;
}
return sum;
}
Upvotes: 1