2012-08-22 35 views
0

我有一个ListView(lvProperties)的适配器是一个自定义ListAdapter(不是ArrayAdapter)。此适配器获取的是来自自定义类型Section的实例变量(包含几个Strings,Integers,ArrayLists和方法)的数据。Android ListView与ListAdapter,需要始终有一个选定的条目

我需要这个列表总是显示一个选择(即使在活动启动时,总​​是必须有一个选定的元素)。

在过去像这样使用一个简单的ArrayAdapter就足够了:

lvProperties.requestFocus(); 
lvProperties.setSelection(0); 

然而,在这种情况下,它不会在所有的工作。我一直在环顾世界和网络和使用:

lvProperties.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
lvProperties.setFocusable(true);    
lvProperties.setFocusableInTouchMode(true); 
lvProperties.requestFocus(); 
lvProperties.setSelection(0); 

而仍然没有。

我ListAdapter:

lvProperties.setAdapter(new ListAdapter() { 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
       View v=convertView; 

       if(v==null) { 
        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        v=vi.inflate(R.layout.detail_row1, null); 
       } 

       TextView name=(TextView)v.findViewById(R.id.propName); 
       TextView value=(TextView)v.findViewById(R.id.propValue); 

       if(name!=null) { 
        name.setText(section.propNames.get(position)); 
       } 
       if(value!=null) { 
       value.setText(section.propValues.get(position)); 
       } 

       return v; 
      } 

     @Override 
     public void unregisterDataSetObserver(DataSetObserver observer) {} 

     @Override 
     public void registerDataSetObserver(DataSetObserver observer) {} 

     @Override 
     public boolean isEmpty() { 
      return false; 
     } 

     @Override 
     public boolean hasStableIds() { 
      return false; 
     } 

     @Override 
     public int getViewTypeCount() { 
      return 2; 
     } 

     @Override 
     public int getItemViewType(int position) { 
      return 0; 
     } 

     @Override 
     public long getItemId(int position) { 
      return 0; 
     } 

     @Override 
     public Object getItem(int position) { 
      return null; 
     } 

     @Override 
     public int getCount() { 
      return section.propNames.size(); 
     } 

     @Override 
     public boolean isEnabled(int position) { 
      return true; 
     } 

     @Override 
     public boolean areAllItemsEnabled() { 
      return true; 
     } 
    }); 

我未实现OnItemSelectedListener:

lvProperties.setOnItemSelectedListener(new OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 
      // TODO Auto-generated method stub 

     } 
    }); 

而且我detail_row1.xml(我将实施以布局明年的行,但这是它是如何的现在:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:orientation="horizontal" 
    android:layout_height="wrap_content" 
    android:padding="6dip" 
    > 

    <TextView 
    android:id="@+id/propName" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"  
    android:textSize="16sp" 
    android:textStyle="bold" 
    android:textColor="#000000" 
    android:gravity="center_vertical|left" 
    android:paddingTop="10sp" 
    android:paddingBottom="10sp" 
    android:paddingLeft="4sp" 
    /> 

    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="2" 
    android:id="@+id/propValue"  
    android:textColor="#000000" 
    android:textSize="16sp" 
    android:textStyle="bold" 
    android:paddingTop="10sp" 
    android:paddingBottom="10sp" 
    android:paddingRight="4sp" 
    android:gravity="center_vertical|right" 
    /> 

</LinearLayout><?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:orientation="horizontal" 
    android:layout_height="wrap_content" 
    android:padding="6dip" 
    > 

    <TextView 
    android:id="@+id/propName" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"  
    android:textSize="16sp" 
    android:textStyle="bold" 
    android:textColor="#000000" 
    android:gravity="center_vertical|left" 
    android:paddingTop="10sp" 
    android:paddingBottom="10sp" 
    android:paddingLeft="4sp" 
    /> 

    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="2" 
    android:id="@+id/propValue"  
    android:textColor="#000000" 
    android:textSize="16sp" 
    android:textStyle="bold" 
    android:paddingTop="10sp" 
    android:paddingBottom="10sp" 
    android:paddingRight="4sp" 
    android:gravity="center_vertical|right" 
    /> 

</LinearLayout> 

任何帮助/指针指导如何做到这一点?我不想使用可绘制的,只是我正在使用的主题的默认选择器。

同样,列表中必须始终有一个选定的条目。

我认识到,通过设计这不是列出了预期的行为,但是这是,我只是复制的东西,我需要与具有的那些情况下,一个规模不适合所有

+0

“我需要此列表总是显示选择” - 选择只能通过箭头键,D-pad或其他非触摸屏指针设备显示。 “我意识到,通过设计,这不是列表的预期行为,但这是其中一种尺寸不适合所有情况的情况之一” - 如果您想解释为什么,也许我们可以找出其他一些指示方法无论你想让用户了解什么。例如,“激活”是在HC/ICS/JB上使用的用于指示通过列表选择的一些相邻材料的背景的状态。 – CommonsWare

+0

简而言之,活动中的其他元素根据列表中选择的内容填充。我将我的应用程序之一从iOS移植到Android,并希望重现(从该应用程序)http://bit.ly/NIXRkZ。我知道他们是不同的平台,但这就是我想成为的样子。 –

+0

你所描述的是“激活”,但是作为一个只存在于Android 3.0+上的概念,因此只有这些设备上的用户才能预料到。即使在这些设备上,“激活”只发生在用户点击之后,而不是之前。 – CommonsWare

回答

1

对于所有有兴趣的人一个惹ChoiceMode,requestFocus的,为setSelection,等等等等,在API 8工作正常向上

当应用程序启动我想的第一行是已经强调

selectedItem.setValue(0)//一个内部类我有,也可以是一个简单的整数

我的内部类ArrayDapter(我需要它来查看父则selectedItem所有的时间):

注:我将在未来改变硬编码的颜色,我只是解决了这个问题,我有:

public class DetailsAdapter extends ArrayAdapter<Section> { 

    public Section section; 
    public View v; 
    public Context c; 

    public DetailsAdapter(Context context, int textViewResourceId, 
      Section s) { 
     super(context, textViewResourceId); 

     this.section=s; 

     this.c=context; 

    } 

    @Override 
    public int getCount() { 
     return section.propNames.size(); 
    } 

    @Override 
    public View getView(int pos, View convertView, ViewGroup parent){ 
     this.v = convertView; 

     LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if(v==null) {  
      v=vi.inflate(R.layout.detail_row1, null); 
     } 

     **LinearLayout llc=(LinearLayout) v.findViewById(R.id.detail_container);** 
     TextView name=(TextView)v.findViewById(R.id.propName); 
     TextView value=(TextView)v.findViewById(R.id.propValue); 
     if(pos==selectedItem.value) { 
      llc.setBackgroundColor(Color.LTGRAY); 
     } else 
     { llc.setBackgroundColor(Color.WHITE);} 
     Log.e("Pos",""+pos); 

     if(llc!=null) { 

     } 

     if(name!=null) { 

      name.setText(section.namesAsArray()[pos]); 
     } 
     if(value!=null) { 
      value.setText(section.valuesAsArray()[pos]); 
     } 


     return v; 

    } 
} 

所以我可以在我的XML文件中避免使用android:state_xxxxxxx的技巧是用嵌套的LinearLayout封装我自定义的行,填充外部行。

布局该行:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:orientation="horizontal" 
android:layout_height="wrap_content" 
android:id="@+id/detail_linearLayout" 
> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="0" 
    android:id="@+id/detail_container" 
    android:padding="6dip" 
    > 

<TextView 
android:id="@+id/propName" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_weight="1"  
android:textSize="16sp" 
android:textStyle="bold" 
android:textColor="#000000" 
android:gravity="center_vertical|left" 
android:paddingTop="10sp" 
android:paddingBottom="10sp" 
android:paddingLeft="4sp" 
/> 

<TextView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_weight="2" 
android:id="@+id/propValue"  
android:textColor="#000000" 
android:textSize="16sp" 
android:textStyle="bold" 
android:paddingTop="10sp" 
android:paddingBottom="10sp" 
android:paddingRight="4sp" 
android:gravity="center_vertical|right" 
/> 

</LinearLayout> 
</LinearLayout> 

最后,我把我的OnItemClickListener:

lvProperties.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 

       **selectedItem.value=arg2; 
       lvProperties.invalidateViews();** 
         . 
            . 
            . 
        }}); 

做,做!

相关问题