安卓中ListItem内的java EditText不可编辑
我制作了一个ListView,因为每个ListItem都有EditText,我想编辑它们,但我无法编辑它们,请帮助我,我的代码如下,我提到了我的java类(活动)、Xml和原始文件,它们将绑定到ListView和适配器: 购物车。xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:orientation="vertical" >
<RelativeLayout
安卓:id="@+id/hdr_cart"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content" >
<ImageView
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:background="@drawable/header_bg" />
<TextView
安卓:id="@+id/tv_title"
style="@style/font_med"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_centerInParent="true"
安卓:text="My cart"
安卓:textColor="#ff0000" />
<ImageView
安卓:id="@+id/iv_bak"
style="@style/iv_buyingrequest"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentLeft="true"
安卓:layout_centerVertical="true"
安卓:background="@drawable/btn_back" />
</RelativeLayout>
<ListView
安卓:id="@+id/cart_list"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_above="@+id/tv_total"
安卓:layout_below="@+id/hdr_cart" />
<TextView
安卓:id="@+id/tv_total"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_above="@+id/ll_botom"
安卓:layout_alignParentLeft="true"
安卓:layout_marginLeft="5dp"
安卓:padding="5dp"
安卓:text="Total:"
安卓:textSize="20dp" />
<LinearLayout
安卓:id="@+id/ll_botom"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_alignParentBottom="true"
安卓:layout_marginLeft="10dp"
安卓:layout_marginRight="10dp"
安卓:orientation="horizontal"
安卓:weightSum="2" >
<Button
安卓:id="@+id/tv_place_order"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:layout_weight="1"
安卓:background="@drawable/bg_blu_btn_selector"
安卓:gravity="center"
安卓:padding="8dp"
安卓:text="Place Order"
安卓:textColor="#ffffff"
安卓:textSize="16dp"
安卓:textStyle="bold" />
<Button
安卓:id="@+id/tv_home"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:layout_weight="1"
安卓:background="@drawable/bg_blu_btn_selector"
安卓:gravity="center"
安卓:padding="8dp"
安卓:text="Continue Shopping"
安卓:textColor="#ffffff"
安卓:textSize="16dp"
安卓:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
生的。xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:descendantFocusability="blocksDescendants"
安卓:padding="10dp" >
<ImageView
安卓:id="@+id/iv_product_img"
安卓:layout_width="70dp"
安卓:layout_height="70dp"
安卓:layout_gravity="left"
安卓:layout_marginTop="5dp" />
<TextView
安卓:id="@+id/product_label"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginLeft="5dp"
安卓:layout_toRightOf="@+id/iv_product_img"
安卓:paddingBottom="5dp"
安卓:paddingTop="5dp"
安卓:text="product Name"
安卓:textColor="#545454"
安卓:textSize="14dp"
安卓:textStyle="bold" />
<TextView
安卓:id="@+id/tv_qty"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/product_label"
安卓:layout_marginLeft="5dp"
安卓:layout_toRightOf="@+id/iv_product_img"
安卓:text="Quantity:"
安卓:textColor="#000000"
安卓:textSize="12dp" />
<EditText
安卓:id="@+id/et_qty"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/product_label"
安卓:layout_marginLeft="5dp"
安卓:layout_toRightOf="@+id/tv_qty"
安卓:background="@drawable/blk_editext"
安卓:ems="4"
安卓:gravity="center_vertical"
安卓:text="200"
安卓:textSize="12dp" />
<TextView
安卓:id="@+id/tv_unit"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/product_label"
安卓:layout_marginLeft="5dp"
安卓:layout_toRightOf="@+id/et_qty"
安卓:text="Acre"
安卓:textColor="#000000"
安卓:textSize="12dp" />
<TextView
安卓:id="@+id/tv_wholesale_proce"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/et_qty"
安卓:layout_marginLeft="5dp"
安卓:layout_toRightOf="@+id/iv_product_img"
安卓:text="Price:"
安卓:textColor="#000000"
安卓:textSize="12dp" />
<TextView
安卓:id="@+id/tv_wprice"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/et_qty"
安卓:layout_marginLeft="5dp"
安卓:layout_toRightOf="@+id/tv_wholesale_proce"
安卓:text="100"
安卓:textColor="#545454"
安卓:textSize="12dp" />
<TextView
安卓:id="@+id/tv_retail"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/tv_wprice"
安卓:layout_marginLeft="5dp"
安卓:layout_toRightOf="@+id/iv_product_img"
安卓:text="Sub Total:"
安卓:textColor="#000000"
安卓:textSize="12dp" />
<TextView
安卓:id="@+id/tv_rprice"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/tv_wprice"
安卓:layout_marginLeft="5dp"
安卓:layout_toRightOf="@+id/tv_retail"
安卓:text="100"
安卓:textColor="#545454"
安卓:textSize="12dp" />
<TextView
安卓:id="@+id/pro_id"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/min_qty"
安卓:layout_marginLeft="5dp"
安卓:layout_toRightOf="@+id/fob_price"
安卓:text=""
安卓:textColor="#cecece"
安卓: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 安卓.app.Activity;
import 安卓.app.Dialog;
import 安卓.app.ProgressDialog;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.graphics.Bitmap;
import 安卓.graphics.drawable.ColorDrawable;
import 安卓.os.AsyncTask;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.view.View.OnClickListener;
import 安卓.view.Window;
import 安卓.view.WindowManager;
import 安卓.widget.AdapterView;
import 安卓.widget.AdapterView.OnItemLongClickListener;
import 安卓.widget.Button;
import 安卓.widget.ImageView;
import 安卓.widget.ListView;
import 安卓.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(安卓.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 安卓.app.ProgressDialog;
import 安卓.content.Context;
import 安卓.os.AsyncTask;
import 安卓.text.Editable;
import 安卓.text.TextWatcher;
import 安卓.util.Log;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.BaseAdapter;
import 安卓.widget.EditText;
import 安卓.widget.ImageView;
import 安卓.widget.LinearLayout;
import 安卓.widget.TextView;
import 安卓.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 楼答案
使用
{ EditText yourEditText=(EditText)视图。findViewById(R.id.youredittextid); listView。SetDegenantFocusability(ViewGroup.FOCUS_后的子体); 你的编辑文本。请求焦点(); } });
问题是,行的选择占据了焦点,但为了编辑EditText,需要将焦点放在它上