gpt4 book ai didi

android - 在 ListView 项的子项上单击

转载 作者:行者123 更新时间:2023-11-30 03:35:39 25 4
gpt4 key购买 nike

我有一个 ListView,其中每个项目都由一些 ImageViewsTextViews 组成,我希望当我点击一个特定的 ImageView 时,一些代码将被执行,我应该把这段代码放在哪里是在 onItemClick 方法中吗?

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

getView代码:

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

最佳答案

假设您的 getView 有一个 textView 和一个 imageView

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.xml, parent, false);
// setup view bindings
TextView textView = view.findViewById(R.id.textView);
ImageView imageView = view.findViewById(R.id.imageView);

setClickListener(textView, position, parent);
setClickListener(imageView, position, parent);
}

private void setClickListener(View view, final int position, final ViewGroup parent){
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// this part is important, it lets ListView handle the clicks
((ListView) parent).performItemClick(v, position, 0);
}
});
}

现在在 ActivityFragment

ListView listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long viewId = view.getId();
if (viewId == R.id.textView) {
Toast.makeText(Activity.this, "TextView Clicked", Toast.LENGTH_SHORT).show();
} else if (viewId == R.id.imageView) {
Toast.makeText(Activity.this, "ImageView Clicked", Toast.LENGTH_SHORT).show();
}
}
});

关于android - 在 ListView 项的子项上单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16674000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com