I have some TextViews and edittexts in a listview item.
What i want is to show some items, quantity,rate and total price...
Whenever quantity or rate is changed, total price also should be changed... Listitem should seen as selected when edittext is focussed.
public class MainActivity extends Activity {
private MySimpleArrayAdapterNew sampleListAdapter;
private ArrayList<HashMap<String, String>> sampleArrayList;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listView1);
sampleArrayList = new ArrayList<HashMap<String, String>>();
for(int i=0;i<100;i++)
{
HashMap<String, String> sampleObjectMap = new HashMap<String, String>();
sampleObjectMap.put("value1", "aaa"+i);
sampleObjectMap.put("value2", "666");
sampleObjectMap.put("value3", "1");
sampleObjectMap.put("value4", "777");
sampleObjectMap.put("value5", "444"+i);
sampleObjectMap.put("value6", "444"+i);
sampleArrayList.add(sampleObjectMap);
}
sampleListAdapter = new MySimpleArrayAdapterNew(MainActivity.this,
R.layout.cartitem, sampleArrayList);
listView.setAdapter(sampleListAdapter);
}
public class MySimpleArrayAdapterNew extends
ArrayAdapter<HashMap<String, String>> {
private Context context;
private int layoutResourceId;
private ArrayList<HashMap<String, String>> sampleArrayLists;
public MySimpleArrayAdapterNew(Context context, int layoutResourceId,
ArrayList<HashMap<String, String>> sampleArrayList) {
super(context, R.layout.cartitem, sampleArrayList);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.sampleArrayLists = sampleArrayList;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.slno = (TextView) row.findViewById(R.id.slno);
holder.itemname = (TextView) row.findViewById(R.id.itemname);
holder.qty = (EditText) row.findViewById(R.id.qty);
holder.rate = (EditText) row.findViewById(R.id.rate);
holder.seatno = (EditText) row.findViewById(R.id.seat);
holder.value = (TextView) row.findViewById(R.id.value);
row.setTag(holder);
} else {
holder = (WeatherHolder) row.getTag();
}
final HashMap<String, String> sampleObjectMap = sampleArrayLists
.get(position);
holder.itemname.setText(sampleObjectMap.get("value1"));
holder.slno.setText(sampleObjectMap.get("value2"));
holder.qty.setText(sampleObjectMap.get("value3"));
holder.rate.setText(sampleObjectMap.get("value4"));
holder.seatno.setText(sampleObjectMap.get("value5"));
holder.value.setText(sampleObjectMap.get("value6"));
holder.rate.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
final EditText Caption = (EditText) v;
String currentrate = sampleObjectMap.get("value4");
try {
if (!((sampleObjectMap.get("value4")) == null)) {
sampleObjectMap.put("value4", Caption.getText()
.toString());
int currentquantity = Integer
.parseInt(sampleObjectMap.get("value3"));
sampleObjectMap.put(
"value6",
(Double.parseDouble(sampleObjectMap
.get("value4")) * (currentquantity))
+ "");
sampleArrayList.set(position, sampleObjectMap);
}
} catch (NumberFormatException ne) {
ne.printStackTrace();
sampleObjectMap.put("value4", currentrate);
sampleArrayList.add(sampleObjectMap);
}
if (!hasFocus) {
notifyDataSetChanged();
} else if (hasFocus) {
}
}
});
holder.qty.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
final EditText Caption = (EditText) v;
try {
if (!((sampleObjectMap.get("value3")) == null)) {
sampleObjectMap.put("value3", Caption.getText()
.toString());
int currentquantity = Integer
.parseInt(sampleObjectMap.get("value3"));
sampleObjectMap.put(
"value6",
(Double.parseDouble(sampleObjectMap
.get("value4")) * (currentquantity))
+ "");
sampleArrayList.set(position, sampleObjectMap);
}
} catch (NumberFormatException ne) {
ne.printStackTrace();
sampleObjectMap.put("value3", "1");
sampleArrayList.add(sampleObjectMap);
}
if (!hasFocus) {
notifyDataSetChanged();
} else if (hasFocus) {
}
}
});
holder.seatno.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
final EditText Caption = (EditText) v;
sampleObjectMap.put("value5", Caption.getText().toString());
sampleArrayList.set(position, sampleObjectMap);
if (!hasFocus) {
notifyDataSetChanged();
} else if (hasFocus) {
}
}
});
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listView.setItemChecked(position, true);
}
});
return row;
}
public class WeatherHolder {
TextView slno;
TextView itemname;
EditText qty;
EditText rate;
EditText seatno;
TextView value;
}
}
}