[英]How to add items dynamically to a custom adapter for ListView
I made adapter for ListView.我为 ListView 制作了适配器。 If I fill the elements array before creating the adapter and linking it to the listView, the elements are displayed.
如果我在创建适配器并将其链接到 listView 之前填充元素数组,则会显示元素。 But if I use the updateItems () method to add items when the button is clicked, nothing happens.
但是如果我在单击按钮时使用 updateItems() 方法添加项目,则什么也不会发生。 Code of adapter:
适配器代码:
public class ListAdapter extends ArrayAdapter<Lf> {
private LayoutInflater inflater;
private int layout;
private List<Lf> lfs;
public ListAdapter(Context context, int resource, List<Lf> lfs) {
super(context, resource, lfs);
this.lfs = lfs;
this.layout = resource;
this.inflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView==null){
convertView = inflater.inflate(this.layout, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) convertView.getTag();
}
Lf lf = lfs.get(position);
viewHolder.name.setText(lf.getLf());
viewHolder.freq.setText((int)lf.getFreq() + "");
return convertView;
}
public void updateItems(List<Lf> lfs) {
this.lfs.clear();
this.lfs.addAll(lfs);
this.notifyDataSetChanged();
}
private class ViewHolder {
final TextView name, freq;
ViewHolder(View view){
name = (TextView) view.findViewById(R.id.text_item_1);
freq = (TextView) view.findViewById(R.id.text_item_2);
}
}
}
Code of MainActivity: MainActivity 代码:
public class MainActivity extends AppCompatActivity {
EditText editText1;
ListView listView;
ListAdapter adapter;
List<Lf> elements = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.edit_text);
listView = (ListView) findViewById(R.id.fragment_list);
// Working... Elements print on screen
for(int i = 0; i < 10; i++) {
Lf temp = new Lf();
temp.setLf("mean");
temp.setFreq(100);
elements.add(temp);
}
adapter = new ListAdapter(this, R.layout.list_item, elements);
listView.setAdapter(adapter);
}
public void activity_button(View view) {
adapter.updateItems(elements);
}
}
If I click on the button, the existing items on the screen are cleared instead of a new one added.如果我单击该按钮,屏幕上的现有项目将被清除,而不是添加新项目。 But in debug I see that elements normally passed to ListAdapter.
但在调试中,我看到元素通常传递给 ListAdapter。
The problem is that your adapter's lfs
's field and your activity's elements
field both refer to the same List
instance.问题是您的适配器的
lfs
字段和您的活动的elements
字段都引用了同一个List
实例。 This happens because you pass elements
to the ListAdapter
constructor, and then simply assign this.lfs = lfs
.发生这种情况是因为您将
elements
传递给ListAdapter
构造函数,然后简单地分配this.lfs = lfs
。
So let's look at what happens when you pass elements
to updateItems()
...所以让我们看看当你将
elements
传递给updateItems()
时会发生什么......
public void updateItems(List<Lf> lfs) {
this.lfs.clear(); // this.lfs and the input lfs are the same list, so this clears both
this.lfs.addAll(lfs); // input lfs is now empty, so addAll() does nothing
this.notifyDataSetChanged();
}
Probably the best thing to do is to create a copy of the list in your adapter's constructor.最好的办法可能是在适配器的构造函数中创建列表的副本。
this.lfs = new ArrayList<>(lfs);
Now your adapter and activity will reference different lists, so this.lfs.clear()
won't accidentally clear out the very list you're passing to it.现在您的适配器和活动将引用不同的列表,因此
this.lfs.clear()
不会意外清除您传递给它的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.