使用 ArrayAdapter 从 LinearLayout 添加和删除项目
Add & delete item from LinearLayout using ArrayAdapter
我正在对选定的联系人进行气泡显示。每个telephone bubble是一个LinearLayout,里面包含ImageView和TextView。然后这些气泡显示在另一个 LinearLayout 中,它是 HorizontalScrollView 的子项。
它 child/parent 树看起来像这样:
- HorizontalScrollView
|- LinearLayout (id="@+id/telField")
|- LinearLayout (id="@+id/telBox") <- is programmatically added to parent
|- TextView (id="@+id/telNumber")
|- ImageView (id="@+id/delNumber")
在我的.java class中我调用这个方法来显示"telBox" LinearLayout in "telField" LinearLayout:
public void createAdapter() {
telList = new ArrayAdapter<>(this, R.layout.text_buble, R.id.telNumber, telNumList);
telField = (LinearLayout) findViewById(R.id.telField);
telField.removeAllViews();
final int adapterCount = telNumList.size();
for (ik = 0; ik < adapterCount; ik++) {
final View item = telList.getView(ik, null, null);
telField.addView(item);
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
telField.removeView(item);
telNumList.remove(ik-1);
telList.notifyDataSetChanged();
refresh();
}
});
}
}
方法refresh();
– 是自定义方法,有助于"reload" Activity:它获取应用程序值,刷新警告ImageViews 和cals createAdapter() 方法.
大按钮 "SELECT" 调用一个 Intent,该 Intent returns 从通讯录中选择 phone 号码。我调用此代码以使用 id "telField":
更新我的 LinearLayout
telNumList.add(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)));
createAdapter();
我面临的问题是:
After I click on LinearLayout with id "telNumber" it one by one deletes every bubble (no matter which I clicked) until it reaches first added bubble. It also crashes 50/50 when reaches first added element, I have not figured out a dependency. The error it returns is "out of bounds error", so I think it is connected with ik - 1
line.
我的问题是:如何更好地构建我的 ArrayAdapter?
在您的 for 循环中,编写以下代码行:
for (ik = 0; ik < adapterCount; ik++) {
final View item = telList.getView(ik, null, null);
item.setTag(ik);
telField.addView(item);
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int index = Integer.parseInt(v.getTag().toString());
telNumList.remove(index);
refresh();
}
});
}
希望对您有所帮助。
在您的代码中,您正尝试通过 ik
删除视图,该视图不断变化,因为您的编码正在删除最后一个视图,我已经修改了您的代码,如下所示
for (ik = 0; ik < adapterCount; ik++) {
final int position=ik;
final View item = telList.getView(ik, null, null);
telField.addView(item);
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
telField.removeView(item);
telNumList.remove(position);
telList.notifyDataSetChanged();
//refresh();
createAdapter();
}
});
}
此处position
将帮助您删除要删除的特定视图。我希望这就是你所要求的。
我正在对选定的联系人进行气泡显示。每个telephone bubble是一个LinearLayout,里面包含ImageView和TextView。然后这些气泡显示在另一个 LinearLayout 中,它是 HorizontalScrollView 的子项。
它 child/parent 树看起来像这样:
- HorizontalScrollView
|- LinearLayout (id="@+id/telField")
|- LinearLayout (id="@+id/telBox") <- is programmatically added to parent
|- TextView (id="@+id/telNumber")
|- ImageView (id="@+id/delNumber")
在我的.java class中我调用这个方法来显示"telBox" LinearLayout in "telField" LinearLayout:
public void createAdapter() {
telList = new ArrayAdapter<>(this, R.layout.text_buble, R.id.telNumber, telNumList);
telField = (LinearLayout) findViewById(R.id.telField);
telField.removeAllViews();
final int adapterCount = telNumList.size();
for (ik = 0; ik < adapterCount; ik++) {
final View item = telList.getView(ik, null, null);
telField.addView(item);
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
telField.removeView(item);
telNumList.remove(ik-1);
telList.notifyDataSetChanged();
refresh();
}
});
}
}
方法refresh();
– 是自定义方法,有助于"reload" Activity:它获取应用程序值,刷新警告ImageViews 和cals createAdapter() 方法.
大按钮 "SELECT" 调用一个 Intent,该 Intent returns 从通讯录中选择 phone 号码。我调用此代码以使用 id "telField":
更新我的 LinearLayouttelNumList.add(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)));
createAdapter();
我面临的问题是:
After I click on LinearLayout with id "telNumber" it one by one deletes every bubble (no matter which I clicked) until it reaches first added bubble. It also crashes 50/50 when reaches first added element, I have not figured out a dependency. The error it returns is "out of bounds error", so I think it is connected with
ik - 1
line.
我的问题是:如何更好地构建我的 ArrayAdapter?
在您的 for 循环中,编写以下代码行:
for (ik = 0; ik < adapterCount; ik++) {
final View item = telList.getView(ik, null, null);
item.setTag(ik);
telField.addView(item);
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int index = Integer.parseInt(v.getTag().toString());
telNumList.remove(index);
refresh();
}
});
}
希望对您有所帮助。
在您的代码中,您正尝试通过 ik
删除视图,该视图不断变化,因为您的编码正在删除最后一个视图,我已经修改了您的代码,如下所示
for (ik = 0; ik < adapterCount; ik++) {
final int position=ik;
final View item = telList.getView(ik, null, null);
telField.addView(item);
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
telField.removeView(item);
telNumList.remove(position);
telList.notifyDataSetChanged();
//refresh();
createAdapter();
}
});
}
此处position
将帮助您删除要删除的特定视图。我希望这就是你所要求的。