ListView 不显示带有 BaseAdapter 的 ImageButton

ListView doesn't show ImageButton with BaseAdapter

我想在右侧显示一个带有删除按钮的名称。我已经使用 BaseAdapter 创建了一个自定义适配器。 ListView 显示正确的名称但没有 ImageButton。希望ypu能帮到我。

这是我的代码:

每个 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_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/PlayerTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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>

我的播放器布局Activity:

<?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_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Baum.PlayerActivity">

    <EditText
        android:id="@+id/PayerEditText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="0dp"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        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>

我的Activity:

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

我想 android.R.layout.simple_list_item_1 期望布局文件中只有一个简单的文本视图。 (我也可能错了)。

仍在使用 Base Adapterandroid.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_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/PlayerTextView"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:textSize="20sp"
        android:textStyle="bold"/>

    <ImageView
        android:id="@+id/PlayerDeleteImageButton"
        android:src="@mipmap/ic_launcher_round"
        android:layout_width="30dp"
        android:layout_height="30dp"/>

</LinearLayout>

设置 PlayerAdpater

List<String> playerNameList = new ArrayList<>();
PlayerAdapter playerAdapter = new PlayerAdapter(this,playerNameList);
playerNameList.setAdapter(playerAdapter);

希望这会有所帮助。 快乐编码!