为所选项设置OnClickListener

时间:2017-02-02 20:03:27

标签: android listview android-json

我有一些从mysql数据库收集的数据到listview(这是一个铃声应用程序).​​.现在我需要在播放imageButon上为所选项目设置onClickListener .. 我知道如何设置onClickListener,但在这种情况下我不知道如何为所选项设置它.. this is a view of my app

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

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));

  }



}

1 个答案:

答案 0 :(得分:0)

像这样更改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();
        }
}