android定时更新listview,安卓里面如何实时刷新listview中item的局部地方?

实现的方式参见下面的示例:

public class ParticalRefreshActivity extends ListActivity {

String[] contents = new String[]{"aa","bb","cc","dd","ee","ff","gg","hh","ii","jj","kk","ll","mm","nn","oo","pp","qq","rr","ss","tt","uu","vv","ww","xx","yy","zz"};

ParticalRefreshAdapter mAdapter;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mAdapter = new ParticalRefreshAdapter(this,contents);

setListAdapter(mAdapter);

}

@Override

protected void onListItemClick(ListView l, View v, int position, long id) {

mAdapter.refreshFixPosition(position);

}

class ParticalRefreshAdapter extends BaseAdapter{

public String[] contents;

LayoutInflater mInflater;

public ParticalRefreshAdapter(Context context,String[] contents){

this.contents = contents;

mInflater = LayoutInflater.from(context);

}

@Override

public int getCount() {

return contents.length;

}

@Override

public Object getItem(int position) {

return null;

}

@Override

public long getItemId(int position) {

return 0;

}

//局部刷新的代码

public void refreshFixPosition(int position){

//刷新局部的item

ViewHolder holder = map.get(position);

String text = holder.textView.getText().toString().trim();

holder.textView.setText(text+"-");

//更改数据源,防止滑动时数据错误

contents[position] = text+"-";

}

public Map map = new HashMap<>();

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

if(convertView == null) {

convertView = mInflater.inflate(R.layout.item_partical_refresh, null);

holder = new ViewHolder();

holder.textView = (TextView) convertView.findViewById(R.id.tv_partical_refresh);

convertView.setTag(holder);

} else {

holder = (ViewHolder) convertView.getTag();

}

holder.textView.setText(contents[position]);

map.put(position, holder);

return convertView;

}

class ViewHolder{

TextView textView;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值