列表查看选定的行

时间:2013-01-02 06:17:37

标签: android android-listview android-dialog

我有AlertDialog,其中包含ListViewListView通过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);

在创建警告对话框但不起作用的函数中。 我的另一个选择是手动将背景颜色设置为我的适配器中的行View,这当然是不可取的,因为主题可能会因设备而异。

提前致谢,并感谢抱歉。

1 个答案:

答案 0 :(得分:1)

在drawable文件夹中创建selector_row.xml,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/Highlight_color" android:state_pressed="true"/>
</selector>

并将其设置为自定义行的后台资源。