我使用數據庫中的數據創建了ExpandableListView
。爲此,我使用的是CursorTreeAdapter
,並使用包含從數據庫檢索的數據的Cursor
對象填充它。ExpandableListView顯示沒有孩子的組
我認爲,默認情況下Android會考慮沒有孩子的組「不可擴展」。
但它仍然顯示行上的展開圖標,當我點擊它時,它什麼都不做。我不想讓它顯示這個圖標。
我只想要有孩子的組顯示擴展圖標,這是不會發生的。它顯示所有行的擴展圖標(那些有子和沒有子的)。
UPDATE
我研究我的代碼,我已經看到了這個問題基本上是在設置groupIndicator
的羣體。不過,我已經試過了很多像創建一個選擇器,並將其設置方法真實繪製基於狀態和可擴展的,就像這樣:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_empty="true"
android:state_expanded="false"
android:drawable="@android:color/transparent"/>
<item android:drawable="@drawable/expander_ic_maximized" />
</selector>
但是當組被摺疊它會隱藏所有羣體,包括那些孩子(因爲Android認出倒塌組爲空)。
任何更好的方法來設置指標只爲有孩子的團體?
這是我的代碼。
public class ContactsExpandableListAdapter extends CursorTreeAdapter
{
TextView mContactNameTextView, mContactNumberTextView;
Cursor mChildrenCursor = null;
public ContactsExpandableListAdapter(Cursor cursor, Context context)
{
super(cursor, context);
}
@Override
protected Cursor getChildrenCursor(Cursor cursor)
{
if(mContactId == -1)
mContactId = null;
return mController.getContactById(mContactId);
}
public int getChildrenCount(int groupPosition)
{
return mChildrenCursor.getCount();
}
@Override
protected View newGroupView(Context context, Cursor cursor, boolean paramBoolean, ViewGroup viewGroup)
{
View view = LayoutInflater.from(context).inflate(R.layout.filterbycontact, viewGroup, false);
mContactNameTextView = (TextView) view.findViewById(R.id.contact_name);
if(cursor.getString(cursor.getColumnIndex("contact_name")) == null) mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
else
mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_name")));
view.setTag(cursor.getString(cursor.getColumnIndex("contact_id")));
return view;
}
@Override
protected View newChildView(Context context, Cursor cursor, boolean paramBoolean, ViewGroup viewGroup)
{
View view = LayoutInflater.from(context).inflate(R.layout.filterbycontact, viewGroup, false);
if(cursor.getString(cursor.getColumnIndex("contact_name")) == null)
{
mContactNameTextView = (TextView) view.findViewById(R.id.contact_name);
mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
}
else
{
mContactNumberTextView = (TextView) view.findViewById(R.id.contact_number);
mContactNumberTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
}
view.setTag(cursor.getString(cursor.getColumnIndex("contact_number")));
return view;
}
@Override
protected void bindGroupView(View view, Context paramContext, Cursor cursor, boolean paramBoolean)
{
mContactNameTextView = (TextView) view.findViewById(R.id.contact_name);
if(cursor.getString(cursor.getColumnIndex("contact_name")) == null)
mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
else
mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_name")));
view.setTag(cursor.getString(cursor.getColumnIndex("contact_id")));
}
@Override
protected void bindChildView(View view, Context context, Cursor cursor, boolean paramBoolean)
{
if(cursor.getString(cursor.getColumnIndex("contact_name")) == null)
{
mContactNameTextView = (TextView) view.findViewById(R.id.contact_name);
mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
}
else
{
mContactNumberTextView = (TextView) view.findViewById(R.id.contact_number);
mContactNumberTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
}
}
}
我該如何在無效方法中返回一個視圖? heheh也許它應該在'newGroupView()'方法中正確嗎? BTW什麼是** viewHolder **和**指標**是什麼? – rogcg
對不起。返回是錯誤的,是真的。在遊標適配器中不需要返回視圖:D。關於ViewHolder(一種在列表中加快速度的模式):http://www.youtube.com/watch?v=wDBM6wVEO70&feature=player_detailpage#t=511s –
上面的鏈接,他正在建立談論ViewHolder。然後他在http://www.youtube.com/watch解釋它?v = wDBM6wVEO70&feature = player_detailpage#t = 511s –