android - 在 ListView 适配器内的 setOnEditorActionListener 中传递多个值

标签 android listview

请帮帮我。

我只想知道是否可以在 onEditorAction 中的 setOnEditorActionListener 中传递两个或多个值?

正如您在代码值中看到的那样?

我在我的数量中添加了 setOnEditorActionListener..

我还想在 onEditorAction 中传递 mcustomers_basket_id 的值

        mcustomers_basket_id = customers_basket_id.getText().toString();

        quantity.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_NEXT || event.getAction() == KeyEvent.KEYCODE_BACK || event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                    String hqty;
                    hqty = v.getText().toString();
                    if (hqty.matches("") || hqty.matches("0")) {
                        Toast.makeText(context, "Please enter a valid quantity.", Toast.LENGTH_SHORT).show();
                        invalidquanity();
                    } else {
                        Toast.makeText(context, hqty+" ", Toast.LENGTH_SHORT).show();
                        //return true; // consume.

                    }
                }
                return false; // pass on to other listeners.
            }

        });

我将 listview 与数组项一起使用。

这里是完整的代码..

ListViewAdapter.java

package com.example.administrator.mosbeau;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 11/2/2015.
 */

@SuppressWarnings("deprecation")

public class ListViewAdapter extends BaseAdapter {

    boolean expanded = false;

    String mgcustomersid, mgcountryid, mproducts_id, mcustomers_basket_id;

    public static final int CONNECTION_TIMEOUT = 1000 * 15;
    public static final String SERVER_ADDRESS = "http://shop.mosbeau.com.ph/android/";

    String myJSONupdateCartQty;
    JSONArray jsonarrayupdateCartQty;

    ProgressDialog mProgressDialog;
    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public ListViewAdapter(Context context,
                           ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView products_id;
        TextView products_name;
        TextView products_price;
        ImageView products_image;
        EditText quantity;
        TextView totalprice;
        TextView pcustomersid;
        TextView pcountryid;
        TextView customers_basket_id;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.cart_listview_item, parent, false);
        // Get the position
        resultp = data.get(position);

        // Locate the TextViews in product_gridview_item.xml
        products_id = (TextView) itemView.findViewById(R.id.products_id);
        products_name = (TextView) itemView.findViewById(R.id.products_name);
        products_price = (TextView) itemView.findViewById(R.id.products_price);
        quantity = (EditText) itemView.findViewById(R.id.quantity);
        totalprice = (TextView) itemView.findViewById(R.id.products_price_total);

        // Locate the ImageView in product_gridview_item.xml
        products_image = (ImageView) itemView.findViewById(R.id.products_image);

        // Capture position and set results to the TextViews
        products_id.setText(resultp.get(CartFragment.products_id));
        products_name.setText(resultp.get(CartFragment.products_name));
        products_price.setText(resultp.get(CartFragment.products_price));
        quantity.setText(resultp.get(CartFragment.customers_basket_quantity));
        totalprice.setText(resultp.get(CartFragment.products_price_total));
        // Capture position and set results to the ImageView
        Glide.with(context).load(resultp.get(CartFragment.products_image)).diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.productloading).into(products_image);
        int color = 0xffffffff;
        itemView.setBackgroundColor(color);


        pcustomersid = (TextView) itemView.findViewById(R.id.customersid);
        pcustomersid.setText(resultp.get(CartFragment.pcustomersid));
        pcountryid = (TextView) itemView.findViewById(R.id.countryid);
        pcountryid.setText(resultp.get(CartFragment.pcountryid));

        customers_basket_id = (TextView) itemView.findViewById(R.id.customers_basket_id);
        customers_basket_id.setText(resultp.get(CartFragment.customers_basket_id));


        mgcustomersid = pcustomersid.getText().toString();
        mgcountryid = pcountryid.getText().toString();
        mproducts_id = products_id.getText().toString();
        mcustomers_basket_id = customers_basket_id.getText().toString();

        quantity.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_NEXT || event.getAction() == KeyEvent.KEYCODE_BACK || event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                    String hqty;
                    hqty = v.getText().toString();
                    if (hqty.matches("") || hqty.matches("0")) {
                        Toast.makeText(context, "Please enter a valid quantity.", Toast.LENGTH_SHORT).show();
                        invalidquanity();
                    } else {
                        Toast.makeText(context, hqty+" ", Toast.LENGTH_SHORT).show();
                        //return true; // consume.

                    }
                }
                return false; // pass on to other listeners.
            }

        });

        return itemView;
    }

    public void invalidquanity(){
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setMessage("Please enter a valid quantity.");
        dialogBuilder.setNegativeButton("OK", null);
        AlertDialog dialog = dialogBuilder.show();
        TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
        messageText.setGravity(Gravity.CENTER);
        dialog.show();
    }
}

请帮帮我..

最佳答案

I just want to know if it is possible to pass two or more value inside setOnEditorActionListener in onEditorAction?

是的,可以使用 setTag/getTag 方法。这样做:

quantity.setTag(mcustomers_basket_id);

并将值设置为:

int mcustomers_basket_id_=Integer.parseInt(v.getTag().toString());

关于android - 在 ListView 适配器内的 setOnEditorActionListener 中传递多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33495856/

相关文章:

java - Android android.R.id.list ListView fragment 异常

安卓 ListView : Center items in layout when fewer items

android - 为什么android :listSelector incorrectly change from Activity to Activity

android - 确认返回提示退出应用

javascript - 如何更改延迟加载 cordova 应用程序的时间?

android - 如何使用Zxing Library获取扫描图片和二维码

带有深度链接 : onNewIntent called multiple times 的 Android 导航组件

c# - 如何控制 Bind、ObservableCollection ListView 中显示的内容

Android ViewAnimator 和 OutOfMemoryError

JavaFX 8,带复选框的 ListView