我有一个活动,其中有一个玩家列表。该列表包含所有玩家的名字。我想为每个名字提供一个微调器,其中包括"删除","评价为最佳"等等 我创建了列表,但无法添加带有一组选项的微调器。
我的代码是。
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>
答案 0 :(得分:1)
当您单击listview时,打开Dialog并将自定义布局附加到Dialog而不是微调器。
例如:
在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与“删除”,“评价最佳”等选项