1、实现功能描述:
要实现一个新建订单的功能,新建订单界面有一个ListView可以动态添加订单行(动态添加Item),并且可以修改订单行中的数据(可以编辑EditText并且获取EditText进行计算显示)
2、实现思路:
在订单界面定义一个添加按钮,点击添加按钮跳转到物品列表选择物品,通过Intent传递到订单界面,加入到ListView中。
3、具体实现:
注意:需要一个文件保存已经添加到列表的数据,每次为订单的ListView添加数据时,先从文件中读取数据,再将新加入的数据加入,再加载到ListView上。
主要有三个点:
1、自定义myTextWatcher,为了获取ViewHolder,以获取当前点击的item
2、自定义myOnFocusChangeListener,为了获取ViewHolder,以获取当前点击的item
3、在布局中定义一个textview用于记录当前Item在stockBeenList中的位置,为了保存和获取对应stockBeenList中的数据
适配器代码:
package com.wxcoming.wxcomingerp.sale.neworder.adapter;
import android.app.DatePickerDialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.wxcoming.wxcomingerp.R;
import com.wxcoming.wxcomingerp.bean.SaleFindOrderLine;
import com.wxcoming.wxcomingerp.bean.Ware;
import java.util.Calendar;
import java.util.List;
/**
*
* Created by adolph_jun on 2017/10/10.
*/
public class SaleNewOrderAdapter extends BaseAdapter {
private List<SaleFindOrderLine> stockBeenList;
private Context context;
private int mSt_Year, mSt_Month, mSt_Day;
private int mEnd_Year, mEnd_Month, mEnd_Day;
private int mNow_Year, mNow_Month, mNow_Day;
public SaleNewOrderAdapter(List<SaleFindOrderLine> stockBeenList, Context context){
this.stockBeenList = stockBeenList;
this.context = context;
Calendar now = Calendar.getInstance();
mEnd_Year = mSt_Year = mNow_Year = now.get(Calendar.YEAR);
mEnd_Month = mSt_Month = mNow_Month = now.get(Calendar.MONTH)+1;
mEnd_Day = mSt_Day = mNow_Day = now.get(Calendar.DAY_OF_MONTH);
}
@Override
public int getCount() {
return stockBeenList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
ViewHolder holder = null;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.item_sale_neworder_list,null,false);
holder = new ViewHolder();
holder.Tv_Code = (TextView) convertView.findViewById(R.id.SaleNewOrder_item_wareCode);
holder.Tv_Name = (TextView) convertView.findViewById(R.id.SaleNewOrder_item_wareName);
holder.Et_price = (EditText) convertView.findViewById(R.id.SaleNewOrder_item_price);
holder.Et_quantity = (EditText) convertView.findViewById(R.id.SaleNewOrder_item_qu);
holder.Tv_money = (TextView) convertView.findViewById(R.id.SaleNewOrder_item_money);
holder.Tv_model = (TextView) convertView.findViewById(R.id.SaleNewOrder_item_model);
holder.Tv_size = (TextView) convertView.findViewById(R.id.SaleNewOrder_item_size);
holder.Tv_unit = (TextView) convertView.findViewById(R.id.SaleNewOrder_item_unit);
holder.Tv_delivery = (TextView) convertView.findViewById(R.id.SaleNewOrder_item_deliveryDate);
holder.Tv_send = (TextView) convertView.findViewById(R.id.SaleNewOrder_item_sendDate);
holder.Tv_pis = (TextView) convertView.findViewById(R.id.SaleNewOrder_item_pis);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.Tv_Code.setText("物料编码:"+stockBeenList.get(position).getWareCode());
holder.Tv_Name.setText("物料名称:"+stockBeenList.get(position).getWareName());
holder.Tv_model.setText("规格:"+stockBeenList.get(position).getWareModel());
holder.Tv_size.setText("尺寸:"+stockBeenList.get(position).getWareSize()+position);
holder.Tv_pis.setText(position+"");
holder.Tv_unit.setText("单位:"+stockBeenList.get(position).getUnitName());
holder.Tv_delivery.setText(stockBeenList.get(position).getDeliveryDate());
holder.Tv_send.setText(stockBeenList.get(position).getSend_date());
//价格Edittext的点击事件
if(holder.Et_price.getTag() instanceof TextWatcher){
holder.Et_price.removeTextChangedListener((TextWatcher) holder.Et_price.getTag());
}
holder.Et_price.setText(stockBeenList.get(position).getPrice()+"");
final myTextWatcher watcher = new myTextWatcher(holder) {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
int pis = Integer.parseInt(v.Tv_pis.getText().toString());
if(TextUtils.isEmpty(s)){
stockBeenList.get(pis).setPrice(0);
}else{
float p = Float.parseFloat(s.toString());
stockBeenList.get(pis).setPrice(p);
stockBeenList.get(pis).setMoney(p* stockBeenList.get(pis).getOrder_quantity());
v.Tv_money.setText("金额:"+stockBeenList.get(pis).getMoney()+"");
}
}
};
holder.Et_price.setOnFocusChangeListener(new myOnFocusChangeListener(holder) {
@Override
public void onFocusChange(View v, boolean hasFocus) {
v1.Et_price.addTextChangedListener(watcher);
v1.Et_price.setTag(watcher);
}
});
//数量Edittext的点击事件
if(holder.Et_quantity.getTag() instanceof TextWatcher){
holder.Et_quantity.removeTextChangedListener((TextWatcher) holder.Et_quantity.getTag());
}
holder.Et_quantity.setText(stockBeenList.get(position).getOrder_quantity()+"");
final myTextWatcher watcher1 = new myTextWatcher(holder) {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
int pis = Integer.parseInt(v.Tv_pis.getText().toString());
if(TextUtils.isEmpty(s)){
stockBeenList.get(pis).setOrder_quantity(0);
}else{
float p = Float.parseFloat(s.toString());
stockBeenList.get(pis).setOrder_quantity(p);
stockBeenList.get(pis).setMoney(p* stockBeenList.get(pis).getPrice());
v.Tv_money.setText("金额:"+stockBeenList.get(pis).getMoney()+"");
}
}
};
holder.Et_quantity.setOnFocusChangeListener(new myOnFocusChangeListener(holder) {
@Override
public void onFocusChange(View v, boolean hasFocus) {
v1.Et_quantity.addTextChangedListener(watcher1);
v1.Et_quantity.setTag(watcher1);
}
});
holder.Tv_money.setText(stockBeenList.get(position).getMoney()+"");
DateListener(holder);
return convertView;
}
//日期选择监听事件
private void DateListener(final ViewHolder holder) {
holder.Tv_delivery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
int pis = Integer.parseInt(holder.Tv_pis.getText().toString());
mSt_Year = year;
mSt_Month = monthOfYear;
mSt_Day = dayOfMonth;
holder.Tv_delivery.setText(mSt_Year+"-"+mSt_Month+"-"+mSt_Day);
stockBeenList.get(pis).setDeliveryDate(mSt_Year+"-"+mSt_Month+"-"+mSt_Day);
}
}, mNow_Year, mNow_Month, mNow_Day).show();
}
});
holder.Tv_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
int pis = Integer.parseInt(holder.Tv_pis.getText().toString());
mEnd_Year = year;
mEnd_Month = monthOfYear;
mEnd_Day = dayOfMonth;
holder.Tv_send.setText(mEnd_Year+"-"+mEnd_Month+"-"+mEnd_Day);
stockBeenList.get(pis).setSend_date(mEnd_Year+"-"+mEnd_Month+"-"+mEnd_Day);
}
}, mNow_Year, mNow_Month, mNow_Day).show();
}
});
}
static class ViewHolder{
private TextView Tv_Code;
private TextView Tv_Name;
private EditText Et_price;
private EditText Et_quantity;
private TextView Tv_money;
private TextView Tv_model;
private TextView Tv_size;
private TextView Tv_unit;
private TextView Tv_delivery;
private TextView Tv_send;
private TextView Tv_pis;//记录当前Item在stockBeenList中的位置,为了保存和获取对应stockBeenList中的数据
}
}
/**
* 自定义myTextWatcher,为了获取ViewHolder,以获取当前点击的item
*/
class myTextWatcher implements TextWatcher{
SaleNewOrderAdapter.ViewHolder v;
public myTextWatcher(SaleNewOrderAdapter.ViewHolder v){
this.v = v;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
}
/**
* myOnFocusChangeListener,为了获取ViewHolder,以获取当前点击的item
*/
class myOnFocusChangeListener implements View.OnFocusChangeListener{
SaleNewOrderAdapter.ViewHolder v1;
public myOnFocusChangeListener(SaleNewOrderAdapter.ViewHolder v ){
this.v1 = v;
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
}
}