ListView 不显示带有 BaseAdapter 的 ImageButton
Posted
技术标签:
【中文标题】ListView 不显示带有 BaseAdapter 的 ImageButton【英文标题】:ListView doesn't show ImageButton with BaseAdapter 【发布时间】:2020-11-19 00:40:47 【问题描述】:我想通过右侧的删除按钮显示数据库中的名称。我用 BaseAdapter 创建了一个自定义适配器。 ListView 显示正确的名称,但没有 ImageButton。希望你能帮助我。
这是我的代码:
每个 ListItem 的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_>
<TextView
android:id="@+id/PlayerTextView"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/PlayerDeleteImageButton"
android:layout_
android:layout_
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:background="@android:color/white"
android:padding="20dp"
app:srcCompat="@drawable/ic_baseline_delete_24"
android:tint="@color/colorPrimaryDark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的 PlayerActivity 的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".Baum.PlayerActivity">
<EditText
android:id="@+id/PayerEditText"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:hint="@string/AddPlayerHint"
app:layout_constraintEnd_toStartOf="@+id/PlayerFloatingActionButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/PlayerListView"
android:layout_
android:layout_
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/PayerEditText" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_
android:layout_
app:itemBackground="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/menu_navigation" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/PlayerFloatingActionButton"
android:layout_
android:layout_
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:clickable="true"
android:onClick="onClickFloatingActionButton"
android:tint="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_baseline_add_24"
tools:ignore="VectorDrawableCompat" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的活动:
public class PlayerActivity extends Activity
private ArrayList<String> players = new ArrayList<>();
DBHelper db = new DBHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
//Initalize custom Players
loadPlayersInArrayList();
// Show all Players in the ListView
ListView listView = (ListView) findViewById(R.id.PlayerListView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, players);
listView.setAdapter(arrayAdapter);
//All BottomNav stuff
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setSelectedItemId(R.id.player_menu);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener()
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
switch (item.getItemId())
case R.id.player_menu:
return true;
case R.id.home_menu:
startActivity(new Intent(getApplicationContext(), HomeActivity.class));
overridePendingTransition(0, 0);
return true;
case R.id.deck_menu:
startActivity(new Intent(getApplicationContext(), DecksActivity.class));
overridePendingTransition(0, 0);
return true;
return false;
);
public void onClickFloatingActionButton(View v)
EditText newPlayerName = findViewById(R.id.PayerEditText);
ArrayList<Player> dbPlayers = db.getAllPlayer();
boolean newPlayer = true;
for(Player p : dbPlayers)
if(newPlayerName.getText().toString().equals(p.getName()))
newPlayer = false;
new AlertDialog.Builder(this)
.setTitle("Information")
.setMessage("Dieser Name ist bereits vergeben")
.setPositiveButton("ok", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
)
.show();
break;
if (newPlayer)
db.addPlayer(newPlayerName.getText().toString());
loadPlayersInArrayList();
public void loadPlayersInArrayList()
if (players != null)
players.clear();
ArrayList<Player> dbPlayers = db.getAllPlayer();
for(Player p : dbPlayers)
players.add(p.getName());
这是我的适配器:
public class PlayerAdapter extends BaseAdapter
private Context context;
private ArrayList<String> items;
public PlayerAdapter(Context context, ArrayList<String> items)
this.context = context;
this.items = items;
@Override
public View getView(int position, View convertView, ViewGroup parent)
if (convertView == null)
convertView = LayoutInflater.from(context).inflate(R.layout.player_list_item, parent, false);
String currentItem = (String) getItem(position);
TextView deckName = (TextView) convertView.findViewById(R.id.PlayerTextView);
ImageButton playerDeleteButton = (ImageButton) convertView.findViewById(R.id.PlayerDeleteImageButton);
deckName.setText(currentItem);
return convertView;
public int getCount()
return items.size();
@Override
public Object getItem(int position)
return items.get(position);
@Override
public long getItemId(int position)
return position;
这是我在应用程序中的 ListView 的屏幕截图: ListView in the app
这里是每个项目的布局截图: Screenshot Layout
【问题讨论】:
你能分享一下你得到的名字列表的屏幕截图吗? @jerome,您能否将 app:srcCompat="@drawable/ic_baseline_delete_24" 替换为 android:src="@drawable/ic_baseline_delete_24" 并检查,也可以代替 ImageView playerDeleteButton,定义为 ImageButton playerDeleteButton. 我在我的任务末尾放了两张截图。 我已将其更改为:android:src="@drawable/ic_baseline_delete_24" 我将 ImageView 更改为 ImageButton。但它也不起作用 【参考方案1】:我猜android.R.layout.simple_list_item_1
只希望布局文件中有一个简单的文本视图。 (我也可能错了)。
仍在使用 Base Adapter 和 android.R.layout.simple_list_item_1
预计会出现在一个简单的微调器项目中。对于列表项的任何自定义布局,请使用ArrayAdapter<type>
。代码如下-
PlayerAdapter.java
public class PlayerAdapter extends ArrayAdapter<String>
List<String> playerNameList;
public PlayerAdapter(@NonNull Context context, @NonNull List<String> playerNameList)
//@param context, layout of list item, textview id of list item and List<String>
super(context, R.layout.player_list_item, R.id.PlayerTextView,playerNameList);
this.playerNameList = playerNameList;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
View view = super.getView(position, convertView, parent);
TextView playerTextView = view.findViewById(R.id.PlayerTextView);
ImageView playerDeleteImageButton = view.findViewById(R.id.PlayerDeleteImageButton);
playerTextView.setText(playerNameList.get(position));
playerDeleteImageButton.setOnClickListener(v ->
// apply on image click code here
);
return view;
我也将列表项修改为简单的 LinearLayout -
列表项布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="5dp"
android:padding="5dp"
android:layout_
android:layout_
android:orientation="horizontal">
<TextView
android:id="@+id/PlayerTextView"
android:layout_
android:layout_weight="1"
android:layout_
android:textSize="20sp"
android:textStyle="bold"/>
<ImageView
android:id="@+id/PlayerDeleteImageButton"
android:src="@mipmap/ic_launcher_round"
android:layout_
android:layout_/>
</LinearLayout>
设置 PlayerAdpater
List<String> playerNameList = new ArrayList<>();
PlayerAdapter playerAdapter = new PlayerAdapter(this,playerNameList);
playerNameList.setAdapter(playerAdapter);
希望这会有所帮助。 快乐编码!
【讨论】:
以上是关于ListView 不显示带有 BaseAdapter 的 ImageButton的主要内容,如果未能解决你的问题,请参考以下文章
android:ListView缓存机制及BaseAdapter的三重境界(逗比式,普通式,文艺式)
是否可以在 ListView 中显示带有文本 + 图标的行?
带有CustomAdapter的Listview项目上的上下文菜单android不显示
ListView 自己定义BaseAdapter实现单选打勾(无漏洞)