android listview动态添加item,Android ListView动态刷新某项Item

这个博客介绍了如何创建一个名为TestListAdapter的自定义适配器,该适配器继承自BaseAdapter,并与List数据集关联。适配器实现了getView方法来填充每个列表项,并通过ViewHolder优化了性能。此外,博客还展示了如何在列表视图中局部更新特定位置的数据,避免了不必要的视图重绘。
摘要由CSDN通过智能技术生成

public classTestListAdapter extends BaseAdapter {privateContext mContext;private ListstrList;public TestListAdapter(Context context, Listlist) {

super();this.mContext =context;this.strList =list;

}

@Overridepublic intgetCount() {//TODO Auto-generated method stub

returnstrList.size();

}

@Overridepublic Object getItem(intposition) {//TODO Auto-generated method stub

returnposition;

}

@Overridepublic long getItemId(intposition) {//TODO Auto-generated method stub

returnposition;

}

@Overridepublic View getView(intposition, View convertView, ViewGroup parent) {//TODO Auto-generated method stub

ViewHolder holder = null;if (null ==convertView) {

convertView= LayoutInflater.from(mContext).inflate(R.layout.line, null);

holder= newViewHolder();

holder.iDText=(TextView) convertView.findViewById(R.id.textView_id);

holder.strText=(TextView) convertView.findViewById(R.id.textView_str);

convertView.setTag(holder);

}else{

holder=(ViewHolder) convertView.getTag();

}

holder.iDText.setText(position+ "");

String str= strList.get(position);

holder.strText.setText(str);returnconvertView;

}private static classViewHolder {privateTextView iDText;privateTextView strText;

}public void updataView(intposi, ListView listView) {int visibleFirstPosi =listView.getFirstVisiblePosition();int visibleLastPosi =listView.getLastVisiblePosition();if (posi >= visibleFirstPosi && posi <=visibleLastPosi) {

View view= listView.getChildAt(posi -visibleFirstPosi);

ViewHolder holder=(ViewHolder) view.getTag();

String txt=holder.strText.getText().toString();

txt= txt + "++;";

holder.strText.setText(txt);

strList.set(posi, txt);

}else{

String txt= strList.get(posi);

txt= txt + "++;";

strList.set(posi, txt);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值