gpt4 book ai didi

android - GridView 中的 EditText : How to get value from Multiple EditText

转载 作者:太空宇宙 更新时间:2023-11-03 11:14:16 25 4
gpt4 key购买 nike

我使用以下 XML 代码创建了 GridView:

grid_layout.xml

<GridView
android:id="@+id/productList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:layout_marginBottom="10dp"
android:drawSelectorOnTop="true"
android:fastScrollEnabled="true"
android:horizontalSpacing="10dp"
android:listSelector="@drawable/list_view_selector"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>

<!-- android:listSelector="@drawable/list_view_selector" is not affecting -->

并且我已将 GridView 与以下布局绑定(bind):

grid_inner_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/productChildView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/login_bg"
android:gravity="center"
android:padding="5dp" >

<ImageView
android:id="@+id/productImage"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_alignLeft="@+id/productName"
android:layout_alignRight="@+id/productName"
android:contentDescription="@string/date_desc"
android:src="@drawable/ic_date" />

<TextView
android:id="@+id/productName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/productImage"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:ellipsize="marquee"
android:gravity="center_horizontal"
android:singleLine="true"
android:text="@string/product_name"
android:textColor="@android:color/black"
android:textSize="@dimen/list_item_text_size" />

<LinearLayout
android:id="@+id/productEditTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/productName" >

<EditText
android:id="@+id/productRateValue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/edit_text_holo_light"
android:gravity="right"
android:hint="@string/hint_rate"
android:inputType="number"
android:nextFocusRight="@+id/productDiscountValue"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black" >
</EditText>

<EditText
android:id="@+id/productDiscountValue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/edit_text_holo_light"
android:gravity="right"
android:hint="@string/hint_disc"
android:inputType="number"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black" />
</LinearLayout>

<EditText
android:id="@+id/productQuantityValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/productEditTextView"
android:layout_marginRight="5dp"
android:background="@drawable/edit_text_holo_light"
android:gravity="right"
android:hint="@string/hint_quantity"
android:inputType="number"
android:nextFocusRight="@+id/productRateValue"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black" >

<requestFocus />
</EditText>

</RelativeLayout>

我终于得到了这个观点:

Preview Image

我的适配器类是这样的:

public class MyGridViewAdapter extends SimpleCursorAdapter {

Cursor cursor;

double productRateValue;
double productDiscountValue;

ImageLoader mImageLoader = new ImageLoader(context);

public MyGridViewAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
// TODO Auto-generated constructor stub
//get reference to the row
this.cursor = c;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View view = convertView;
ViewHolder holder;

if(convertView == null)
{
//get reference to the row
view = super.getView(position, convertView, parent);

holder = new ViewHolder();

productRateValue = cursor.getDouble(productCursor.getColumnIndex("rate"));
productDiscountValue = cursor.getDouble(productCursor.getColumnIndex("discount"));

//*** Image ***//
holder.prodImage = (ImageView) view.findViewById(R.id.productImage);
String path = productCursor.getString(productCursor.getColumnIndex(DatabaseHelper.PRODUCT_IMAGES));
mImageLoader.DisplayImage(path, holder.prodImage);

holder.prodName = (TextView) view.findViewById(R.id.productName);
holder.prodName.setText(productCursor.getString(productCursor.getColumnIndex(DatabaseHelper.PRODUCT_NAME)));

holder.prodQty = (EditText) view.findViewById(R.id.productQuantityValue);
holder.prodQty.setText("");

holder.prodRate = (EditText) view.findViewById(R.id.productRateValue);
holder.prodRate.setText(String.valueOf(productRateValue));

holder.prodDisc = (EditText) view.findViewById(R.id.productDiscountValue);
holder.prodDisc.setText(String.valueOf(productDiscountValue));

holder.prodRate.setFocusable(isRateEditable);
holder.prodRate.setEnabled(isRateEditable);

holder.prodDisc.setFocusable(isDiscountEditable);
holder.prodDisc.setEnabled(isDiscountEditable);

/* For set Focus on Next */
if(isRateEditable)
holder.prodQty.setNextFocusDownId(R.id.productRateValue);
else if(isDiscountEditable)
holder.prodQty.setNextFocusDownId(R.id.productDiscountValue);

if(isDiscountEditable)
holder.prodRate.setNextFocusDownId(R.id.productDiscountValue);

holder.prodDisc.setNextFocusDownId(R.id.productQuantityValue);

view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}

return view;
}

public class ViewHolder{
ImageView prodImage;
TextView prodName;
EditText prodRate;
EditText prodQty;
EditText prodDisc;
}
}

现在我的问题是,当我点击ORDER 选项卡时,我想保存所有输入“数量”的数据,我已经覆盖了onPause()onStop() 方法来调用 saveData() 但我可以在 saveData() 方法中做什么

as per my view i want to create JSONObject and store all value as an Array of JSON.

请帮忙...

提前致谢...

最佳答案

根据我的问题,我成功将数据保存在 JSONArray 中但又得到了 This Problem

最后我找到了解决方案:

我用过 BaseAdapter 而不是 SimpleCursorAdapter

MyGridViewAdapter.java

class MyGridViewAdapter extends BaseAdapter {

private ArrayList<ProductItems> productItemList;
private LayoutInflater inflater = null;

ViewHolder holder;

ImageLoader mImageLoader;

int concatid;

String productQtyValue;
Double productRateValue;
Double productDiscountValue;

int tempId = 0;

public MyGridViewAdapter(ArrayList<ProductItems> productItemsList) {
// TODO Auto-generated constructor stub
this.productItemList = productItemsList;
mImageLoader = new ImageLoader(context);
inflater =(LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return productItemList.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return productItemList.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertView;

if(convertView == null)
{
view = inflater.inflate(R.layout.list_product_view, null);
holder = new ViewHolder();

}else
{
holder = (ViewHolder) view.getTag();
}

ProductItems currentProductItem = productItemList.get(position);

holder.prodId = currentProductItem.getProdId();
holder.prodImagePath = currentProductItem.getProdImagePath();
holder.prodDesc = currentProductItem.getProdDesc();

//Image
holder.prodImage = (ImageView) view.findViewById(R.id.productImage);
mImageLoader.DisplayImage(holder.prodImagePath, holder.prodImage);
holder.prodImage.setTag(holder);

holder.prodImage.setOnClickListener(new OnClickListener() {

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

ViewHolder tempHolder = (ViewHolder)v.getTag();

AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
alertDialog.setTitle(tempHolder.prodName.getText());
LayoutInflater inflater = getActivity().getLayoutInflater();

// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog
// layout
View view = inflater.inflate(R.layout.custom_image, null);

ImageView image = (ImageView) view.findViewById(R.id.dialogImageView);
mImageLoader.DisplayImage(tempHolder.prodImagePath, image);

((TextView)view.findViewById(R.id.dialogTextViewDesc)).setText(tempHolder.prodDesc);

alertDialog.setView(view);

alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
dialog.dismiss();
}
});

alertDialog.show();
}
});

holder.prodName = (TextView) view.findViewById(R.id.productName);
holder.prodName.setText(currentProductItem.getProdName());

holder.prodQty = (EditText) view.findViewById(R.id.productQuantityValue);
holder.prodQty.setTag(holder);

holder.prodQty.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus)
{
ViewHolder viewHolder = (ViewHolder) v.getTag();
saveData(viewHolder);
}
}
});

holder.prodRate = (EditText) view.findViewById(R.id.productRateValue);
holder.prodRate.setTag(holder);

holder.prodDisc = (EditText) view.findViewById(R.id.productDiscountValue);
holder.prodDisc.setTag(holder);

/** First check whether value of Saved product Array is >0 or not..*/
Log.d("msg", "Saved Prod : "+allProductArray.length());
if(allProductArray.length() <= 0)
{
productRateValue = currentProductItem.getProdRate();
productDiscountValue = currentProductItem.getProdDisc();
}

/* Filling up value if previously entered... */
for (int array = 0; array < allProductArray.length(); array++) {
try {
if(allProductArray.getJSONObject(array).getInt("prodid") == holder.prodId)
{
productRateValue = allProductArray.getJSONObject(array).getDouble("rate");
productDiscountValue = allProductArray.getJSONObject(array).getDouble("discount");
productQtyValue = allProductArray.getJSONObject(array).getString("qty");
holder.prodQty.setText(String.valueOf(productQtyValue));
break;
}else
{
productRateValue = currentProductItem.getProdRate();
productDiscountValue = currentProductItem.getProdDisc();

holder.prodQty.setText("");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.e("msg", "JSON error in bindView : " + e.getMessage());
}
}

//holder.prodQty.setText("");
holder.prodRate.setText(String.valueOf(productRateValue));
holder.prodDisc.setText(String.valueOf(productDiscountValue));

holder.prodRate.setFocusable(isRateEditable);
holder.prodRate.setEnabled(isRateEditable);

holder.prodDisc.setFocusable(isDiscountEditable);
holder.prodDisc.setEnabled(isDiscountEditable);

// For set Focus on Next
if(isRateEditable)
holder.prodQty.setNextFocusDownId(R.id.productRateValue);
else if(isDiscountEditable)
holder.prodQty.setNextFocusDownId(R.id.productDiscountValue);

if(isDiscountEditable)
holder.prodRate.setNextFocusDownId(R.id.productDiscountValue);

holder.prodDisc.setNextFocusDownId(R.id.productQuantityValue);

int catid = currentProductItem.getProdId();
concatid = Integer.parseInt(catid + "" + tempId++);

Log.d("msg", "Inner Concat ID :"+concatid);

view.setId(concatid);
view.setTag(holder);

return view;
}

public class ViewHolder{
int prodId;
String prodImagePath;
String prodDesc;
ImageView prodImage;
TextView prodName;
EditText prodRate;
EditText prodQty;
EditText prodDisc;
}
}

我已经转换 Cursor ArrayList<ProductItems> :

if (!dbHelper.db.isOpen())
dbHelper.open();

productCursor = dbHelper.getProduct(companyid, categoryid, isDistributor);

// String[] cols = new String[] { DatabaseHelper.PRODUCT_IMAGES, DatabaseHelper.PRODUCT_NAME, "rate", "discount"};
// int[] views = new int[] { R.id.productImage, R.id.productName, R.id.productRateValue, R.id.productDiscountValue};

Log.d("msg", "Count of Product : "+productCursor.getCount());

prodItemsArrayList.clear();

if (productCursor != null && productCursor.getCount() > 0) {

ProductItems items;

for(productCursor.moveToFirst(); !productCursor.isAfterLast(); productCursor.moveToNext())
{
items = new ProductItems();

items.setProdId(productCursor.getInt(productCursor.getColumnIndex(DatabaseHelper.PRODUCT_SERVER_ID)));
items.setProdName(productCursor.getString(productCursor.getColumnIndex(DatabaseHelper.PRODUCT_NAME)));
items.setProdImagePath(productCursor.getString(productCursor.getColumnIndex(DatabaseHelper.PRODUCT_IMAGES)));
items.setProdQty("");
items.setProdRate(productCursor.getDouble(productCursor.getColumnIndex("rate")));
items.setProdDisc(productCursor.getDouble(productCursor.getColumnIndex("discount")));
items.setProdDesc(productCursor.getString(productCursor.getColumnIndex(DatabaseHelper.PRODUCT_DESC)).equals("null") ? "" : productCursor.getString(productCursor.getColumnIndex(DatabaseHelper.PRODUCT_DESC)));

prodItemsArrayList.add(items);
}

//Now create an array adapter and set it to display using our row
//passing cursor with zero rows and displaying error message
adapter = new MyGridViewAdapter(prodItemsArrayList);
adapter.notifyDataSetChanged();
productTable.setAdapter(adapter);

/* HIDE ERROR MSG */
(view.findViewById(R.id.productIfNoAvailable)).setVisibility(View.GONE);
} else {
/* SHOW ERROR MSG */
(view.findViewById(R.id.productIfNoAvailable)).setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
adapter.notifyDataSetInvalidated();
}

任何建议将不胜感激:)

谢谢...

关于android - GridView 中的 EditText : How to get value from Multiple EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19196322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com