简体   繁体   中英

on button click add custom row to list view using custom adapter

Disclaimer! The code is working fine but it isn't efficient. The problem is that I have to click the FAB TWICE before the row is added to the list view. I want to be able to click the FAB just once so the row is added to the list view!

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;
    }
}

............Adapter........

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;

    }

}

And the fragment activity...

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;
    }

...

you need to notify the adapter that your list is changed and there is a new item

add this :

adapter.notifyDataSetChanged();

after

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

and this will fix your problem


why it was working after clicking twice ?

it was worked when you click twice because at the first time you will add new item to the list and initialize a new adapter

at this line -> adapter = new ListItemHelper(getActivity(),generateData());

and the second time when you click again

you set the new adapter to the list here -> list.setAdapter(adapter);

and this will connect the new internalized adapter with the new items to the list


you code should look like this to fix the problem

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();

            }
        });

Try the following:

Adapter:

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;
}
}

Activity:

        @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");
    }

here is the problem you are doing everything thing ok the but click in fab is first setting the adapter call and then puts the new data into an adapter try this code

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();
                ;

            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM