問題:當我點擊添加按鈕,然後將所有的數據都清楚,只有一個產品展示....我想告訴老項目,以及新添加的項目...
這是我的代碼.....
模型類
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;
}
}
可能的複製,添加元素逐個](http://stackoverflow.com/questions/9416215/listview-with-a-custom-adapter-addin g-elements-by-one) –
在OnClick中,創建數組對象並將其傳遞給適配器將每次只創建一行。在全局級別創建數組列表並添加您的組成員。將有望解決您的問題 –
謝謝你的回覆,,,,我想添加一行時,OnClick ... –