简体   繁体   English

如何将项目动态添加到 ListView 的自定义适配器

[英]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.

相关问题 如何将项目动态添加到列表视图 - How to add items to listview dynamically 使用Android应用程序的自定义适配器动态添加项目到列表视图 - Dynamically add items to list view using custom adapter for Android app 使用自定义适配器将项目动态添加到列表视图中的问题 - issue to add items dynamically into list view with custom adapter 如何在android中动态添加listview中的项目? - How to add the items in listview Dynamically in android? 单击列表项时如何在编辑列表视图项时使用自定义适配器? - How to use custom adapter while editing listview items when they are clicked? 如何通过自定义适配器将项目的ArrayList填充到ListView? - How to populate an ArrayList of items to ListView through a custom adapter? 在添加和编辑列表视图项时如何使用自定义适配器? - How to use custom adapter while adding and editing listview items? 使用自定义适配器添加2按钮ListView - Add 2 Button ListView with Custom Adapter 使用自定义自定义适配器将Item添加到自定义ListView - add Item to custom ListView with custom custom Adapter 在 Android 中,给定带有 TextView 和 RadioGroup 的项目的 ListView,如何动态添加项目? - In Android, given a ListView of items with TextView and RadioGroup, how to add items dynamically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM