开发者

Read EditText Content From ListView

I have a ListView in following manner

Item_name       Quantity      Price/Unit    Net Amount

<TextView>     <EditText>    <TextView>    <TextView>
<TextView>     <EditText>    <TextView>    <TextView>
<TextView>     <EditText>    <TextView>    <TextView>
<TextView>     <EditText>    <TextView>    <TextView>

and so on

Now i want that when i enter any text in the EditText for quantity then it should generate the Net Amount by given formaula

Net Amount=Quantity*Price/Unit

Example

Quantity 3 Price/Unti 30

so Net Amount TextView should display 90

I want this in an efficient user interface way

Please give me any solution

My code

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;

         }
    }
}

My Layout Files

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="ve开发者_高级运维rtical"
  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>


  1. You can create one layout with three TextView and one EditText control and inflect your layout.

  2. In getView method you can access all UI object.

  3. Write anonymous inner class for onkeylistener

  4. Calculate net amount and set in 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;
             }
         });
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜