在按钮上单击使用自定义适配器添加自定义行到列表视图

时间:2018-02-01 17:08:59

标签: java android xml listview listviewitem

免责声明!代码工作正常,但效率不高。 问题是我必须在行添加到之前单击FAB TWICE 列表显示。我希望能够单击FAB一次,以便添加行 列表视图!

ListItems.java

 public class ListItems {
    int icon;
    String name;
    String detail;




    public ListItems(int icon,String name,String detail){
        super();
        this.icon = icon;
        this.name = name;
        this.detail = detail;

    }

    public String getTitle() {
        return name;
    }

    public String getDescription() {

        return detail;
    }

    public int getIcon() {
        return icon;
    }
}

............适配器........

ListItemHelper.java

  public class ListItemHelper extends ArrayAdapter<ListItems> {
    private final Context context;
    private final ArrayList<ListItems> itemsArrayList;
    public ListItemHelper(Context context, ArrayList<ListItems> itemsArrayList) {
        super(context, R.layout.list_item, itemsArrayList);
        this.context = context;
        this.itemsArrayList = itemsArrayList;

    }





       @Override

    public View getView(int position, View convertView, ViewGroup parent) {


        LayoutInflater inflater = (LayoutInflater) context

                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        View rowView = inflater.inflate(R.layout.list_item, parent, false);


        ImageView iconView = (ImageView) rowView.findViewById(R.id.iv_list_icon);

        TextView labelView = (TextView) rowView.findViewById(R.id.tv_list_name);

        TextView valueView = (TextView) rowView.findViewById(R.id.tv_detail_list);



        labelView.setText(itemsArrayList.get(position).getTitle());

        valueView.setText(itemsArrayList.get(position).getDescription());

        iconView.setImageResource(itemsArrayList.get(position).getIcon());


        return rowView;

    }

}

片段活动......

FragmentActivity.java

...

    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);



     //This is where the problem is ..I think!

        fabCall = (FloatingActionButton) view.findViewById(R.id.fab_menu_call);
        fabCall.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                list = (ListView) getActivity().findViewById(R.id.list);
                list.setAdapter(adapter);
                adapter = new ListItemHelper(getActivity(),generateData());

            }
        });
}



   // Or may the problem is here.. I don't know!

     private ArrayList<ListItems> generateData() {

            ArrayList<ListItems> items = new ArrayList<ListItems>();

            items.add(new ListItems(R.drawable.ic_shortcut_call,"Call","Shortcut added"));


        return items;
    }

...

3 个答案:

答案 0 :(得分:0)

您需要通知适配器您的列表已更改并且有新项目

添加:

adapter.notifyDataSetChanged();

之后

adapter = new ListItemHelper(getActivity(),generateData());

这将解决您的问题

点击两次后它为什么工作?

当您点击两次时它会起作用,因为您第一次将新项目添加到列表并初始化新适配器

在这一行 - &gt; adapter = new ListItemHelper(getActivity(),generateData());

第二次再次点击

您将新适配器设置为此处的列表 - &gt; list.setAdapter(adapter);

这会将新的内部化适配器与新项目连接到列表

您的代码应该如下所示来解决问题

fabCall.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                list = (ListView) getActivity().findViewById(R.id.list);
                adapter = new ListItemHelper(getActivity(),generateData());
                list.setAdapter(adapter);

                adapter.notifyDataSetChanged();

            }
        });

答案 1 :(得分:0)

尝试以下方法:

适配器:

public class ListItemHelper extends ArrayAdapter {
private Context context;
public ListItemHelper(Context context, ArrayList<ListItems> itemsArrayList) {
    super(context, R.layout.list_item, itemsArrayList);
    this.context = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.list_item, parent, false);
    ImageView iconView = (ImageView) rowView.findViewById(R.id.iv_list_icon);
    TextView labelView = (TextView) rowView.findViewById(R.id.tv_list_name);
    TextView valueView = (TextView) rowView.findViewById(R.id.tv_detail_list);

    labelView.setText(((ListItems)getItem(position)).getTitle());
    valueView.setText(((ListItems)getItem(position)).getDescription());
    iconView.setImageResource(((ListItems)getItem(position)).getIcon());
    return rowView;
}
}

的活动:

        @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        fabCall = (FloatingActionButton) view.findViewById(R.id.fab_menu_call);
        fabCall.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                adapter.add(generateData());
            }
        });
        list = view.findViewById(R.id.list);
        adapter = new ListItemHelper(getContext(), new ArrayList<ListItems>());
        list.setAdapter(adapter);
    }

    private ListItems generateData() {
        return new ListItems(R.drawable.ic_launcher_background,"Call","Shortcut added");
    }

答案 2 :(得分:0)

这里有一个问题,你正在做的一切都没问题,但点击工厂首先设置适配器调用,然后将新数据放入适配器尝试此代码

fabCall.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                list = (ListView) getActivity().findViewById(R.id.list);
               //first fill the adapter with updated list
                adapter = new ListItemHelper(getActivity(),generateData())
               // then set the adapter
                list.setAdapter(adapter);
                //notify change
                 adapter.notifyDataSetChanged();
                ;

            }
        });