On a ListView item's child click

Hossam Oukli picture Hossam Oukli · May 21, 2013 · Viewed 7.2k times · Source

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

Answer

Md Abdul Gafur picture Md Abdul Gafur · May 21, 2013

You put this code in

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

}

or you can write something like that

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, final View view,
          int position, long id) {
        final String item = (String) parent.getItemAtPosition(position);
        view.animate().setDuration(2000).alpha(0)
            .withEndAction(new Runnable() {
              @Override
              public void run() {
                list.remove(item);
                adapter.notifyDataSetChanged();
                view.setAlpha(1);
              }
            });
      }

    });