每6個按鈕,我有一個顯示客戶的要求,並且在每行有三個ImageButtons,允許您設置的完成狀態的自定義ListAdapter,在過程,或者取消,分別。 ImageButtons設置爲像單選按鈕一樣工作,其中每行只能激活一個按鈕,並且每個按鈕都標有messageID和所需的狀態。設置在我listadapter激活的按鈕也能激活下面
當按鈕被激活/停用,我添加/從一個全局的ArrayList稱爲AppInfo.messageButtons刪除它們。我稍後迭代ArrayList以確定哪些請求將被執行,以及應執行什麼操作。按鈕可以點擊一次選擇,再次點擊取消選擇。從我的messageButtons數組列表的角度來看,這部分似乎工作正常。
我遇到的問題是與ImageButtons獲得激活狀態,而不必被點擊。實際上,對於每個ImageButton I Activate,在同一列下方的每個第6個按鈕都會被激活,就好像它們已被鏈接。這與請求的數量無關。我已用6個請求完成它,我曾與30
例如做到了,這個圖表描繪了設定請求1「已完成」的結果,並且請求3「取消」。只有2次點擊,但5個項目顯示爲已激活。只有在我AppInfo.messageButtons原來的兩個點擊結束了ArrayList的,但如果我是爲了再次單擊完成的請求7(停用),它也將停用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處理程序是如何激活下面六行按鈕?爲什麼每個第六個按鈕?這讓我感到困惑。
這裏是我的自定義適配器代碼:
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>
請在這裏看到我的回答:[列表視圖重複項,每6次](http://stackoverflow.com/a/17800061/2558882)。 – Vikram