2012-12-16 64 views
1

我为我的列表视图有一个自定义布局项目。在布局项目中有一个微调器需要填充值,通常由android:entries
我用这种方法的问题是最终用户无法修改值,这是我想包括的。随着布局项目和随后的微调,在同一个列表视图上重复多次,我想必须有一种方法来以编程方式填充它一次。我无法弄清楚。具有自定义布局的微调器的动态填充

XML:

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

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/exerciselbl" /> 

     <Spinner 
      android:id="@+id/Exercise" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:entries="@array/workout_items" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/repslbl" /> 

     <Spinner 
      android:id="@+id/Reps" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:entries="@array/reps_count" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/weightlbl" /> 

     <EditText 
      android:id="@+id/Weight" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:inputType="number" /> 
    </LinearLayout> 

</LinearLayout> 

所以,是的,我想避免使用android:entries="@array/workout_items"因为这意味着手动在XML文件中的资源打字的微调每一个项目,我不能动态地添加项目,而该程序正在运行。

回答

0

下面是一个简单的例子,它显然可以改变泛型,并且还有其他加载微调器的方式。

public void populateSpinner() { 
    Spinner spinner = (Spinner) findViewById(R.id.spinner1); 
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item); 
    spinner.setAdapter(adapter); 
    List<CharSequence> list = new ArrayList<CharSequence>(); 
    for (int i = 0; i < 10; i++) { 
     list.add("Item " + i); 
    } 
    adapter.addAll(list); 
    adapter.notifyDataSetChanged(); 
} 

一个比较重要的线是adapter.notifyDataSetChanged(),这是因为它告诉它需要刷新这个微调相关的数据视图。

欲了解更多信息,请参阅ArrayAdapterSpinner类。

+0

他谈论'Spinner'正在行布局中,并且您的当前代码不适用于此。另外,add()和addAll()(API级别为11,如果我没弄错的话)已经调用了'notifyDataSetChanged()',所以最后一次调用没有用处。 – Luksprog

+0

我没有意识到'''add()''''和'''addAll()'''会'''notifyDataSetChanged()'''',这似乎并没有在API中记录。我相信我的例子依然如此,除了用这种方法找到微调器之外。相反,我认为你会做类似于: 'LinearLayout row =((ListView)findViewById(R.id.listview1))。getItemAtPosition(position);''' ''(Spinner)row。 findViewById(R.id.spinner1);''' 也就是说,我没有时间去测试它,所以这只是猜测。 – TomJ

+0

非常感谢,这和我一直在做的事情类似,我只是无法让它工作。这种方法的关键在于View实际上已经被初始化了。在onCreate()','onStart()'或'onResume()'试图找到微调不工作,因为视图不可见... 我发现的问题是,我仍然必须为ListView中的每个Spinner做这件事,我希望有更通用的东西,比如在'onCreate()'中创建的资源,可以通过XML或类似的东西引用(我认为这实际上不可行...我已经看过)。 – Trent