改变listview中某一个item的控件值,怎么对其单独刷新
1个回答
展开全部
package com.nsz.test;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.TextView;
import com.example.test.R;
import com.nsz.test.MyAdapter.CallBackItemListener;
public class ListViewTest extends Activity {
ListView listview;
ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
private MyAdapter myadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
listview = (ListView) findViewById(R.id.split_listview);
for (int i = 1; i <= 100; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("number", i + "");
listItem.add(map);
}
myadapter = new MyAdapter(this, listItem);
listview.setAdapter(myadapter);
myadapter.setmListener(new CallBackItemListener() {
@Override
public void callBackItem(int position, HashMap<String, Object> map) {
listItem.remove(position);
map.put("number", "测试下哦");
listItem.add(position, map);
myadapter.notifyDataSetChanged();
}
});
listview.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});
// /////////////////////////下面是我认为可以对listview中的第50item进行改变的代码:
// View localView = getViewByPosition(50, listview);
// TextView localtextviewNumber = (TextView) localView .findViewById(R.id.tv_split_number);
// Log.i("hahalog", "number_before=" + localtextviewNumber.getText());
// localtextviewNumber.setText("500");
// Log.i("hahalog", "number2_after=" + localtextviewNumber.getText());
// myadapter.notifyDataSetChanged();
// ////////////////////////////通过log信息看出,以上代码改变了其值,但没有更新在界面上,界面上的值还是50,而不是500!!!
}
public View getViewByPosition(int pos, ListView listView) {
int firstListItemPosition = listView.getFirstVisiblePosition();
int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询