gpt4 book ai didi

java - 在listview android中多次点击

转载 作者:行者123 更新时间:2023-11-30 02:47:45 25 4
gpt4 key购买 nike

我正在做一个显示数据库用户的应用程序。我想根据每个元素中的点击区域添加不同的操作。

这是这个 ListView 的实际代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" >

<ImageView
android:id="@+id/logo"
android:layout_width="100px"
android:layout_height="100px"
android:layout_marginLeft="5px"
android:layout_marginRight="20px"
android:layout_marginTop="5px"
android:src="@drawable/icondatabase" >
</ImageView>

<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.91"
android:text="Not users"
android:textSize="70px" />

<ImageView
android:id="@+id/imageAction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>

</LinearLayout>

这是创建列表的类:

public class UsersArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private final List<Bitmap> listImages;
private final String process;

public UsersArrayAdapter(Context context, String[] values,
List<Bitmap> listImages, String process) {
super(context, R.layout.users, values);
this.context = context;
this.values = values;
this.listImages = listImages;
this.process = process;
}

public UsersArrayAdapter(Context context, String[] values) {
super(context, R.layout.users, values);
this.context = context;
this.values = values;
this.listImages = null;
this.process = "";

}

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

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.users, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
ImageView imageView2 = (ImageView) rowView
.findViewById(R.id.imageAction);
textView.setText(values[position]);
if (listImages == null) {
imageView.setImageResource(R.drawable.iconenroll);
} else {
imageView.setImageBitmap(listImages.get(position));

if (process.compareTo("users") == 0) {

imageView2.setImageResource(android.R.drawable.ic_menu_delete);
}

if (process.compareTo("verify") == 0) {
imageView2.setImageResource(android.R.drawable.ic_media_play);
}
}

return rowView;
}
}

如果用户在 imageaction 中单击执行某些操作,而在 textview 中单击执行其他操作,我该怎么办?

谢谢

最佳答案

您可以为 textview 和 imageview 设置 onclicklisteners 。但你必须使用 ViewHolders。

static class ViewHolder{
ImageView imageone;
ImageView imagetwo;
TextView tvone;
}

in the adapter getview

final ViewHolder holder;
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.custom_view_new, null);
holder = new ViewHolder();
........
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}



holder.imageone.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

// do ur work
}
}

holder.tvone.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

// do ur work
}
}

关于java - 在listview android中多次点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24736373/

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