【问题标题】:Customizing row spinner when values are fetched from database从数据库中获取值时自定义行微调器
【发布时间】:2013-01-08 20:11:21
【问题描述】:

我正在使用光标从数据库中检索值,并使用简单的适配器列表将它们显示在微调器中!一切都很好,唯一让我担心的是我想要自定义微调器下拉菜单的外观,我搜索了很多,但由于我使用数据库值而无法获得任何结果!让我知道我们是否可以自定义下拉菜单。

提前致谢!!

【问题讨论】:

    标签: android android-spinner android-sqlite


    【解决方案1】:

    一旦通过这些链接。希望它有所帮助。所有最好的伙伴

    Link 1
    Link 2

    【讨论】:

    • 谢谢。我以前浏览过这些链接。但我不知道如何使用它,同时使用简单的游标适配器从数据库中取回它们。截至目前,我正在使用这样的东西 - SimpleCursorAdapter adapter_vehicle = new SimpleCursorAdapter(this, R.layout.simple_spinner_item, c_vehicle, vehicle_name, vehicle_value);
    • 如果你发布一些代码会很好。这样我们就可以知道程序实际上在做什么。如果你使用单独的适配器,那么在 getView() 函数中我们可以自定义微调器
    • 我没有使用 getView()。不知道如何使用它。我使用的代码 String[] vehicle_name = new String[]{"Vehicle_type"}; int[] vehicle_value = new int[]{android.R.id.text1}; SimpleCursorAdapter adapter_vehicle = new SimpleCursorAdapter(this, R.layout.simple_spinner_item, c_vehicle, vehicle_name, vehicle_value); adapter_vehicle.setDropDownViewResource(android.R.layout.simple_spinner_item); spinner_vehicle_type.setAdapter(adapter_vehicle);
    【解决方案2】:

    也许这会对你有所帮助:

    Check out these link

    总结:

    row.xml 设置每一行的布局(在这种情况下:每行一个图像和一个文本):

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon"/>
    <TextView
    android:id="@+id/weekofday"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:background="@drawable/new_button"/>//background changing as per selector
    </LinearLayout>
    

    java代码示例

    public class AndroidCustomSpinner extends Activity {
    
    String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",
    "Wednesday", "Thursday", "Friday", "Saturday"};
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
      {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
    
       Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
         R.layout.row, R.id.weekofday, DayOfWeek);
       mySpinner.setAdapter(adapter);
      }
    }
    

    Addind Selector to TextView android:bacground="@drawable/new_button" can achieve your background on the basis of selected or focused res/drawable/new_button.xml

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

    【讨论】:

    • 正确理解自定义微调器可用@easyandroidtutorials.blogspot.in/2012/05/…
    • 我正在使用这样的 xml 文件,并为文本视图设置了背景,它仅显示所选项目的背景,而不是下拉列表中的项目。我什至想为下拉列表中的项目应用样式
    • @SwathiRao 添加选择器可以为您提供您所要求的。我已经编辑了代码,请通过它。
    • 我尝试了代码,这仅显示微调器中所选项目的背景。即使在列表中,我也想要项目的样式!假设我在列表中有汽车、公共汽车、火车等值,那么所有值都应该有一个 bg,而不仅仅是选定的项目。
    【解决方案3】:

    谢谢大家的帮助!!有效。不仅在 xml 中将其设置为背景,我还应该在 java 代码中将其设置为 setDropDownViewResource()。及其运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多