为所选项目设置 OnClickListener

set OnClickListener for selected item

我从 mysql 数据库中收集了一些数据到列表视图中(这是一个铃声应用程序)。现在我需要在播放 imageButon 时为所选项目设置 onClickListener .. 我确实知道如何设置 onClickListener,但在这种情况下我不知道如何为所选项目设置它..

这就是我到目前为止所做的:

public class ListActivity extends AppCompatActivity implements AsyncResponse
,AdapterView.OnItemClickListener{

private boolean paused = true;

URL url;
Bitmap bmImg = null;



public static final int NOTIFICATION_ID = 1;

final String LOG = "ListActivity";

private ArrayList<Product> productList;
private ListView lvProduct;
private FunDapter<Product> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.d_activity_list_d);


    ImageLoader.getInstance().init(UILConfig.config(ListActivity.this));

    PostResponseAsyncTask taskRead = new PostResponseAsyncTask(ListActivity.this, this);

     taskRead.execute("http://symphonyrecords.6te.net/product.php");
    lvProduct = (ListView)findViewById(R.id.lvProduct);
    registerForContextMenu(lvProduct);


}//onCreate


@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.list_context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    final Product selectedProduct = adapter.getItem(info.position);
    Log.d(LOG, selectedProduct.name);

    if(item.getItemId() == R.id.menuUpdate){
        Intent in = new Intent(ListActivity.this, DetailActivity.class);
        in.putExtra("product", selectedProduct);
        startActivity(in);
    }


    return super.onContextItemSelected(item);
}

@Override
public void  processFinish(String s) {
    productList = new JsonConverter<Product>().toArrayList(s, Product.class);

    BindDictionary<Product> dict = new BindDictionary<Product>();
    dict.addStringField(R.id.tvName, new StringExtractor<Product>() {
        @Override
        public String getStringValue(Product product, int position) {
            return product.name;
        }
    });




    adapter = new FunDapter<>(
            ListActivity.this, productList, R.layout.d_layout_list_d, dict);


    lvProduct.setAdapter(adapter);
    lvProduct.setOnItemClickListener(this);

}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Product selectedProduct = productList.get(position);

}




public void playAndPause(View view1){

            ImageButton button = (ImageButton ) findViewById(R.id.playPause);
    int icon;

    if (paused) {
        paused = false;
        icon = R.drawable.ic_pause;
        Toast.makeText(ListActivity.this, "Playing", Toast.LENGTH_LONG).show();
    }
    else {
        paused = true;
        icon = R.drawable.ic_play;
        Toast.makeText(ListActivity.this, "Stopped", Toast.LENGTH_LONG).show();
    }

    button.setImageDrawable(
        ContextCompat.getDrawable(getApplicationContext(), icon));

  }



}

像这样更改 getView 方法

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

        // Inflate a new view or use a recycled view.
        View v = convertView;
        GenericViewHolder holder;
        if (null == v) {
            LayoutInflater vi =
                    (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(mLayoutResource, null);
            holder = new GenericViewHolder();
            holder.root = v;
             // There must be view name of that play button
            holder.playButton.setOnClickListener(new View.OnClickListener(){
  public void onClick(View v)
       //do your task over here
        }

            // init the sub views and put them in a holder instance
            FunDapterUtils.initViews(v, holder, mBindDictionary);

            v.setTag(holder);
        } else {
            holder = (GenericViewHolder) v.getTag();
        }
}