2012-05-23 30 views
-1

在第一次選擇價格時,qty values.i添加了所有價格並以textview.up的形式顯示爲textview.up,現在工作正常。如果我改變了數量值,我可以在那個時候自動改價值,我想改變的TextView總價值如何在android中使用基本適配器類在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

是否想從列表視圖的edittext中更新列表視圖中的textview?這是你想實現的嗎? –

+0

你面臨的問題是什麼?哪部分你覺得困難? –

回答

0

OKay。在適配器的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)); 

    } 

} 
+0

是的,你可以傳遞textview對象的自定義適配器的構造函數,並在那裏使用它 –

+0

是編輯我的答案。一探究竟。 –

+0

ya int q = Integer.parseInt(holder.qty.getText()。toString());在這一行我得到運行時錯誤,因爲無法解析一個整數。我怎樣才能做到這一點 – naveen

相關問題