【问题标题】:Retrieving element in an ArrayList Adapter检索 ArrayList 适配器中的元素
【发布时间】:2018-04-04 09:08:43
【问题描述】:

我创建了一个具有图像和按钮的listview - 我正在使用ArrayAdapter 来查看listview 中的项目。

当我单击按钮时,我想获取所单击项目的详细信息。

所以我尝试了以下方法:

pd = productList.get(position);

productListArrayList<ProductDetails> productList;

getproductdetailsbuttonproductListadapter extends ArrayAdapter<ProductDetails> 中的一个按钮

holder.getproductdetailsbutton.setOnClickListener(new View.OnClickListener() 
{
     @Override
     public void onClick(View v) {

            String productID = pd.getProductID();
            String productName = pd. getProductName();

            Log.d("Value", " Product Details " + productID + " " + productName);
        }
 });

当我从第一个项目中单击按钮时,我会在日志中获得屏幕上显示的最后一个项目的详细信息。

如何获取该位置的点击项目的详细信息?

谢谢!

更新:getVIEW 代码:

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

        Typeface tf = Typeface.createFromAsset(contextValue.getAssets(), fontPath);
        Typeface tf2 = Typeface.createFromAsset(contextValue.getAssets(), fontPath2);

        ViewHolder holder = null;

        if (convertView == null)
        {

            convertView = vi.inflate(R.layout.product_details_items, null);

            holder = new ViewHolder();
            holder.image = (ImageView) convertView.findViewById(R.id.productimage);
            holder.productgetdetails = (Button) convertView.findViewById(R.id.productgetdetails);
            holder.productgetdetails.setTag(position);    

            holder.productgetdetails.setTypeface(tf2);

            convertView.setTag(holder);

            ProductDetails pd = productList.get(position);

            if (pinexist.equalsIgnoreCase(contextValue.getString(R.string.pinexistvalue)))
            {
                Log.d("Value"," - ID " + pd.getProductID());

                if (logdb.checkproductDetailsExists(pd.getProductID()) == 0)
                {
                    holder.productgetdetails.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v)
                        {
                            Log.e("Value", " = FINAL" + productList.get(position).getProductID());
                        }
                    });
                }
                else
                {
                    holder.productgetdetails.setText(contextValue.getString(R.string.nodata));
                }

            }
            else
            {
                //DO NOTHING
            }
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        ProductDetails pd = productList.get(position);

        Glide.with(getContext().getApplicationContext())
                .load(pd.getProduct_image())
                .placeholder(R.drawable.placeholder)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .into(holder.image);


        return convertView;
    }

【问题讨论】:

  • 请显示完整代码
  • 尝试在点击监听器中添加pd = productList.get(position);,这样,你会得到正确的位置。
  • 尝试 String productID = productList.get(position).getProductID();
  • @AseemSharma : 无法在 clicklistener 中添加 productList.get(Position) 因为位置不可访问。
  • @Ankit:无法在 clicklistener 中执行此操作,因为位置不可访问。

标签: android arraylist android-arrayadapter


【解决方案1】:

我猜(实际上我很有信心),您必须将pd 声明为global 变量,该变量由显示的最后一项更新,尝试将pd 移动到getView() 并将其标记为@987654325 @ 所以你可以在 onclick 中使用它,就像在

中一样
public void getView(){
   final ProductDetails pd = productList.get(position);
   // your code for click
}

【讨论】:

    【解决方案2】:

    在扩展布局以在适配器中查看时 你可以在按钮上设置OnClickListener

    public View getView(int position,View view,ViewGroup parent) {
            LayoutInflater inflater=context.getLayoutInflater();
            View rowView=inflater.inflate(R.layout.list_layout, null,true);
            final Product pd = pdList.get(position);
            //....
            Button btn= (Button) = rowView.findViewById(R.id.getproductdetailsbutton);
            btn.setOnClickListener(new View.OnClickListener() 
            {
                 @Override
                 public void onClick(View v) {
    
                        String productID = pd.getProductID();
                        String productName = pd.getProductName();
    
                        Log.d("Value", " Product Details " + productID + " " + productName);
                    }
             });
            //..
    
            return rowView;
    
        };
    

    或者你可以处理 ListView OnItemClickListener

    【讨论】:

    • 这是怎么做到的?我无法访问点击侦听器中的位置。请帮忙。
    猜你喜欢
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多