从上周开始,我试图在可扩展列表视图中实现多个倒计时器。我希望将时间减少到00:00:00,并根据我需要更新可扩展列表视图的父元素中的文本。
我的问题是:
1)滚动可扩展列表视图计时器重新启动
2)关于展开和折叠计时器重新启动
3)关于子按钮上的事件我需要在父视图上更新特定位置的文本
我正在尝试实现的另一件事是从子视图中单击一个按钮我需要在父视图上更新文本,但我被困在那里。
这是包含父级和子级的适配器。
public class Sent_ListAdapter extends BaseExpandableListAdapter {
private Context _context;
**private List<SentModel> _listDataHeader;**
// child data in format of header title, child title
**private HashMap<SentModel, List<SentModel>> _listDataChild;**
private TextView txt_summary;
private TextView datetime_element;
private LinearLayout ll_arrowtorecord;
**private String[] ti;**
**private CountDownTimer cdt_sent;**
**private HashMap<TextView, CountDownTimer> counters;**
**private TextView deadline;**
public Sent_ListAdapter(Context context, List<SentModel> listDataHeader, HashMap<SentModel, List<SentModel>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
ll_end = new FrameLayout[_listDataHeader.size()];
ll_closeaceess = new FrameLayout[_listDataHeader.size()];
ll_whoaccpted = new LinearLayout[_listDataHeader.size()];
**this.counters = new HashMap<TextView, CountDownTimer>();**
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final SentModel headerTitle = (SentModel) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.element_sentlist, null);
}
ll_arrowtorecord = (LinearLayout) convertView.findViewById(R.id.ll_arrowtorecord);
ll_arrowtorecord.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (headerTitle.getFile().trim().contains(".mp4")) {
Log.v("", TAG + "==video==" + headerTitle.getFile());
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse(headerTitle.getFile());
intent.setDataAndType(data, "video/mp4");
startActivity(intent);
} else if (headerTitle.getFile().trim().contains(".jpg")) {
Log.v("", TAG + "==image==" + headerTitle.getFile());
Intent gotorecoedactivity = new Intent(context, RecordedAsset.class);
gotorecoedactivity.putExtra("image", headerTitle.getFile());
startActivity(gotorecoedactivity);
overridePendingTransition(R.anim.right_slide_in, R.anim.left_side_out);
finish();
} else {
Toast.makeText(context, "No recorded asset is there.", Toast.LENGTH_SHORT).show();
}
}
});
txt_summary = (TextView) convertView.findViewById(R.id.txt_summary);
if (headerTitle.get_desc().equalsIgnoreCase("") || headerTitle.get_desc() == null) {
txt_summary.setText(" Description");
} else {
txt_summary.setText(headerTitle.get_desc());
}
datetime_element = (TextView) convertView.findViewById(R.id.datetime_element);
final TextView tv = datetime_element;
deadline = (TextView) convertView.findViewById(R.id.deadline);
**if (headerTitle.getRemaining_completion_time().equalsIgnoreCase("") || headerTitle.getRemaining_completion_time() == null) {
datetime_element.setText("");
}
else if (headerTitle.getRemaining_completion_time().equalsIgnoreCase("00:00:00"))
{
tv.setText("Completion time over");
deadline.setVisibility(View.GONE);
}
else
{
ti = headerTitle.getRemaining_completion_time().split(":");
int hrs = Integer.parseInt(ti[0]);
cdt_sent = counters.get(datetime_element);
int days = Integer.parseInt(headerTitle.getRemaining_days());
// int hours1 = datetime_plus(days, Integer.parseInt(ti[0]));
int min = Integer.parseInt(ti[1]);
int sec = Integer.parseInt(ti[2]);
long d1 = TimeUnit.DAYS.toMillis(days);
long m1 = TimeUnit.MINUTES.toMillis(min);
long h1 = TimeUnit.HOURS.toMillis(hrs);
long s1 = TimeUnit.SECONDS.toMillis(sec);
final long milliseco = d1 + m1 + h1 + s1;
if (cdt_sent != null) {
cdt_sent.cancel();
cdt_sent = null;
}
cdt_sent = new CountDownTimer(milliseco, 1000)
{
int days = 0;
int hours1 = 0;
private String sDate;
int min = 0;
int sec = 0;
@Override
public void onTick(long millisUntilFinished)
{
millisUntilFinished -= (days * DateUtils.DAY_IN_MILLIS);
if (millisUntilFinished > DateUtils.HOUR_IN_MILLIS)
{
hours1 = (int) (millisUntilFinished / DateUtils.HOUR_IN_MILLIS);
}
millisUntilFinished -= (hours1 * DateUtils.HOUR_IN_MILLIS);
if (millisUntilFinished > DateUtils.MINUTE_IN_MILLIS)
{
min = (int) (millisUntilFinished / DateUtils.MINUTE_IN_MILLIS);
}
millisUntilFinished -= (min * DateUtils.MINUTE_IN_MILLIS);
if (millisUntilFinished > DateUtils.SECOND_IN_MILLIS)
{
sec = (int) (millisUntilFinished / DateUtils.SECOND_IN_MILLIS);
}
sDate = " " + String.format("%02d", hours1) + "h " + String.format("%02d", min) + "m " + String.format("%02d", sec) + "s ";
_listDataHeader.get(groupPosition).setTime(sDate);
String hms = String.format("%02d,%02d:%02d:%02d", TimeUnit.MILLISECONDS.toDays(milliseco),TimeUnit.MILLISECONDS.toHours(milliseco),
TimeUnit.MILLISECONDS.toMinutes(milliseco) % TimeUnit.HOURS.toMinutes(1),
TimeUnit.MILLISECONDS.toSeconds(milliseco) % TimeUnit.MINUTES.toSeconds(1));
// Log.v("", "Bansi"+"==hms=="+hms);
tv.setText(_listDataHeader.get(groupPosition).getTime());
}
@Override
public void onFinish() {
tv.setText("Completion time over");
deadline.setVisibility(View.GONE);
}
};
counters.put(tv, cdt_sent);
cdt_sent.start();
}**
return convertView;
}
@SuppressLint("InflateParams")
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final SentModel childText = (SentModel) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.element_childsent, null);
}
ll_whoaccpted[groupPosition] = (LinearLayout) convertView.findViewById(R.id.ll_whoaccpted);
ll_end[groupPosition] = (FrameLayout) convertView.findViewById(R.id.ll_end);
ll_closeaceess[groupPosition] = (FrameLayout) convertView.findViewById(R.id.ll_closeaceess);
txt_whoaccepted = (TextView) convertView.findViewById(R.id.txt_whoaccepted);
img_block = (ImageView) convertView.findViewById(R.id.img_block);
img_end = (ImageView) convertView.findViewById(R.id.img_end);
txt_end = (TextView) convertView.findViewById(R.id.txt_end);
txt_closeacess = (TextView) convertView.findViewById(R.id.txt_closeacess);
ll_whoaccpted[groupPosition].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showWhoAcceptedpopup(childText.getB_id(), groupPosition + "");
if (Utils.detectInternetConnection(context)) {
new post_WhoAccepted().execute(childText.getB_id(), groupPosition + "");
} else {
progressDialog.showWarningDialog_Click(getString(R.string.no_internet), new OnClickListener() {
@Override
public void onClick(View v) {
progressDialog.dialogDismiss();
try {
Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
startActivity(callGPSSettingIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
});
if (childText.getIs_cancel().equalsIgnoreCase("0")) {
ll_end[groupPosition].setBackgroundColor(Color.parseColor("#CBCBCB"));
txt_end.setTextColor(Color.parseColor("#7E7E7E"));
txt_end.setText("You ended the .");
txt_end.setGravity(Gravity.CENTER);
img_end.setImageResource(R.drawable.cross_circleclick);
ll_end[groupPosition].setClickable(false);
} else {
ll_end[groupPosition].setBackgroundColor(Color.parseColor("#650030"));
txt_end.setTextColor(Color.parseColor("#FF0101"));
txt_end.setText("End ");
txt_end.setGravity(Gravity.RIGHT);
img_end.setImageResource(R.drawable.cross_circle);
ll_end[groupPosition].setClickable(true);
if (endornot.get(groupPosition) == 0) {
} else {
ll_end[groupPosition].setBackgroundColor(Color.parseColor("#CBCBCB"));
txt_end.setTextColor(Color.parseColor("#7E7E7E"));
txt_end.setText("You ended the .");
img_end.setImageResource(R.drawable.cross_circleclick);
txt_end.setGravity(Gravity.CENTER);
ll_end[groupPosition].setClickable(false);
}
ll_end[groupPosition].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.v("", TAG + "==childposition==" + groupPosition);
if (endornot.get(groupPosition) == 0) {
if (Utils.detectInternetConnection(context)) {
new post_EndorClose().execute(childText.getB_id(), "1", groupPosition + "");
} else {
progressDialog.showWarningDialog_Click(getString(R.string.no_internet), new OnClickListener() {
@Override
public void onClick(View v) {
progressDialog.dialogDismiss();
try {
Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
startActivity(callGPSSettingIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
} else {
}
}
});
}
if (childText.getIs_close().equalsIgnoreCase("0")) {
ll_closeaceess[groupPosition].setBackgroundColor(Color.parseColor("#E7E7E7"));
txt_closeacess.setTextColor(Color.parseColor("#7E7E7E"));
txt_closeacess.setText("Access to the closed");
txt_closeacess.setGravity(Gravity.CENTER);
ll_closeaceess[groupPosition].setClickable(false);
} else {
ll_closeaceess[groupPosition].setBackgroundColor(Color.parseColor("#006500"));
txt_closeacess.setTextColor(Color.parseColor("#043303"));
txt_closeacess.setText("Close access to ");
ll_closeaceess[groupPosition].setClickable(true);
txt_closeacess.setGravity(Gravity.RIGHT);
if (aceessdeniedornot.get(groupPosition) == 0) {
} else {
ll_closeaceess[groupPosition].setBackgroundColor(Color.parseColor("#E7E7E7"));
txt_closeacess.setTextColor(Color.parseColor("#7E7E7E"));
txt_closeacess.setText("Access to the closed");
txt_closeacess.setGravity(Gravity.CENTER);
ll_closeaceess[groupPosition].setClickable(false);
}
ll_closeaceess[groupPosition].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.v("", TAG + "==childposition==" + groupPosition);
if (aceessdeniedornot.get(groupPosition) == 0) {
if (Utils.detectInternetConnection(context)) {
new post_EndorClose().execute(childText.getB_id(), "2", groupPosition + "");
} else {
progressDialog.showWarningDialog_Click(getString(R.string.no_internet), new OnClickListener() {
@Override
public void onClick(View v) {
progressDialog.dialogDismiss();
try {
Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
startActivity(callGPSSettingIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
} else {
}
}
});
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
请帮助我,我被困在这里仍然在努力,但我有时间保持沉默。 谢谢!
答案 0 :(得分:0)
我首先要说的是,你的代码真的是很难来阅读。
getGroupView
和getChildView
上的繁忙码中的视图创建代码使用方法进行拆分。您也可以使用getChildView
上的ViewHolder模式来保存您的观点,而不是数组(ll_whoaccpted
等等)......) TextUtils.isEmpty()
可以帮助您更好地检查您的String是空的还是null
if (TextUtils.isEmpty(headerTitle.getRemaining_completion_time()))
//instead of
if (headerTitle.getRemaining_completion_time().equalsIgnoreCase("") || headerTitle.getRemaining_completion_time() == null)
每次Android获取列表视图时,让Sent_ListAdapter
实现OnClickListener而不是重新创建侦听器(新)。在view参数上添加条件以检查单击的视图类型。
在CountDownTimer上使用日期和SimpleDataFormat,手动字符串格式化只是非常糟糕且不可读
现在主要主题是:
滚动可扩展列表视图计时器重新启动
这是因为ListView重新创建未显示的视图/组(调用getView / getGroup方法)以重用其他数据的行。
如果你想避免这种情况,你应该在getView和getGroup之外创建和启动你的计数器(构造函数是个好地方)。
关于展开和折叠计时器重新启动
与第一个问题完全相同的事情。
关于子按钮事件的我需要更新特定位置的文本 在父视图上
我没有真正得到这个,所以我猜你想要在主要布局(包含listview的那个)上更改TextView的文本。那么你需要将该TextView的引用提供给Sent_ListAdapter
。
或者,不是将通用上下文传递给Sent_ListAdapter
,而是传递Activity / Fragment:
public Sent_ListAdapter(MyActivity activty, List<SentModel> listDataHeader, HashMap<SentModel, List<SentModel>> listChildData) {
this._myactivity = myactivity;
this._listDataHeader = listDataHeader;
...
}
// The listener that handles all the events of your Sent_ListAdapter
@Override
public void onClick(View v) {
if(v.getTag() == "theBoutonThatShouldResetTheCounter") { //or other condition
myactivite.myTextView.setText("------");
}
}
但是我如何在每一行的构造函数中获得位置?
您需要反思:listview使用您的数据源来显示行。因此,您需要遍历数据源上的元素(在您的情况下为listDataHeader)。类似的东西:
private List<CountDownTimer> counters;
public Sent_ListAdapter(Context context, List<SentModel> listDataHeader, HashMap<SentModel, List<SentModel>> listChildData) {
...
counters = new ArrayList()<>;
for(SentModel model : listDataHeader) {
long milliseco = model.getRemainingTimeInMs(); //TODO: create something like this or figure out how to get your couters starting times on ms
CountDownTimer cdt = new MyCountDownTimer(milliseco, 1000);
cdt.start(); // I don't know when you want to start your counters, if they start at the same time it could be here
}
}
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
...
datetime_element = (TextView) convertView.findViewById(R.id.datetime_element);
deadline = (TextView) convertView.findViewById(R.id.deadline);
...
CountDownTimer cdt = counters.get(groupPosition);
cdt.setTv1 = datetime_element;
cdt.setTv2 = deadline;
...
}
public class MyCountDownTimer extends CountDownTimer {
public TextView tv1; //TODO: make setters instead of public
public TextView tv2; //TODO: make setters instead of public
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
//Only use this if u have something to do each tick
SimpleDateFormat df = new SimpleDateFormat(" dd,hh:mm:ss");
Date timeRemaining = //TODO: figure out how you calculate your remaining time
if(tv1 != null) {
tv1.setText(df.format(timeRemaining));
}
}
@Override
public void onFinish() {
if(tv1 != null && tv2!= null) {
tv1.setText("Completion time over");
tv2.setVisibility(View.GONE);
}
}
}