【发布时间】:2014-05-21 15:08:43
【问题描述】:
我遇到了这个问题,我有一个可扩展列表视图和一个倒数计时器。使用倒数计时器,我想扩展一个组并用剩余时间更新 textview。我没有在网上找到任何解决方案,这就是我在这里写的原因。
如何从计时器访问视图?
这是我的可扩展列表视图代码:
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();
【问题讨论】:
-
你到底想要什么?在计时器结束时,所有孩子都应该展开??还是什么时候?
-
当我创建新计时器时,选定的组应该扩展,它会这样做。我想更新该扩展组的子视图,但我无法访问它。
-
你做了什么来访问它?你遇到了什么异常?
-
TextView tx 在我尝试设置字符串“2”时返回空异常。我什至不确定我应该如何访问扩展组子,这就是为什么我首先扩展选定的组,然后我尝试通过 ongroupexpand 监听器访问它,但它不起作用。
-
您是否尝试在适配器内创建类?作为班上的班级。这样,从适配器访问变量会容易得多。
标签: android android-listview android-view countdowntimer