gpt4 book ai didi

android - 如何更改自定义适配器中按钮单击时的按钮文本

转载 作者:行者123 更新时间:2023-11-29 20:24:54 26 4
gpt4 key购买 nike

我有用于 ListView 的自定义适配器,上面有按钮。

所以我想为位置上的特定项目更改按钮点击时的按钮文本。

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"

>

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</LinearLayout>

<Button
android:id="@+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"/>

我的自定义适配器:

static class ViewHolder {
public TextView name;
public Button btnadd;
}

@Override
public View getView(final int position, final View convertView,
ViewGroup parent) {
final ViewHolder holder;
View v = convertView;

if (v == null) {
final LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row_brandselected, null);
holder = new ViewHolder();

holder.name = (TextView) v.findViewById(R.id.name);


holder.btnadd = (Button) v.findViewById(R.id.btnadd);

v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}

UserName mUserNrand = values.get(position);

holder.name.setText(mUserBrand.getName().toString());



holder.btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(holder.btnadd.getText().toString() == "ADD"){

holder.btnadd.setText("ADDED");
notifyDataSetChanged();

}





}
});

return v;
}

当我点击按钮时,该位置上的特定项目的按钮文本没有改变。

我如何更改按钮点击特定位置的特定项目的按钮文本?

最佳答案

我认为您应该像这样更新您的 OnClick:使用 .equals 来比较字符串,否则就是在比较对象。

使用 Button btnadd = (Button)v; 确保您使用的是被点击的 View ,此 View 作为参数提供给 onClick 函数因此始终如您所愿。

holder.btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button btnadd = (Button)v;
if(btnadd.getText().toString().equals("ADD") ){
btnfollow.setText("ADDED");
notifyDataSetChanged();
}
}
});

关于android - 如何更改自定义适配器中按钮单击时的按钮文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32682692/

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