我有以下方式的ListView
Item_name Quantity Price/Unit Net Amount
<TextView> <EditText> <TextView> <TextView>
<TextView> <EditText> <TextView> <TextView>
<TextView> <EditText> <TextView> <TextView>
<TextView> <EditText> <TextView> <TextView>
等等
现在我希望当我在EditText中输入任何数量的文本时,它应该通过给定的格式生成净金额
净金额=数量*价格/单位
实施例
数量3 价格/ Unti 30
因此净金额TextView应显示90
我想以高效的用户界面方式
请给我任何解决方案
我的代码
Class FinalOrder.java
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import com.DAL.MenuCumActivity;
public class FinalOrder extends MenuCumActivity
{
private static String[] items=null;
private static String[] price=null;
private static String[] netamount=null;
private ListView listview;
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.finalorderview);
items=getIntent().getStringArrayExtra("items");
price=getIntent().getStringArrayExtra("price");
netamount=new String[price.length];
listview=(ListView)findViewById(R.id.listview);
EfficientAdapter adapter=new EfficientAdapter(this);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parentView, View v, int position,long id)
{
Toast.makeText(v.getContext(),"select",Toast.LENGTH_LONG).show();
}
});
}
public void onFocus(View v)
{
listview.setFocusable(true);
//listview.
}
private static class EfficientAdapter extends BaseAdapter
{
private LayoutInflater mInflater;
private Context ctx;
public EfficientAdapter(Context context)
{
ctx=context;
mInflater = LayoutInflater.from(context);
}
public int getCount()
{
return items.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.finalorderitem,parent,false);
holder = new ViewHolder();
holder.item = (TextView) convertView.findViewById(R.id.txtnameFinalOrderItem);
holder.quantity=(EditText)convertView.findViewById(R.id.edttextFinalOrderQty);
holder.singleprice = (TextView) convertView.findViewById(R.id.txtperpriceFinalOrderItem);
holder.finalprice=(TextView)convertView.findViewById(R.id.txtfinalpriceFinalOrderItem);
holder.item.setText(items[position]);
holder.singleprice.setText(price[position]);
int net_amount=0;
if(!holder.quantity.getText().toString().equals(""))
net_amount=Integer.parseInt(holder.quantity.getText().toString())*Integer.parseInt(price[position]);
holder.finalprice.setText(""+net_amount);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.item.setText(items[position]);
holder.singleprice.setText(price[position]);
holder.quantity.setText("");
int net_amount=0;
if(!holder.quantity.getText().toString().equals(""))
net_amount=Integer.parseInt(holder.quantity.getText().toString())*Integer.parseInt(price[position]);
holder.finalprice.setText(""+net_amount);
return convertView;
}
public class ViewHolder
{
TextView item;
EditText quantity;
TextView singleprice;
TextView finalprice;
}
}
}
我的布局文件
finalorderview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<!-- Header -->
<LinearLayout android:id="@+id/header"
android:background="#FFFFFF"
android:padding="10dip"
android:orientation="horizontal"
android:paddingBottom="5dip"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
<TextView android:id="@+id/heading"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Select Quantity"
android:gravity="left"
android:layout_weight="1"
android:textColor="#CC6633"
android:textSize="25px"
android:textStyle="bold"
/>
<Button
android:text="Make Order"
android:id="@+id/btnOrder"
android:layout_weight="1"
android:textSize="15px"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<LinearLayout android:id="@+id/header"
android:background="#FFFFFF"
android:padding="5dip"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
<LinearLayout
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:padding="5dip"
android:id="@+id/linearLayout2">
<TextView
android:id="@+id/txtnameItem"
android:text="Item"
android:gravity="center"
android:textColor="#000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:padding="5dip"
android:gravity="left"
android:id="@+id/linearLayout2">
<TextView
android:id="@+id/txtquantityfinalorder"
android:text="Quantity"
android:layout_weight="1"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
</TextView>
<TextView
android:id="@+id/txtperprice"
android:gravity="center"
android:text="Price"
android:layout_weight="1"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="left">
</TextView>
<TextView
android:id="@+id/txtfinalprice"
android:gravity="right"
android:text="Net Amount"
android:layout_weight="1"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right">
</TextView>
</LinearLayout>
</LinearLayout>
<!-- List Divider -->
<View android:layout_width="fill_parent"
android:layout_height="5dip"
android:background="#C85A17" />
<!-- ListView (list_items) -->
<LinearLayout android:id="@+id/layout"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<ListView android:id="@+id/listview"
android:layout_height="wrap_content"
android:clickable="true"
android:descendantFocusability="beforeDescendants"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
</LinearLayout>
2)finalorderitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_height="fill_parent">
<LinearLayout
android:layout_margin="15dip"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="#FFFFFF"
android:orientation="horizontal"
android:id="@+id/linearLayout1">
<LinearLayout
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:padding="3dip"
android:paddingRight="0dip"
android:id="@+id/linearLayout2">
<TextView
android:id="@+id/txtnameFinalOrderItem"
android:textSize="20px"
android:textStyle="bold"
android:text="col_1"
android:gravity="left"
android:textColor="#000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textStyle="bold"
android:id="@+id/linearLayout2">
<EditText
android:text="0"
android:id="@+id/edttextFinalOrderQty"
android:layout_width="fill_parent"
android:layout_weight="1"
android:numeric="integer"
android:layout_height="wrap_content">
</EditText>
<TextView
android:id="@+id/txtperpriceFinalOrderItem"
android:gravity="center"
android:layout_gravity="center"
android:text="price"
android:layout_weight="1"
android:textSize="15px"
android:textColor="#000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/txtfinalpriceFinalOrderItem"
android:gravity="right"
android:layout_gravity="center"
android:text="col_2"
android:layout_weight="1"
android:textSize="20px"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:1)
TextView
和一个EditText
控件的布局,并改变您的布局。 getView
方法中,您可以访问所有UI对象。onkeylistener
计算净额并在TextView
中设置。
holder.quantity=(EditText)convertView.findViewById(R.id.edttextFinalOrderQty);
holder.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
//////////// calculation ///////
return false;
}
});
如果您需要更多帮助,请告诉我,
KPBird