向ArrayAdapter添加一个新条目

时间:2014-02-24 10:34:39

标签: android android-arrayadapter

我在这里要做的是设置一个按钮,为我的自定义listView添加一个新条目,比如添加到卡片概念。

然而,一切正常,只是当我点击按钮时只添加了一个条目,而当我第二次点击它时没有任何改变。

我的片段

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    rootView=inflater.inflate(R.layout.fragment_port_group, container,false);
    injectViews();
    mGroupPortAdapter=new GroupPortLazyAdapter(getActivity());
    addToList.setOnClickListener(new OnAddToListClickLinsten());
    return rootView;
 }


public class OnAddToListClickLinsten implements OnClickListener{

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    mGroupPortAdapter.add(new GroupPortInModel("23424",12321324,"0177889062"));
    portGroupListView.setAdapter(mGroupPortAdapter);

}

 }

这是懒惰的适配器

public class GroupPortLazyAdapter extends ArrayAdapter {     private int [] colors = new int [] {0x30bebebe,0x30FFFFFF};

public GroupPortLazyAdapter(Context context) {
    super(context, -1);
}

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
     ViewHolder holder;


    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(
                R.layout.port_group_custom_list_view, null);
        holder = new ViewHolder();
        holder.acountId = (TextView) convertView
                .findViewById(R.id.port_account_id);
        holder.serialNo = (TextView) convertView
                .findViewById(R.id.port_serial_no);
        holder.phoneNumber = (TextView) convertView
                .findViewById(R.id.port_phone_number);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.acountId.setText(getItem(position).getAcountId());
    holder.serialNo
            .setText(String.valueOf(getItem(position).getSerialNo()));
    holder.phoneNumber.setText(getItem(position).getPhone());
    int colorPos = position % colors.length;
    convertView.setBackgroundColor(colors[colorPos]);
    return convertView;
}

static class ViewHolder {
    TextView acountId;
    TextView serialNo;
    TextView phoneNumber;
    int position;
}

}

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用mGroupPortAdapter.notifyDatasetChanged()代替portGroupListView.setAdapter(mGroupPortAdapter)

另外,您尝试将此作为getView()

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
    GroupPortInModel item = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(
                R.layout.port_group_custom_list_view, null);
    }

    ((TextView) convertView.findViewById(R.id.port_account_id))
        .setText(item.getAcountId());
    ((TextView) convertView.findViewById(R.id.port_serial_no))
        .setText(item.getSerialNo());
    ((TextView) convertView.findViewById(R.id.port_phone_number))
        .setText(item.getPhone());

    int colorPos = position % colors.length;
    convertView.setBackgroundColor(colors[colorPos]);

    return convertView;
}