来自另一个XML文件的onClickListener()

尝试从MainActivity文件中的其他XML获取ImageView onClickListener,以便在单击时执行某些操作 . 这里的示例是Toast消息未显示 . 示例代码:

//View Window from Main Activity
    setContentView(R.layout.activity_main);

    // Inflate the view
    final View addView = getLayoutInflater().inflate(R.layout.places_list_2, null);
    // Locate the ImageView in the inflated View
    final ImageView places = (ImageView) addView.findViewById(R.id.imageViewGoogle);
    places.setClickable(true);
    places.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Open Google Maps.",
                    Toast.LENGTH_LONG).show();
        }
    });

更新:我尝试使用适配器类与View Holder一起工作 . 我做的样本:

@Override
  public void onBindViewHolder(ProductViewHolder holder, int position) {
    //getting the product of the specified position
    Product product = productList.get(position);

    //binding the data with the viewholder views
    holder.textViewClinicName.setText(product.getClinicName());              
    holder.imageViewGoogle.setClickable(true);
    holder.imageViewGoogle.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.d("myTag", "This is my message");
        }
    });

}

回答(0)