I have a custom listView with editText in each row to carry value entered by the user.
and i have a button next to the listview which onclick will add up the editText value from all the rows from the list.
can anyone give me some link for some clue.
import android.app.Activity;
import android.app.ListFragment;
import android.content.PeriodicSync;
import android.os.Bundle;
import android.text.InputFilter.LengthFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class NRSessionScheduleFragmentOne extends ListFragment {
String[] dummyVal = new String[] { "hello", "how", "are", "u", "how",
"are", "u", "how", "are" };
ListView parentList;
int addVal;
EditText period;
private NRSessionFragmentHolder parentobj;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.nrtpsessiontopics, container, false);
return v;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new MyListAdapter(getActivity()));
parentList = (ListView) getActivity().findViewById(android.R.id.list);
addVal = valueReturn(dummyVal, period, parentList);
}
public class MyListAdapter extends ArrayAdapter {
Activity contextACT;
public MyListAdapter(Activity context) {
super(context, R.layout.nrrow_of_list_lesson, dummyVal);
this.contextACT = context;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
LayoutInflater inflator = contextACT.getLayoutInflater();
final View row = inflator.inflate(R.layout.nrrow_of_list_lesson,
null);
final TextView name = (TextView) row.findViewById(R.id.lessonname);
period = (EditText) row.findViewById(R.id.ssperiodeditText);
final Button increase = (Button) row.findViewById(R.id.ssincrease);
final Button decrease = (Button) row.findViewById(R.id.ssdecrease);
increase.setId(position);
decrease.setId((dummyVal.length) + position);
final Integer currentQuant = Integer.parseInt(period.getText()
.toString());
increase.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
increase.getId();
Log.w("button value", "" + increase.getId());
if (currentQuant != 0) {
period.setText(new Integer(currentQuant + 1).toString());
}
}
});
decrease.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Log.w("button value1", " " + decrease.getId());
decrease.getId();
if (currentQuant != 1) {
period.setText(new Integer(currentQuant - 1).toString());
}
}
});
return (row);
}
}
void setParent(NRSessionFragmentHolder parent) {
this.parentobj = parent;
}
public int valueReturn(String[] dummyVal, EditText period2,
ListView parentList) {
Integer add = 0;
for (int i = 0; i <= dummyVal.length; i++) {
View temp = null;
temp = parentList.getChildAt(i);
while (!(temp == null)) {
add = Integer.parseInt(period2.getText().toString());
}
}
return add;
}
}
You need your Button’s
onClickListener()to get the adapter of the listView, convert it to a List and sum up the contents of the list.