gpt4 book ai didi

android - 在我的列表适配器中设置一个激活的按钮也会激活下面的每第 6 个按钮

转载 作者:行者123 更新时间:2023-11-29 01:51:20 25 4
gpt4 key购买 nike

我有一个显示客户请求的自定义 ListAdapter,每行中有三个 ImageButton,允许您分别设置已完成、正在处理或取消的状态。 ImageButtons 设置为像单选按钮一样工作,其中每行只能激活一个,并且每个按钮都标记有消息 ID 和所需的状态。

当按钮被激活/停用时,我从一个名为 AppInfo.messageButtons 的全局可用数组列表中添加/删除它们。稍后我遍历 ArrayList 以确定要对哪些请求采取行动,以及应该执行什么行动。按钮可以单击一次选择,再次单击取消选择。从最终出现在我的 messageButtons 数组列表中的角度来看,这部分似乎工作正常。

我遇到的问题是 ImageButtons 在没有被点击的情况下获得激活状态。事实上,对于我激活的每个 ImageButton,同一列中它下方的每第 6 个按钮都会被激活,就好像它们是链接的一样。这与请求的数量无关。我用 6 个请求完成了,我用 30 个完成了。

例如,此图表描述了将请求 1 设置为“已完成”和将请求 3 设置为“取消”的结果。仅点击 2 次,但有 5 个项目显示为已激活。只有最初的两次点击最终出现在我的 AppInfo.messageButtons 数组列表中,但如果我再次点击 Request 7 Completed(停用),它也会停用 1 和 13。因此除了令人困惑之外,它还搞砸了标记消息。

            Complete   InProcess   Cancel
Request 1 X - -
Request 2 - - -
Request 3 - - X
Request 4 - - -
Request 5 - - -
Request 6 - - -
Request 7 X - -
Request 8 - - -
Request 9 - - X
Request 10 - - -
Request 11 - - -
Request 12 - - -
Request 13 X - -
Request 14 - - -

谁能解释一个按钮的 onClick 处理程序如何激活下面六行的按钮?为什么每第 6 个按钮?这让我很困惑。

这是我的自定义适配器的代码:

public class EloquenceMessageAdapterStaff
extends ArrayAdapter<GlobalMessage>
{
ArrayList<GlobalMessage> roomMessages;

public EloquenceMessageAdapterStaff(Context context, int textViewResourceId, ArrayList<GlobalMessage> roomMessages){
super(context, textViewResourceId, roomMessages);

this.roomMessages = roomMessages;

int count = roomMessages.size();
Log.i("Eloquence","roomMessages adapter count=" + Integer.toString(count) );
}

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

View v = convertView;

if(v == null){
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.messagestaff, null);
}

GlobalMessage md = roomMessages.get(position);

if (md != null) {

int messageId = md.getId();
String messageIdString = Integer.toString(messageId);
String messageText= md.getText();
Date messageTime = md.getInitiatedTime();
PNMCode messagePnm = md.getPnmCode();
String messageFullText = messagePnm.getFullText();


TextView mp_text = (TextView) v.findViewById(R.id.mp_text);
TextView mp_timedate = (TextView) v.findViewById(R.id.mp_timedate);

DateFormat dateformat = new SimpleDateFormat("h:mm a, E d");
String datetext = dateformat.format(messageTime);

if (mp_text != null){
mp_text.setText( messageFullText );
}
if (mp_timedate != null){
mp_timedate.setText( datetext );
}

final ImageButton button_completed = (ImageButton) v.findViewById(R.id.button_messagestatus_Completed);
final ImageButton button_inprocess = (ImageButton) v.findViewById(R.id.button_messagestatus_InProcess);
final ImageButton button_cancel = (ImageButton) v.findViewById(R.id.button_messagestatus_Cancel);

button_completed.setTag(R.id.tag_message_id,messageIdString);
button_completed.setTag(R.id.tag_message_status,"Completed");

button_inprocess.setTag(R.id.tag_message_id,messageIdString);
button_inprocess.setTag(R.id.tag_message_status,"InProcess");

button_cancel.setTag(R.id.tag_message_id,messageIdString);
button_cancel.setTag(R.id.tag_message_status,"Canceled");


button_completed.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if ( button_completed.isActivated() ) {
AppInfo.messageButtons.remove(button_completed);
button_completed.setActivated(false);
} else {
AppInfo.messageButtons.add(button_completed);
button_completed.setActivated(false);
button_inprocess.setActivated(false);
button_cancel.setActivated(false);
button_completed.setActivated(true);
}
}
});
button_inprocess.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if ( button_inprocess.isActivated() ) {
AppInfo.messageButtons.remove(button_inprocess);
button_inprocess.setActivated(false);
} else {
AppInfo.messageButtons.add(button_inprocess);
button_completed.setActivated(false);
button_inprocess.setActivated(false);
button_cancel.setActivated(false);
button_inprocess.setActivated(true);
}
}
});
button_cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if ( button_cancel.isActivated() ) {
AppInfo.messageButtons.remove(button_cancel);
button_cancel.setActivated(false);
} else {
AppInfo.messageButtons.add(button_cancel);
button_completed.setActivated(false);
button_inprocess.setActivated(false);
button_cancel.setActivated(false);
button_cancel.setActivated(true);
}
}
});
}
return v;
}
}

这是我的主要布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text=""
/>
</LinearLayout>

这是我的行布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
>
<TextView
android:id="@+id/mp_timedate"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text=""
android:layout_alignParentLeft="true"
/>
<TextView
android:id="@+id/mp_text"
android:layout_width="336dp"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text=""
android:layout_toRightOf="@id/mp_timedate"
/>
<ImageButton android:id="@+id/button_messagestatus_Completed"
style="@style/buttontight"
android:src="@drawable/ic_menu_mark"
android:layout_toRightOf="@id/mp_text"
android:contentDescription="Mark message completed"
/>
<ImageButton android:id="@+id/button_messagestatus_InProcess"
style="@style/buttontight"
android:src="@drawable/ic_menu_stopwatch"
android:layout_toRightOf="@id/button_messagestatus_Completed"
android:contentDescription="Mark message In Process"
/>
<ImageButton android:id="@+id/button_messagestatus_Cancel"
style="@style/buttontight"
android:src="@drawable/ic_menu_block"
android:layout_toRightOf="@id/button_messagestatus_InProcess"
android:contentDescription="Mark message cancelled"
/>
</RelativeLayout>

最佳答案

ListView 中的 View 被回收。因此,即使您的列表中有 14 个项目,实际上也只创建了 6 个 View (行)。这是 convertView 发挥作用的地方。如果您的适配器使用 convertView,则意味着它正在使用已设置了另一个项目信息的 View 。问题是转换后的 View 的按钮保持了绑定(bind)到它的前一个项目的状态。所以在 getView 中,您需要做一些事情来设置按钮的状态以匹配它们所代表的 GlobalMessage。

关于android - 在我的列表适配器中设置一个激活的按钮也会激活下面的每第 6 个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17846913/

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