问题:当我点击添加按钮时,所有数据都清晰,只有一个项目显示....我想显示旧项目以及新添加的项目... < / p>
这是我的代码.....
模特课
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;
}
}
ADAPTER类
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;
}
}
答案 0 :(得分:1)
尝试在Adapter中添加方法:
public void addItem(GroupModel groupModel){
yourList.add(groupModel);
notifyDataSetChanged();
}
On Click方法执行此操作,
plus_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
model.addToGname("jignesh");
model.addToGmember("8");
adapter.addItem(model);
}
});
答案 1 :(得分:0)
您的ListView
总是有一个元素,因为每次点击add button
,您都会创建新的ArrayList
,然后在ArrayList
=&gt;中添加一个元素您的ArrayList
总是有一个项目=&gt;您的ListView
总是有一个项目
因此,只需将ArrayList<GroupModel> l = new ArrayList<GroupModel>();
移至plus_img.setOnClickListener(new View.OnClickListener()
...
ArrayList<GroupModel> l = new ArrayList<GroupModel>();
...
plus_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
...
}
});
答案 2 :(得分:0)
在您的活动中尝试以下代码。
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;
}
}
答案 3 :(得分:0)
adapter = new GroupAdapter(context,getListData()); lv.setAdapter(适配器);
如果你观察到这一行你正在设置getListData()返回的内容......所以声明一个新的全局列表,然后再执行以下操作
globalList.addAll(getListData());
adapter= new GroupAdapter(context,globalList);
现在您可以保留旧数据和新数据......您维护一个新列表,其中包含所有数据....并使用“globalList”进行操作
谢谢