-1

第一次选择价格,数量值。我添加了所有价格并显示为 textview.up 到现在工作正常。如果我更改了数量值,我可以自动更改当时的价格值我想更改 textview 中的总值

我试试这段代码

     public class SelectedItem extends Activity  {
 private ListView lv;
    private CustomAdapter adapter;
    int tableid;
    String id;
ArrayList<String> arr=new ArrayList<String>();
ArrayList<String> price=new ArrayList<String>();
ArrayList<Bitmap> image=new ArrayList<Bitmap>();
Double amountvalue;
TextView totalamount;
Button order;
String dtotalamount;
    protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
arr=getIntent().getStringArrayListExtra("itemname");
price=getIntent().getStringArrayListExtra("itemprice");


image=getIntent().getExtras().getParcelableArrayList("itemimage");

setContentView(R.layout.selecteditem);
 lv = (ListView) findViewById(R.id.selecteditemlist);
 adapter = new CustomAdapter(this, arr,price,image);
 lv.setAdapter(adapter);
 amountvalue=CustomAdapter.x;
 System.out.println(amountvalue);
 totalamount=(TextView)findViewById(R.id.amount);
 totalamount.setText(String.valueOf(amountvalue));(here only i can display the total value...now i want change qty means total also will change)
 order=(Button)findViewById(R.id.order);
 order.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        try
        {
            tableid=Login.tableid;
            id=Integer.toString(tableid);
            System.out.println(id);
            String x=amountvalue.toString();
                           public class SelectedItem extends Activity  {
 private ListView lv;
    private CustomAdapter adapter;
    int tableid;
    String id;
ArrayList<String> arr=new ArrayList<String>();
ArrayList<String> price=new ArrayList<String>();
ArrayList<Bitmap> image=new ArrayList<Bitmap>();
Double amountvalue;
TextView totalamount;
Button order;
String dtotalamount;

  protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
arr=getIntent().getStringArrayListExtra("itemname");
price=getIntent().getStringArrayListExtra("itemprice");


image=getIntent().getExtras().getParcelableArrayList("itemimage");

setContentView(R.layout.selecteditem);
 lv = (ListView) findViewById(R.id.selecteditemlist);
 adapter = new CustomAdapter(this, arr,price,image);
 lv.setAdapter(adapter);
 amountvalue=CustomAdapter.x;
 System.out.println(amountvalue);
 totalamount=(TextView)findViewById(R.id.amount);
 totalamount.setText(String.valueOf(amountvalue));
 order=(Button)findViewById(R.id.order);
 order.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

            tableid=Login.tableid;
            id=Integer.toString(tableid);
            System.out.println(id);
            String x=amountvalue.toString();

         }
             });
          }
          }

CustomAdapter.java

            public class CustomAdapter extends BaseAdapter {

public static Double x = 0.0;
public static Double z;
ArrayList<Integer> selectprice = new ArrayList<Integer>();
public static ArrayList<String> arr1 = new ArrayList<String>();
public static ArrayList<String> itemprice = new ArrayList<String>();
public static ArrayList<Bitmap> itemimage = new ArrayList<Bitmap>();
ArrayList<Integer> total = new ArrayList<Integer>();
public Context Context;
private LayoutInflater inflater;

HashMap<String, String> map = new HashMap<String, String>();

public CustomAdapter(Context context, ArrayList<String> arr,
        ArrayList<String> price, ArrayList<Bitmap> image) {
    Context = context;
    inflater = LayoutInflater.from(context);
    arr1 = arr;
    itemprice = price;

    itemimage = image;
    System.out.println(itemprice);
    System.out.println("arr: " + arr.size());

    for (int i = 0; i < price.size(); i++) {

        x = x + Double.parseDouble(price.get(i));

    }

}

public int getCount() {
    // TODO Auto-generated method stub
    return arr1.size();

}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return arr1.get(position);
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public void clear() {
    arr1.clear();

}

public View getView(final int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.selecteditemlistview, null);
        holder = new ViewHolder();

        holder.textViewSelectedText = (TextView) convertView
                .findViewById(R.id.selectedtext);
        holder.price = (TextView) convertView
                .findViewById(R.id.selectitemprice);
        holder.image = (ImageView) convertView
                .findViewById(R.id.selectitemimage);
        holder.qty = (EditText) convertView.findViewById(R.id.selectqty);
        holder.total = (TextView) convertView.findViewById(R.id.price);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    @SuppressWarnings("unused")
    String amount = holder.qty.getText().toString();
    final Double price1 = Double.parseDouble(itemprice.get(position));
    int qut = Integer.parseInt(holder.qty.getText().toString());
    final double total = (price1 * qut);
    holder.textViewSelectedText.setText(arr1.get(position));
    holder.price.setText(itemprice.get(position));
    holder.image.setImageBitmap(itemimage.get(position));
    holder.total.setText(String.valueOf(total));

    holder.qty.setOnFocusChangeListener(new OnFocusChangeListener() {

        @SuppressWarnings("static-access")
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub

            if (!hasFocus) {

                int position = v.getId();
                final EditText Caption = (EditText) v;
                Caption.setFocusable(true);
                holder.qty.setFocusable(true);
                int q = Integer.parseInt(holder.qty.getText().toString());
                Double result = (price1 * q);
                Double y = x - total;
                double z = y + result;

            }

        }

    });

    return convertView;
}

class ViewHolder {
    TextView textViewSelectedText = null;
    TextView price = null;
    ImageView image = null;
    EditText qty = null;
    TextView total = null;
}
}

第一次可以显示 x(customadapter) 值。更改数量后,我将获得 z(customadapter) 值。这个z值我想通过总..请任何人都可以帮助我

4

1 回答 1

1

好的。在 Adapter 的 getView() 中执行此操作,

您必须为您的 EditText 使用TextWatcher。很简单。

   holder.qty.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
            int q = Integer.parseInt(holder.qty.getText().toString()); 
            Double result = (price1 * q);
            Double y = x - total;
            double z = y + result;
            totalamount.setText(String.valueOf(z));             
    }
});

编辑 1

在您的 SelectedItem.Java 中,

更改此行

adapter = new CustomAdapter(this, arr,price,image);

作为,

adapter = new CustomAdapter(this, arr,price,image,totalamount);

现在在您的CustomAdapter 类中全局添加一个 TextView,

TextView totalamount=null; 

现在更改构造函数,

public CustomAdapter(Context context, ArrayList<String> arr,
        ArrayList<String> price, ArrayList<Bitmap> image,TextView text) {
    Context = context;
    inflater = LayoutInflater.from(context);
    arr1 = arr;
    itemprice = price;
    totalamount=text;
    itemimage = image;
    System.out.println(itemprice);
    System.out.println("arr: " + arr.size());

    for (int i = 0; i < price.size(); i++) {

        x = x + Double.parseDouble(price.get(i));

    }

}
于 2012-05-23T12:54:03.347 回答