我有一个AlertDialog
包含一个ListView
. 通过ListView
customCursor 适配器填充。一切正常,除了我无法在ListView
.
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);
在创建警报对话框但它不起作用的函数中。我的另一个选择是手动为我的适配器中的行视图设置背景颜色,这当然是不可取的,因为主题可能会因设备而异。
在此先感谢,并为冗长的帖子感到抱歉。