2013-01-02 34 views
1

我有一个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); 
在创建警报对话框功能

,但没有奏效。 我的另一个选择是手动设置背景颜色到我的适配器中的行视图,这当然不是可取的,因为主题可能因设备而异。

在此先感谢,并对一篇冗长的文章感到抱歉。

回答

1

在可绘制文件夹中创建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> 

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

+0

我不认为highlight_color是一个定义的常量。这是抛出一个错误@android:color/Highlight_color – yagnasri

+0

高亮颜色应该由你在color.xml中定义为#51ffffff sandy

+0

我在问题中提到我不想硬编码值。无论何时我显示对话框,颜色应该与当前主题的焦点颜色相同,我都希望特定的行可以被聚焦。我只是想让listview.setItemSelected(pos,true);上班 – yagnasri