1
我有一個AlertDialog
它包含一個ListView
。 ListView
通過customCursor適配器進行填充。一切正常,但我無法在ListView
內設置特定的行(在Holo主題中用藍色突出顯示)。列出查看行選擇
public AlertDialog m_accountsDialog;
private AlertDialog createAccountSwitcherDialog2()
{
Cursor listCursor = getDb().getAllListEntries();
if(listCursor.moveToFirst())
{
//Prepare the dialog box
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.strAccounts);
ListView listView = new ListView(this);
listView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
listView.setAdapter(new AccountsAdapter(this,this,m_ActiveId, listCursor));
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, final long rowId)
{
m_accountsDialog.dismiss();
if(m_ActiveId != (int) rowId)
{
//Do some stuff on click
}
}
});
builder.setView(listView);
builder.setPositiveButton(...);
builder.setNegativeButton(...);
m_accountsDialog = builder.create();
m_accountsDialog.setOnShowListener(...);
m_accountsDialog.show();
}
return m_accountsDialog;
}
以下是用於填充警報對話框內的listView的適配器。
public class AccountsAdapter extends CursorAdapter
{
private Context m_context; /**<Context to which we're bound*/
private Activity m_activity;
private boolean m_isTabletDevice;
private int m_ActiveId; // not used now. Can this be used to highlight the row?
public AccountsAdapter(Context context,Activity activity,int activeId, Cursor cursor)
{
super(context, cursor);
m_context = context;
m_activity = activity;
m_isTabletDevice = isTabletDevice(context);
m_ActiveId = activeId;
}
@Override
public void bindView(View rowView,final Context context, Cursor cursor)
{
if(rowView == null)
{
rowView = newView(context,cursor,null);
}
TextView tv = (TextView) rowView.findViewById(R.id.accountName);
tv.setText(cursor.getString(cursor.getColumnIndex(ProfileDatabase.COLUMN_PROFILENAME)));
ImageButton editAccountImageButton = (ImageButton) rowView.findViewById(R.id.accountEdit);
editAccountImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0)
{
Integer activeId = (Integer)arg0.getTag();
if(m_activity instanceof MainActivity)
{
((MyActivity)m_activity).m_accountsDialog.dismiss();
((MyActivity)m_activity).startEditAccountsActivity(activeId);
}
}
});
int accountId = cursor.getInt(cursor.getColumnIndex(ProfileDatabase.COLUMN_ACCOUNT_ID));
editAccountImageButton.setTag(profileId);
editAccountImageButton.setFocusable(false);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
//create and return a rowView
}
}
我的列表視圖的自定義行。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight" >
<ImageButton
android:id="@+id/accountEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/option_selector"
android:src="@drawable/edit_account_button" />
<TextView
android:id="@+id/accountName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/accountEdit"
android:bufferType="spannable"
android:ellipsize="end"
android:layout_alignParentLeft="true" />
我試過,
listView.setItemChecked(position, true);
listView.setSelection(position);
在創建警報對話框功能
,但沒有奏效。 我的另一個選擇是手動設置背景顏色到我的適配器中的行視圖,這當然不是可取的,因爲主題可能因設備而異。
在此先感謝,並對一篇冗長的文章感到抱歉。
我不認爲highlight_color是一個定義的常量。這是拋出一個錯誤@android:color/Highlight_color – yagnasri
高亮顏色應該由你在color.xml中定義爲#51ffffff –
sandy
我在問題中提到我不想硬編碼值。無論何時我顯示對話框,顏色應該與當前主題的焦點顏色相同,我都希望特定的行可以被聚焦。我只是想讓listview.setItemSelected(pos,true);上班 – yagnasri