gpt4 book ai didi

java - ListView 适配器行为异常。在我单击同一个 ListView 中的按钮后,ListView 中不相关的按钮被单击

转载 作者:太空狗 更新时间:2023-10-29 13:15:57 28 4
gpt4 key购买 nike

So, what I want is to individually disable a button for the concerned item in the ListView when clicked on the button itself. This should work for multiple buttons as well.

I tried many places for solutions but didn't get a good enough answer.

What's happening is when i click on a button in the listview, i see other buttons getting clicked automatically when i scroll down to see other buttons which is quite weird and not helping the situation.

这是适配器。

public class ListViewAdapter extends BaseAdapter implements ListAdapter {

private ArrayList<String> list = new ArrayList<String>();
private Context context;
private StringBuilder user_id;
private Button sendReq;
private ListViewAdapter adapter;
private JSONArray peopleArray;
private int selectedIndex;
private ViewHolder viewHolder;
private static boolean[] buttonsClicked;
private View rowView;

public ListViewAdapter(ArrayList<String> list, Context context,String user_id,JSONArray jsonArray) {
this.user_id = new StringBuilder(user_id.toString());
this.list = list;
this.context = context;
peopleArray = jsonArray;
selectedIndex = -1;
this.adapter = this;
buttonsClicked = new boolean[list.size()];
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int pos) {
return list.get(pos);
}

@Override
public long getItemId(int pos) {
//return list.get(pos).getId();
//just return 0 if your list items do not have an Id variable.
return 0;
}

@Override
public boolean isEnabled(int position){
return false;
}

static class ViewHolder{
public TextView ItemText;
public Button ItemButton;
}

public void setSelectedIndex(int index){
selectedIndex = index;
notifyDataSetChanged();
}

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

if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.my_custom_list_layout, null);
//configure view holder
viewHolder = new ViewHolder();
viewHolder.ItemText = (TextView) rowView.findViewById(R.id.list_item_text);
viewHolder.ItemButton = (Button) rowView.findViewById(R.id.list_item_button);
rowView.setTag(viewHolder);
}
else {
//fill data
viewHolder = (ViewHolder) rowView.getTag();
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)rowView.findViewById(R.id.list_item_text);
listItemText.setText(list.get(position));

//Handle buttons and add onClickListeners
sendReq = (Button) rowView.findViewById(R.id.list_item_button);

viewHolder.ItemButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something
//list.remove(position); //or some other task

Log.d("Button position: ", " " + position);
((Button)v).setEnabled(false);
notifyDataSetChanged();
}
});

return rowView;
}
}

这是布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:weightSum="100"
android:orientation="horizontal">

<ImageView
android:id="@+id/list_item_image"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="20"
android:src="@drawable/default_picture"/>

<TextView
android:id="@+id/list_item_text"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_weight="50"
android:textSize="18sp"
android:textStyle="bold" />

<Button
android:id="@+id/list_item_button"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="30"
android:text="Send Req"
android:focusable="false"/>

</LinearLayout>

这就是我在 Activity 中使用适配器的方式

arrayList.add("Never");
arrayList.add("let");
arrayList.add("anyone");
arrayList.add("make");
arrayList.add("you");
arrayList.add("that");
arrayList.add("you");
arrayList.add("have");
arrayList.add("limits");
myAdapter = new ListViewAdapter(arrayList,this,"",jsonArray);
listview.setAdapter(myAdapter);

最佳答案

当你点击一个按钮时,它会禁用一个Button .然后,当你滚动时,那个 Button将被其他元素回收和再利用。你永远不会重新启用那个 Button ,所以过了一会儿,上下滚动,你所有的Button s 将被禁用。

这不是执行此操作的正确方法。您不能在任何 View 中进行更改基于用户所做的某些操作(如单击按钮),因为 View s 在 ListView所有这些都被回收并重新用于不同的列表项。当用户执行某些操作时,您需要更改您的数据。在 getView()您需要使用数据 来设置View 的状态

在你的情况下,你可以这样做:

  • 添加 private boolean[] enabled数组作为适配器中的成员变量。
  • 像这样在适配器构造函数中初始化数组:

    enabled = new boolean[list.size()];
    // Enable all items
    for (int k = 0; k < enabled.length; k++) {
    enabled[k] = true;
    }

  • 在你的onClick()方法,执行此操作而不是禁用 Button :

    enabled[position] = false;
    notifyDataSetChanged();

  • getView() , 设置 Button启用状态基于您的数据,如下所示:

    viewHolder.ItemButton.setEnabled(enabled[position]);

关于java - ListView 适配器行为异常。在我单击同一个 ListView 中的按钮后,ListView 中不相关的按钮被单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34679847/

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