-1

enter image description here

PROBLEM : when i am click on add button then all data are clear and only one item is shows.... i want to Show Old items as well as new added item...

this is my code.....

MODEL class

public class GroupModel {
    ArrayList<String> G_member=new ArrayList<String>();
    ArrayList<String> G_name = new ArrayList<String>();


    public ArrayList<String> getG_member() {
        return G_member;
    }

    public void setG_member(ArrayList<String> g_member) {
        G_member = g_member;
    }
    public void addToGmember(String gname){
        this.G_member.add(gname);
    }

    public ArrayList<String> getG_name() {
        return G_name;
    }

    public void setG_name(ArrayList<String> g_name) {
        G_name = g_name;
    }
    public void addToGname(String name){
        this.G_name.add(name);
    }
}

MAIN ACTIVITY

public class MainActivity extends ActionBarActivity {

    Context context=this;
    private ListView lv;
    ImageView plus_img;// plus button to add GROUP Runtime.
    GroupModel model = new GroupModel();
    GroupAdapter adapter;
    ListView myListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.group__listview);
        myListView = lv;
        //plus image
        plus_img = (ImageView) findViewById(R.id.add_img);
        plus_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                model.addToGname("jignesh");
                model.addToGmember("8");
                ArrayList<GroupModel> l = new ArrayList<GroupModel>();
                l.add(model);
           //     adapter.setListData(getListData());
                myListView.setAdapter(new GroupAdapter(context, l));
            //    adapter.notifyDataSetChanged();
            }
        });


        adapter= new GroupAdapter(context,getListData());
        lv.setAdapter(adapter);

}

    private ArrayList getListData() {
        ArrayList list = new ArrayList();
        int untilloop;

        if(model.getG_member().size()>10)
        {
            untilloop = model.getG_member().size();
            for (int i=0;i<untilloop;i++)
            {
                model.getG_member();
                model.getG_name();
                list.add(model);
            }
        }
        else{
            untilloop=10;
            for(int i = 0; i <untilloop; i++){
                model.getG_name().add("raj "+i );
                model.getG_member().add(""+i);
                list.add(model);
            }
        }

        return list;
    }
}

ADAPTER class

public class GroupAdapter extends BaseAdapter {
    ArrayList<GroupModel> glist;
    Context context;
    private LayoutInflater layoutInflater;

    public GroupAdapter(Context mainActivity, ArrayList<GroupModel> glist) {
        context = mainActivity;
        this.glist=glist;
        layoutInflater = LayoutInflater.from(context);
    }
    public void setListData(ArrayList<GroupModel> data){
        glist = data;
    }

    @Override
    public int getCount() {
        return glist.size();
    }

    @Override
    public Object getItem(int position) {
        return glist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
         //  LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            convertView = layoutInflater.inflate(R.layout.row_list_data,null);
            holder= new ViewHolder();
            holder.g_number=(TextView)convertView.findViewById(R.id.txt_g_number);
            holder.g_name=(TextView)convertView.findViewById(R.id.txt_g_name);
            holder.nxt_aerrow=(ImageView)convertView.findViewById(R.id.img_nxt);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.g_number.setText(glist.get(position).getG_member().get(position).toString());
        holder.g_name.setText(glist.get(position).getG_name().get(position).toString());

        holder.nxt_aerrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"image click",Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;
    }
    static class ViewHolder {
        TextView g_number,g_name;
        ImageView nxt_aerrow;
    }
}
Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50
  • Possible duplicate of [ListView with a custom adapter, adding elements one by one](http://stackoverflow.com/questions/9416215/listview-with-a-custom-adapter-adding-elements-one-by-one) – Mike M. Apr 21 '16 at 07:01
  • In OnClick you are creating array object and passing it to your adapter will create only row every time. Create that array list at global level and add your Group member in it. will hopefully solve your problem – Drup Desai Apr 21 '16 at 07:05
  • thank you for reply,,,,I want to add one row when OnClick ... – Rajesh Satvara Apr 21 '16 at 07:07
  • i am not getting you @Drup Desai.. – Rajesh Satvara Apr 21 '16 at 07:09
  • Okay then declare you Arraylist as global variable and add your group member in it. – Drup Desai Apr 21 '16 at 07:09

4 Answers4

1

try this add the method in Adapter:

public void addItem(GroupModel groupModel){
   yourList.add(groupModel);
  notifyDataSetChanged();
}

And On Click method do this,

plus_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                model.addToGname("jignesh");
                model.addToGmember("8");

                adapter.addItem(model);
            }
        });
Pradeep Gupta
  • 1,770
  • 1
  • 9
  • 23
  • yup.. its working...now i want to delete any particular item if long pressed... so how can i achieve ? – Rajesh Satvara Apr 21 '16 at 07:34
  • @RjzSatvara One question per post, please. Your posted question has been answered. If you have another, post a new question. Better yet, search first. That question has definitely been answered here before. – Mike M. Apr 21 '16 at 07:55
0

You always got one element in your ListView because every time you click on add button, you create new ArrayList then you add one element to your ArrayList => your ArrayList always have one item => your ListView always have one item

So Simply move ArrayList<GroupModel> l = new ArrayList<GroupModel>(); to outside of plus_img.setOnClickListener(new View.OnClickListener()

...
ArrayList<GroupModel> l = new ArrayList<GroupModel>();
...
plus_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               ...
            }
});
Linh
  • 57,942
  • 23
  • 262
  • 279
  • inside `setOnClickListener`, can you write a logcat for print the `ArrayList size`. and then tell me the array size increase correctly when you press on add button or not – Linh Apr 21 '16 at 07:10
0

Try below code in you activity.

public class MainActivity extends ActionBarActivity {

Context context=this;
private ListView lv;
ImageView plus_img;// plus button to add GROUP Runtime.
GroupModel model = new GroupModel();
GroupAdapter adapter;
ListView myListView;
ArrayList<GroupModel> l = new ArrayList<GroupModel>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.group__listview);
    myListView = lv;
    //plus image
    plus_img = (ImageView) findViewById(R.id.add_img);
    plus_img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            model.addToGname("jignesh");
            model.addToGmember("8");
            l.add(model);
       //     adapter.setListData(getListData());
            myListView.setAdapter(new GroupAdapter(context, l));
        //    adapter.notifyDataSetChanged();
        }
    });


    adapter= new GroupAdapter(context,getListData());
    lv.setAdapter(adapter);

}

private ArrayList getListData() {
    ArrayList list = new ArrayList();
    int untilloop;

    if(model.getG_member().size()>10)
    {
        untilloop = model.getG_member().size();
        for (int i=0;i<untilloop;i++)
        {
            model.getG_member();
            model.getG_name();
            list.add(model);
        }
    }
    else{
        untilloop=10;
        for(int i = 0; i <untilloop; i++){
            model.getG_name().add("raj "+i );
            model.getG_member().add(""+i);
            list.add(model);
        }
    }

    return list;
}
}
Drup Desai
  • 912
  • 6
  • 18
0

adapter= new GroupAdapter(context,getListData()); lv.setAdapter(adapter);

if u observe this line you are setting whatever getListData() is returning...so declare a new Global List and later do the following

globalList.addAll(getListData());
adapter= new GroupAdapter(context,globalList);

Now you can preserve both old as well as new data...you maintain a new list where in you have all the data....and manipulate using that "globalList"

Thank you

Lokanath
  • 662
  • 5
  • 18