android - 制作一个列表查看选中的行

标签 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
   }
}

我的 ListView 的自定义行。

<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 设置背景颜色,这当然是不可取的,因为主题可能会因设备而异。

提前致谢,抱歉发了这么长的帖子。

最佳答案

在 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>

并将其设置为自定义行的背景资源。

关于android - 制作一个列表查看选中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14117673/

相关文章:

android - 无尽的滚动 ListView

android - 对话框 : dim everything except some region

android - 如何使用Android的(非模式)对话框?

android - 如何防止屏幕旋转时显示对话框

java - 如何从 Parse.com 数据库中检索对象并将其显示在 Android TextView 中?

android - 如何访问 Android 设备中的数据/数据文件夹?

java - Android ListView - 获取选中的项目

android - 在 `Thread.setDefaultUncaughtExceptionHandler` 中显示一个对话框

Android:如果有来电,我的 Activity 会怎样?

android - android中媒体播放器问题的多个实例