Note that there are some explanatory texts on larger screens.

plurals
  1. POMake a list View row selected
    primarykey
    data
    text
    <p>I have an <code>AlertDialog</code> which contains a <code>ListView</code>. The <code>ListView</code> is populated via a customCursor adapter. Everything works fine except that I am unable to set a particular row as selected(highlighted with the blue color in Holo theme) inside the <code>ListView</code>. </p> <pre><code>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&lt;?&gt; 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; } </code></pre> <p>Here is the adapter used to populate the listView that is inside the alert Dialog.</p> <pre><code>public class AccountsAdapter extends CursorAdapter { private Context m_context; /**&lt;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 } } </code></pre> <p>The custom row of my list view.</p> <pre><code>&lt;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" &gt; &lt;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" /&gt; &lt;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" /&gt; </code></pre> <p></p> <p>I tried,</p> <pre><code>listView.setItemChecked(position, true); listView.setSelection(position); </code></pre> <p>in the function which creates the alert dialog but it did not work. The other option I have is to manually set a background color to the row View in my adapter, which of course is not advisable as the theme may change from device to device.</p> <p>Thanks in advance and sorry for a lengthy post.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload