【问题标题】:How to add a spinner against each item of list view如何针对列表视图的每个项目添加微调器
【发布时间】:2016-03-21 20:09:06
【问题描述】:

我有一个包含玩家列表的活动。该列表包含所有玩家的姓名。我想为每个名称提供一个微调器,其中包含“删除”、“评价为最佳”等选项。 我已经创建了列表,但无法添加带有特定选项集的微调器。

我的代码是。

public class AddPlayerActivity extends AppCompatActivity {

    @InjectView(R.id.players_listView)
    ListView playersListView;

    // Adapter for playersListView
    private ArrayAdapter<String> playersArrayAdapter;

    // Array of the players to be loaded in the list view
    String[] players = {"ABC", "XYZ", "MNO", "GHI"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_player);
        ButterKnife.inject(this);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        // Initializing array adapter
        playersArrayAdapter = new ArrayAdapter<>(this, R.layout.players_name, players);
        playersListView.setAdapter(playersArrayAdapter);


        playersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }
}

activity_add_player.xml 是-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_add_player">

        <ListView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:id="@+id/players_listView"
            android:choiceMode="singleChoice"/>
</LinearLayout>

【问题讨论】:

  • players_name.xml 中有 spinner 组件吗?而不是使用默认适配器编写 customAdapter 并根据需要使用它。
  • 在每一行看到这个链接stackoverflow.com/questions/15002821/…你可以添加sipner
  • 创建一个布局,它将在 LinearLayout 中包含 TextView 和 Spinner(方向:水平)。然后使用将扩展 BaseAdapter 的自定义适配器。现在在你的适配器中扩展这个布局。查看此示例以了解如何使用自定义适配器:androidadapternotifiydatasetchanged.blogspot.in
  • 使用列表视图的自定义适配器为每个列表项添加微调器对您有好处。

标签: android android-spinner


【解决方案1】:

当你点击列表视图打开对话框并将自定义布局附加到对话框而不是微调器。

例如:

在listview clicklistner中编写以下代码--

 final Dialog alertDialog = new Dialog(Add_Vendor.this);
                        alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                        alertDialog.setContentView(R.layout.layout_taglist);
                        alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

                        alertDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

                        alertDialog.show();

                        list = (ListView) alertDialog.findViewById(R.id.list_view);
                        Button btndone  = (Button) alertDialog.findViewById(R.id.button1);

                        CustomAdapterTag cadapter = new CustomAdapterTag(Add_Vendor.this,android.R.layout.simple_spinner_item,     

                                 tagslist);        

                        list.setAdapter(cadapter);

通过这种方式,您可以将 customAdaper 与“删除”、“评价为最佳”等选项一起使用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多