androidlistviewchildrenonitemclicklistener

On a ListView item's child click


I have a ListView where each of its items is composed from some ImageViews and TextViews, i want that when i click on a specefic ImageView, some code will be executed, where should i put this code is it in onItemClick method?

public class EspaceClientUplodedProducts extends Activity implements OnItemClickListener{

List<Produit> lesProduits= new ArrayList<Produit>();
ListView lvListe;
ProductsDataSource produitSource;

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

    produitSource=new ProductsDataSource(this);
    produitSource.open();
    lesProduits=produitSource.getAllUploads();

    lvListe= (ListView)findViewById(R.id.UploadListView);
    UploadedAdapter adapter = new UploadedAdapter(this, lesProduits);
    lvListe.setAdapter(adapter);
    lvListe.setOnItemClickListener(this);
}



@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
{

}


@Override
protected void onPause() {
    produitSource.close();
    super.onPause();
}

the getView code:

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

    ViewHolder holder;
    if (convertView==null)
    {
        holder=new ViewHolder();
        convertView = inflater.inflate(R.layout.espaceclientuploadsource, null);
        holder.nomduProduit = (TextView)convertView.findViewById(R.id.UploadedProductName);
        holder.prixDuProduit = (TextView)convertView.findViewById(R.id.UplodedProductPrice);
        holder.imageDuProduit = (ImageView)convertView.findViewById(R.id.Uplodimage);
        holder.status = (ImageView)convertView.findViewById(R.id.UploadedStatus);
        holder.delete=(ImageView)convertView.findViewById(R.id.ImageViewDelete);
        convertView.setTag(holder);
    }

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

    Bitmap bitmapImage = BitmapFactory.decodeFile(path+File.separator+lesProduits.get(position).getImage());

    Drawable drawableImage = new BitmapDrawable(bitmapImage);
    holder.imageDuProduit.setImageDrawable(drawableImage);
    holder.nomduProduit.setText(lesProduits.get(position).getNomDuProduit());
    holder.prixDuProduit.setText(lesProduits.get(position).getPrixDuProduit());
    holder.delete.setImageResource(R.drawable.delete);

    switch (lesProduits.get(position).getStatus())
    {

    case 3://annonce Accepté
    holder.status.setImageResource(R.drawable.accepte); 
    break;

    case 2://annonce en Attente
    holder.status.setImageResource(R.drawable.enattente);   
    break;

    case 1://annonce refusé
    holder.status.setImageResource(R.drawable.refuse);
    break;

    case 0://produit vendu
    holder.status.setImageResource(R.drawable.vendu);
    break;
    }
    return convertView;
}

Solution

  • In the UploadedAdapter in getView() you must be creating the object of the ImageViews.

    Just set the onClickListener to the objects of that ImageViews and handle the click event in the onClick on the basis of the id of each ImageView.