iamRaja iamRaja - 3 months ago 39
Android Question

how to get all values of particular TextView of listView? -Android

i have made ListView with three columns 'item','qty','rate' i get this entries from the user and i have made the listview work perfectly but i want to get all the values of the 'rate' column and add them for the net amount.

Here is my android code:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
populateList();
adapter = new listviewAdapter(List.this, list);
lview.setAdapter(adapter);
}


private void populateList() {
HashMap temp = new HashMap();
list = new ArrayList<HashMap>();

item = etItem.getText().toString();
qty = etQty.getText().toString();
rate = etRate.getText().toString();

temp.put(FIRST_COLUMN, "1");
temp.put(SECOND_COLUMN, item);
temp.put(THIRD_COLUMN, qty);
temp.put(FOURTH_COLUMN, rate);
list.add(temp);


}

I tried out this method below but it only toast the first value.But i want to get all the values under the rate column and add them up for the net-amount.

public void get() {
StringBuilder sb = new StringBuilder();
for(int i=0; i<adapter.getCount(); i++) {
String a = ((TextView) findViewById(R.id.FourthText)).getText().toString();
adapter.getItem(i).toString();
sb.append(a);
sb.append("\n");
}
text = sb.toString();
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
}

Answer

I think this might help you.

private int GrandTotal(ListView list) {
int sum=0;
for (int i = 0; i < list.getCount(); i++) {
    View v = list.getChildAt(i);
    TextView rate = (TextView) v.findViewById(R.id.rate);
    sum = sum + Integer.parseInt(rate.getText().toString() )
}
return sum;

}

:)

Comments