ListItem中的Edittext在android中不可编辑

时间:2014-05-15 10:05:59

标签: java android xml android-layout listview

我已经制作了一个ListView,每个ListItem都有EditText,我想编辑它们,但我无法编辑它们,请帮助我,我的代码如下,我提到了我的java类(活动),Xml和原始文件将绑定到ListView和适配器: 的 cart.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/hdr_cart"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/header_bg" />

        <TextView
            android:id="@+id/tv_title"
            style="@style/font_med"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="My cart"
            android:textColor="#ff0000" />

        <ImageView
            android:id="@+id/iv_bak"
            style="@style/iv_buyingrequest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:background="@drawable/btn_back" />
    </RelativeLayout>

    <ListView
        android:id="@+id/cart_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tv_total"
        android:layout_below="@+id/hdr_cart" />

    <TextView
        android:id="@+id/tv_total"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/ll_botom"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dp"
        android:padding="5dp"
        android:text="Total:"
        android:textSize="20dp" />

    <LinearLayout
        android:id="@+id/ll_botom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:orientation="horizontal"
        android:weightSum="2" >

        <Button
            android:id="@+id/tv_place_order"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@drawable/bg_blu_btn_selector"
            android:gravity="center"
            android:padding="8dp"
            android:text="Place Order"
            android:textColor="#ffffff"
            android:textSize="16dp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/tv_home"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@drawable/bg_blu_btn_selector"
            android:gravity="center"
            android:padding="8dp"
            android:text="Continue Shopping"
            android:textColor="#ffffff"
            android:textSize="16dp"
            android:textStyle="bold" />
    </LinearLayout>

</RelativeLayout>

raw.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/iv_product_img"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="left"
        android:layout_marginTop="5dp" />

    <TextView
        android:id="@+id/product_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/iv_product_img"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:text="product Name"
        android:textColor="#545454"
        android:textSize="14dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_qty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/product_label"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/iv_product_img"
        android:text="Quantity:"
        android:textColor="#000000"
        android:textSize="12dp" />

    <EditText
        android:id="@+id/et_qty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/product_label"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/tv_qty"
        android:background="@drawable/blk_editext"
        android:ems="4"
        android:gravity="center_vertical"
        android:text="200"
        android:textSize="12dp" />

    <TextView
        android:id="@+id/tv_unit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/product_label"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/et_qty"
        android:text="Acre"
        android:textColor="#000000"
        android:textSize="12dp" />

    <TextView
        android:id="@+id/tv_wholesale_proce"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_qty"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/iv_product_img"
        android:text="Price:"
        android:textColor="#000000"
        android:textSize="12dp" />

    <TextView
        android:id="@+id/tv_wprice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_qty"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/tv_wholesale_proce"
        android:text="100"
        android:textColor="#545454"
        android:textSize="12dp" />

    <TextView
        android:id="@+id/tv_retail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_wprice"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/iv_product_img"
        android:text="Sub Total:"
        android:textColor="#000000"
        android:textSize="12dp" />

    <TextView
        android:id="@+id/tv_rprice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_wprice"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/tv_retail"
        android:text="100"
        android:textColor="#545454"
        android:textSize="12dp" />

    <TextView
        android:id="@+id/pro_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/min_qty"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/fob_price"
        android:text=""
        android:textColor="#cecece"
        android:textSize="12dp" />

</RelativeLayout>

cartList.java

package com.epe.yehki.ui;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.epe.yehki.adapter.CartAdapter;
import com.epe.yehki.backend.BackendAPIService;

import com.epe.yehki.util.Const;
import com.epe.yehki.util.Pref;
import com.example.yehki.R;

public class CartListActivity extends Activity {
    private ProgressDialog pDialog;
    Intent in = null;
    ListView lv;
    JSONObject jsonObj;
    ArrayList<HashMap<String, String>> cartList;
    Bitmap bitmap;;
    private CartAdapter cartContent;
    JSONArray carts = null;
    ImageView back;
    TextView tv_place_order, tv_home;
    String total, pid;
    TextView tv_total;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_cart_list);
        lv = (ListView) findViewById(R.id.cart_list);
        back = (ImageView) findViewById(R.id.iv_bak);
        tv_place_order = (TextView) findViewById(R.id.tv_place_order);
        tv_home = (TextView) findViewById(R.id.tv_home);
        tv_total = (TextView) findViewById(R.id.tv_total);
        cartList = new ArrayList<HashMap<String, String>>();

        back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // execute the cartList api()...........!!!!
        new GetCartList().execute();
        // listView ClickEvent

        lv.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                System.out.println("::::::::::::Long click:::::::::::::::::");
                pid = cartList.get(position).get(Const.TAG_PRODUCT_ID);
                showCustomeAlert(CartListActivity.this, "Are You Sure want to delete?", "Yehki", "No", "Yes", position, pid);

                return false;
            }
        });
        tv_home.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                in = new Intent(CartListActivity.this, HomeActivity.class);
                startActivity(in);
            }
        });
        tv_place_order.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                in = new Intent(CartListActivity.this, CartPlaceOrderActivity.class);
                startActivity(in);
            }
        });
    }

    /*
     * CART LIST PRODUCT LIST...............!!!!!!!!!
     */
    private class GetCartList extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(CartListActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            String cartUrl = Const.API_CART_LIST + "?customer_id=" + Pref.getValue(CartListActivity.this, Const.PREF_CUSTOMER_ID, "");
            BackendAPIService sh = new BackendAPIService();
            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(cartUrl, BackendAPIService.GET);

            Log.d("Response: ", "> " + jsonStr);
            try {
                if (jsonStr != null) {

                    jsonObj = new JSONObject(jsonStr);
                    total = jsonObj.getString(Const.TAG_TOTAL);

                    // Getting JSON Array node
                    if (jsonObj.has(Const.TAG_PRO_LIST)) {

                        carts = jsonObj.getJSONArray(Const.TAG_PRO_LIST);
                        if (carts != null && carts.length() != 0) {
                            // looping through All Contacts

                            for (int i = 0; i < carts.length(); i++) {
                                JSONObject c = carts.getJSONObject(i);

                                String proId = c.getString(Const.TAG_PRODUCT_ID);
                                String proName = c.getString(Const.TAG_PRODUCT_NAME);
                                String wPrice = c.getString(Const.TAG_WHOLESALE_PRICE);
                                String rPrice = c.getString(Const.TAG_RETAIL_PRICE);
                                String qty = c.getString(Const.TAG_QUANTITY);
                                String subTotal = c.getString(Const.TAG_SUBTOTAL);

                                String proimg = Const.API_HOST + "/" + c.getString(Const.TAG_PRODUCT_IMG);
                                HashMap<String, String> cartProduct = new HashMap<String, String>();

                                cartProduct.put(Const.TAG_PRODUCT_ID, proId);
                                cartProduct.put(Const.TAG_PRODUCT_NAME, proName);
                                cartProduct.put(Const.TAG_PRODUCT_IMG, proimg);
                                cartProduct.put(Const.TAG_WHOLESALE_PRICE, wPrice);
                                cartProduct.put(Const.TAG_RETAIL_PRICE, rPrice);
                                cartProduct.put(Const.TAG_QUANTITY, qty);
                                cartProduct.put(Const.TAG_SUBTOTAL, subTotal);
                                cartProduct.put(Const.TAG_TOTAL, total);
                                cartList.add(cartProduct);

                            }
                        }
                    }

                } else {
                    Log.e("ServiceHandler", "Couldn't get any data from the url");
                }

            } catch (JSONException e) {
                e.printStackTrace();
                System.out.println("::::::::::::::::::got an error::::::::::::");
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();

            cartContent = new CartAdapter(CartListActivity.this, cartList);
            lv.setAdapter(cartContent);
            StringBuilder b = new StringBuilder();
            for (int i = 0; i > cartContent.getCount(); i++)
                b.append(cartContent.getItem(i));
            tv_total.setText("Total:" + total);

        }

    }

    public void showCustomeAlert(final Context context, String message, String title, String leftButton, String rightButton, final int position, final String pid2) {
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.popup_alert_delete);
        dialog.setCancelable(true);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        TextView txt_message = (TextView) dialog.findViewById(R.id.txtMessage);
        final Button btn_left = (Button) dialog.findViewById(R.id.btnLeft);
        final Button btn_right = (Button) dialog.findViewById(R.id.btnRigth);

        TextView txtTitle = (TextView) dialog.findViewById(R.id.txtTitle);

        txtTitle.setText(title);
        txt_message.setText(message);
        btn_left.setText(leftButton);
        btn_right.setText(rightButton);
        btn_left.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss();

            }
        });
        btn_right.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String pid = cartList.get(position).get(Const.TAG_PRODUCT_ID);
                cartList.clear();
                new RemoveCart().execute(pid2);
                cartContent.notifyDataSetChanged();

                dialog.dismiss();

            }
        });

        dialog.show();
    }

    /*
     * Remove from cart List.........!!!
     */
    private class RemoveCart extends AsyncTask<String, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(CartListActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(String... arg0) {
            // Creating service handler class instance
            String cartUrl = Const.API_REMOVE_CART_LIST + "?customer_id=" + Pref.getValue(CartListActivity.this, Const.PREF_CUSTOMER_ID, "") + "&product_id=" + arg0[0];
            BackendAPIService sh = new BackendAPIService();
            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(cartUrl, BackendAPIService.GET);

            Log.d("Response: ", "> " + jsonStr);
            try {
                if (jsonStr != null) {

                    jsonObj = new JSONObject(jsonStr);
                    total = jsonObj.getString(Const.TAG_TOTAL);

                    // Getting JSON Array node
                    if (jsonObj.has(Const.TAG_PRO_LIST)) {

                        carts = jsonObj.getJSONArray(Const.TAG_PRO_LIST);
                        if (carts != null && carts.length() != 0) {
                            // looping through All Contacts

                            for (int i = 0; i < carts.length(); i++) {
                                JSONObject c = carts.getJSONObject(i);

                                String proId = c.getString(Const.TAG_PRODUCT_ID);
                                String proName = c.getString(Const.TAG_PRODUCT_NAME);
                                String wPrice = c.getString(Const.TAG_WHOLESALE_PRICE);
                                String rPrice = c.getString(Const.TAG_RETAIL_PRICE);
                                String qty = c.getString(Const.TAG_QUANTITY);
                                String subTotal = c.getString(Const.TAG_SUBTOTAL);

                                String proimg = Const.API_HOST + "/" + c.getString(Const.TAG_PRODUCT_IMG);
                                HashMap<String, String> cartProduct = new HashMap<String, String>();

                                cartProduct.put(Const.TAG_PRODUCT_ID, proId);
                                cartProduct.put(Const.TAG_PRODUCT_NAME, proName);
                                cartProduct.put(Const.TAG_PRODUCT_IMG, proimg);
                                cartProduct.put(Const.TAG_WHOLESALE_PRICE, wPrice);
                                cartProduct.put(Const.TAG_RETAIL_PRICE, rPrice);
                                cartProduct.put(Const.TAG_QUANTITY, qty);
                                cartProduct.put(Const.TAG_SUBTOTAL, subTotal);
                                cartProduct.put(Const.TAG_TOTAL, total);
                                cartList.add(cartProduct);

                            }
                        }
                    }

                } else {
                    Log.e("ServiceHandler", "Couldn't get any data from the url");
                }

            } catch (JSONException e) {
                e.printStackTrace();
                System.out.println("::::::::::::::::::got an error::::::::::::");
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();

            cartContent = new CartAdapter(CartListActivity.this, cartList);
            lv.setAdapter(cartContent);
            StringBuilder b = new StringBuilder();
            for (int i = 0; i > cartContent.getCount(); i++)
                b.append(cartContent.getItem(i));
            tv_total.setText("Total:" + total);

        }

    }

}

cartAdapter.java

package com.epe.yehki.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.epe.yehki.backend.BackendAPIService;
import com.epe.yehki.ui.WholesaleProductDetailActivity;
import com.epe.yehki.util.Const;
import com.epe.yehki.util.Pref;
import com.example.yehki.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public class CartAdapter extends BaseAdapter {
    public ArrayList<HashMap<String, String>> cartArray;
    private Context mContext;
    private DisplayImageOptions options;
    public static ImageLoader imageLoader;
    String retailPrice;
    String wholesalePrice;
    String retailQty;
    String wholesaleQty;
    private ProgressDialog pDialog;
    String status;

    public CartAdapter(Context paramContext, ArrayList<HashMap<String, String>> cartList) {
        this.mContext = paramContext;
        this.cartArray = cartList;

        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(paramContext));

        options = new DisplayImageOptions.Builder().cacheOnDisc(true).showStubImage(R.drawable.logo).showImageOnFail(R.drawable.ic_launcher).build();

    }

    public int getCount() {
        return this.cartArray.size();
    }

    public Object getItem(int paramInt) {
        return Integer.valueOf(paramInt);
    }

    public long getItemId(int paramInt) {
        return paramInt;
    }

    @SuppressWarnings("static-access")
    public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
        LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext.getSystemService("layout_inflater");
        Viewholder localViewholder = null;
        if (paramView == null) {
            paramView = localLayoutInflater.inflate(R.layout.raw_cart, paramViewGroup, false);
            localViewholder = new Viewholder();
            localViewholder.pid = ((TextView) paramView.findViewById(R.id.pro_id));
            localViewholder.proImg = ((ImageView) paramView.findViewById(R.id.iv_product_img));
            localViewholder.proName = ((TextView) paramView.findViewById(R.id.product_label));
            localViewholder.price = ((TextView) paramView.findViewById(R.id.tv_wprice));
            localViewholder.qty = ((EditText) paramView.findViewById(R.id.et_qty));
            localViewholder.subTotal = ((TextView) paramView.findViewById(R.id.tv_rprice));

            paramView.setTag(localViewholder);

        } else {
            localViewholder = new Viewholder();
            localViewholder = (Viewholder) paramView.getTag();
        }
        System.out.println("::::::::::::::array indexes::::::::::::" + cartArray.get(paramInt));

        retailQty = cartArray.get(paramInt).get(Const.TAG_MIN_ORDER_QTY_RETAIL);
        retailPrice = cartArray.get(paramInt).get(Const.TAG_RETAIL_PRICE);
        wholesaleQty = cartArray.get(paramInt).get(Const.TAG_MIN_ORDER_QTY_WHOLESALE);
        wholesalePrice = cartArray.get(paramInt).get(Const.TAG_WHOLESALE_PRICE);

        localViewholder.proName.setText(cartArray.get(paramInt).get(Const.TAG_PRODUCT_NAME));
        localViewholder.pid.setText(cartArray.get(paramInt).get(Const.TAG_PRODUCT_ID));
        localViewholder.pid.setVisibility(View.GONE);
        localViewholder.qty.setText(cartArray.get(paramInt).get(Const.TAG_QUANTITY));
        localViewholder.subTotal.setText(cartArray.get(paramInt).get(Const.TAG_SUBTOTAL));

        /*
         * for changing the price based on quantity....
         */
        localViewholder.price.setText(cartArray.get(paramInt).get(Const.TAG_WHOLESALE_PRICE));
        imageLoader.displayImage(cartArray.get(paramInt).get(Const.TAG_PRODUCT_IMG), localViewholder.proImg, options);

        return paramView;

    }

    static class Viewholder {
        ImageView proImg;
        TextView proName;
        TextView price;
        TextView subTotal;
        TextView pid;
        EditText qty;

    }

}

1 个答案:

答案 0 :(得分:0)

使用

 myListView.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> listView, View view, int position, long id) 

{     EditText yourEditText =(EditText)view.findViewById(R.id.youredittextid);     listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);     yourEditText.requestFocus(); } });

问题是选择行会占用焦点但是为了编辑EditText你需要关注它