【发布时间】: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_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>
我的 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_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>
我的活动:
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。但它也不起作用
标签: android android-studio android-layout android-listview android-adapter