我遇到了这个问题,我有一个expandablelistview和一个倒数计时器。使用倒计时器我想扩展一个组并在剩余时间内更新textview。我没有在网上找到任何解决方案,这就是我在这里写作的原因。
如何从定时器访问View?
这是我的可扩展列表视图代码:
public class ExerciseListAdapter extends BaseExpandableListAdapter {
private View childview, rowview;
private List<List<Bitmap>> vajeImage;
private ArrayList<ExerciseItem> headers;
public ExerciseListAdapter(View ChView, View RView, List<List<Bitmap>> vaje, ArrayList<ExerciseItem> header) {
this.childview = ChView;
this.rowview = RView;
this.vajeImage = vaje;
this.headers = header;
}
@Override
public int getGroupCount() {
return headers.size();
}
@Override
public int getChildrenCount(int i) {
return vajeImage.get(i).size();
}
@Override
public Object getGroup(int i) {
return headers.get(i);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return vajeImage.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.exercise_elv_row, viewGroup, false);
TextView txHead = (TextView) view.findViewById(R.id.textViewExeHead);
txHead.setText(headers.get(i).getIme());
TextView txSub = (TextView) view.findViewById(R.id.textViewExeSub);
txSub.setText(headers.get(i).getOpis());
return view;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.exercise_elv_child_item, viewGroup, false);
ImageView imview = (ImageView) view.findViewById(R.id.imageviewChild);
imview.setImageBitmap(vajeImage.get(i).get(i1));
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
}
这是我的计时器代码
public class MyCountDownTimer extends CountDownTimer {
int current=0;
ExpandableListView EL;
TextView timeText;
public MyCountDownTimer(long startTime, long interval, ExpandableListView list) {
super(startTime, interval);
EL=list;
list.expandGroup(current);
//this returns null pointer exception on TextView tx
list.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
if(EL.isGroupExpanded(groupPosition)) {
TextView tx = (TextView) EL.getChildAt(groupPosition).findViewById(R.id.textViewTime);
tx.setText("2");
}
}
});
}
@Override
public void onFinish() {
//timeText.setText("Time's up!");
}
@Override
public void onTick(long millisUntilFinished) {
//timeText.setText("" + millisUntilFinished / 1000);
Log.v("test","" + millisUntilFinished / 1000);
}
}
修改
这里我尝试访问我的适配器的公共方法,适配器类是公共的,方法是公共的,但没有找到
if(EL.isGroupExpanded(groupPosition)) {
//epl.setChildViewData(groupPosition, 0, "2");
epl=(ExpandableListAdapter)EL.getExpandableListAdapter();
//cannot resolve method "notifyDataSetChanged
epl.notifyDataSetChanged();
答案 0 :(得分:2)
拨打电话时未创建您尝试访问的childview
。因此,在您的setOnGroupExpandListener()
中,尚未创建子视图,因此您获得该异常,因此您必须等到创建所有视图才能进行修改
我建议你将值传递给适配器,以便进行修改。像这样的东西
在ExerciseListAdapter
文件中添加此HashMap
private HashMap<String, String> mMapStuff =new HashMap<String, String>();
还在适配器中添加此方法
public void setChildViewData(int groupPosition, int childPosition, String value){
mMapStuff.put(groupPosition+""+childPosition, value);
}
现在使用getChildView()
方法
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Some Other Code
.....
//Note this should be your last line before the return statement
String msg = mMapStuff.get(groupPosition+""+childPosition);
if(msg != null)
txtListChild.setText(msg);
return convertView;
}
现在在setOnGroupExpandListener()
添加此行
listAdapter.setChildViewData(groupPosition, 0, "2");
像这样
//this returns null pointer exception on TextView tx
list.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
listAdapter.setChildViewData(groupPosition, 0, "2");
}
});
让我知道这是否有效