【问题标题】:Add a new Entry to ArrayAdapter向 ArrayAdapter 添加新条目
【发布时间】:2014-02-24 19:59:02
【问题描述】:

我在这里要做的是有一个按钮来向我的自定义 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);

}

 }

这是惰性适配器

公共类 GroupPortLazyAdapter 扩展 ArrayAdapter { 私有 int[] 颜色 = 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;
}

}

【问题讨论】:

  • 您用holder 填充视图的代码显然是错误的。 holder 变量应该在 getView() 内声明并初始化,即使是 convertView != null
  • 我已经编辑了我的代码,但仍然没有机会,

标签: android android-arrayadapter


【解决方案1】:

您是否尝试过使用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;
}

【讨论】:

  • 尝试使用我刚刚编辑到答案中的getView。
  • 非常感谢我的朋友,您所指出的都是正确的,但我刚刚发现错误是什么,我的 listView 嵌套在 scrollView 中,这导致了问题。谢谢你的回答..
  • 太棒了,很高兴你成功了。如果我的回答有帮助,请考虑accepting the answer
猜你喜欢
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 2015-06-01
  • 1970-01-01
  • 2020-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多