使用自定义适配器将数据添加到Listview运行时中

里兹·萨特瓦拉(Rjz Satvara)

在此处输入图片说明

问题:当我单击添加按钮时,所有数据均被清除,并且仅显示一项...。​​我想显示旧项目以及新添加的项目...

这是我的代码.....

模型类

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

主要活动

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

适配器类

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;
    }
}
普拉迪普·古普塔(Pradeep Gupta)

试试这个在适配器中添加方法:

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

然后在“点击”方法中执行此操作

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

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

                adapter.addItem(model);
            }
        });

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章