Android Studio:在适配器中动态添加按钮
Android Studio: Dynamically add buttons in Adapter
我需要帮助为适配器内的 listView 项目创建动态按钮。
以下代码是我的适配器 getView 函数:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null) {
convertView = getLayoutInflater().inflate(R.layout.sell_animal_list_entry,null);
}
Animal animal = (Animal) getItem(position);
int count = 0;
int value = 0;
int age = 0;
int lifespan = 0;
int savePosition = 0;
String name = null;
lifespan = animal.getLifespan();
age = animal.getAge();
value = animal.getValue();
name = animal.getName();
savePosition = animal.getPosition();
LinearLayout buttonLayout = (LinearLayout) convertView.findViewById(R.id.buttonIncludeLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Button sellButton = new Button(SellAnimalActivity.this);
sellButton.setText(getResources().getString(R.string.sellAnimalButtonText));
count = buttonLayout.getChildCount();
if (count == 0) {
final int finalSavePosition = savePosition;
sellButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
displaySellWarning(finalSavePosition);
}
});
buttonLayout.addView(sellButton, lp);
}
return convertView;
}
我现在的问题是我无法在点击按钮时出售正确的动物。
我在列表中有 5 种不同的动物。前 3 只动物工作正常,
但是第 4 和第 5 只动物在出售第一只动物时按错了按钮,即使名称是正确循环的。
编辑:
进一步解释:我的 ListView 布局显示了动物的名称、它的寿命和年龄,我动态地向它添加了一个按钮。每只动物的名字、年龄和寿命都正确应用。
该按钮应该出售动物。它适用于列表中的动物 3。但是第 4 和第 5 只动物的按钮试图出售第 1 只动物。
当我 Log.d savePosition 我得到以下值:
0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,3,4
我觉得他只在第 5 个循环中读取第 4 个和第 5 个条目似乎很奇怪?
在那个问题上花了那么多小时,也许你能给我解决方案提示,为什么第 4 和第 5 个动物错了?
不知道为什么之前没有找到,搜索的时候有人和我有同样的问题:
related question
并且解决方案也发布在那里。
当 listView 的 layout:height 为 wrap_content 时,ListView 显然会感到困惑。必须是 fill_parent.
我需要帮助为适配器内的 listView 项目创建动态按钮。 以下代码是我的适配器 getView 函数:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null) {
convertView = getLayoutInflater().inflate(R.layout.sell_animal_list_entry,null);
}
Animal animal = (Animal) getItem(position);
int count = 0;
int value = 0;
int age = 0;
int lifespan = 0;
int savePosition = 0;
String name = null;
lifespan = animal.getLifespan();
age = animal.getAge();
value = animal.getValue();
name = animal.getName();
savePosition = animal.getPosition();
LinearLayout buttonLayout = (LinearLayout) convertView.findViewById(R.id.buttonIncludeLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Button sellButton = new Button(SellAnimalActivity.this);
sellButton.setText(getResources().getString(R.string.sellAnimalButtonText));
count = buttonLayout.getChildCount();
if (count == 0) {
final int finalSavePosition = savePosition;
sellButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
displaySellWarning(finalSavePosition);
}
});
buttonLayout.addView(sellButton, lp);
}
return convertView;
}
我现在的问题是我无法在点击按钮时出售正确的动物。
我在列表中有 5 种不同的动物。前 3 只动物工作正常,
但是第 4 和第 5 只动物在出售第一只动物时按错了按钮,即使名称是正确循环的。
编辑:
进一步解释:我的 ListView 布局显示了动物的名称、它的寿命和年龄,我动态地向它添加了一个按钮。每只动物的名字、年龄和寿命都正确应用。
该按钮应该出售动物。它适用于列表中的动物 3。但是第 4 和第 5 只动物的按钮试图出售第 1 只动物。
当我 Log.d savePosition 我得到以下值:
0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,3,4
我觉得他只在第 5 个循环中读取第 4 个和第 5 个条目似乎很奇怪?
在那个问题上花了那么多小时,也许你能给我解决方案提示,为什么第 4 和第 5 个动物错了?
不知道为什么之前没有找到,搜索的时候有人和我有同样的问题: related question
并且解决方案也发布在那里。 当 listView 的 layout:height 为 wrap_content 时,ListView 显然会感到困惑。必须是 fill_parent.