我有一個ListActivity使用自定義ArrayAdapter工作一個奇怪的問題定義適配器按鈕的點擊。 列表項通過定製的getView綁定到數據,並且包含幾個文本視圖和一個按鈕。 當點擊該項目的按鈕,我打開一個新的活動根據按鈕按下標籤,其中包含數據項目ID。問題與ListActivity
事情是, 有時點擊某個項目的按鈕實際上會執行點擊其他項目的點擊,或者看起來像這樣。 這真的很奇怪,它表現得好像我點擊的按鈕失去了焦點,而是被點擊了。 當使用標準按鈕其實我可以看到有某種混亂爲重點,從一個跳到另一個列表按鈕。
我還沒有想出的情景,但我懷疑這是某種方式與在後臺的多次調用getView。
這是我的適配器的getView實現:
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
Context context = App.getContext();
if (v == null)
{
// if the view is empty, init
v = mInflater.inflate(R.layout.list_item, null);
// add click listener to the item button
Button b = (Button)v.findViewById(R.id.b);
OnClickListener lsn = new OnClickListener()
{
@Override
public void onClick(View view)
{
// when clicked we want to open the new activity with the selected id
int id = Integer.parseInt(view.getTag().toString());
Intent i = new Intent(mActivityContext, otherActivity.class);
i.putExtra("ID", id);
mActivityContext.startActivity(i);
}
};
b.setOnClickListener(lsn);
}
ItemData currItem = m_Items.get(position);
// Some fields data bindings to currItem
Button btn = (Button)v.findViewById(R.id.b);
btn.setEnabled(currItem.isButtonEnabled);
btn.setTag(currItem.getID());
return v;
}
這是我的名單上定義的佈局:
<ListView android:layout_width="fill_parent" android:id="@+id/android:list"
android:fadingEdge="none" android:divider="@color/transparent" android:dividerHeight="2dp"
android:background="@color/transparent" android:focusable="false"
android:cacheColorHint="@color/transparent" android:layout_height="fill_parent"></ListView>
有什麼想法? 謝謝!
重要更新:
一些測試,我注意到,有什麼項目是越來越錯誤地點擊一些一致性之後。它始終是鏡子項目。即當點擊列表中的第一個項目時,當我收到其他項目時,它總是最後一個!
e.g如果在列表中的3項,第一個和最後一個有時稱呼對方,但從來沒有中間項,如預期的中間項目始終工作。 好像某處有另一個列表的項目反向順序,有時我得到鏡像索引,而不是常規和可見的一個...
這真的很奇怪!我很可能在做一些非常錯誤的事情,但我無法想象會發生這種情況。
我已經轉換陣列適配器的一類擴展baseAdapter用的ArrayList但行爲繼續...
最後更新... 所以我發現了這個問題,只有當發生notifyDatasetChanged正在後臺運行。但是我只能每2秒減少一次呼叫。這導致的問題是很難趕上,但它仍然存在...
任何線索?
在對陣列中的對象列表進行任何更改後,問題是否存在?如果是這樣,你刷新listView? – Wizetux
我最困惑的事實是有兩個引用的按鈕。會觸發click事件的按鈕(由R.id.b引用)與貼有標籤的按鈕不同(由R.id.button引用)。我沒有看到活動是如何以正確的id值開始的! – Devunwired
你可以添加ListActivity的完整代碼嗎?問題可能在於如何調用getView方法/ setup。 – Bryan