如何使用android中的基本适配器类在textview中传递总值

时间:2012-05-23 12:00:07

标签: android android-listview baseadapter

首次选择价格,数量值。我添加所有价格并在totalamount中显示为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值我想传递总数..请任何人都可以帮助我

1 个答案:

答案 0 :(得分:0)

还好。在适配器的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);

as,

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));

    }

}