使用setById时需要注意的地方

本文介绍在Android ListView中如何为Item内的控件设置唯一ID,确保可以通过ID准确找到这些控件。通过示例代码详细解释了为何需要对控件ID进行特殊设置,并保持其唯一性。

在使用listview的时候,通常要对某个item中的一个控件进行操作,那么我们就需要取得这个控件的引用

这个时候可以使用view.setId(int id);来设置一个id值,然后再通过view.getViewById(int id)精确得取得该控件

举个例子:

@Override
	public View getView(int position, View converview, ViewGroup viewgroup) {
		ViewHolder holder = new ViewHolder();
		if (converview == null) {
			converview = nodeGroupView.inflate(
					R.layout.company_regulation_group_item, null);

			holder.department = (TextView) converview
					.findViewById(R.id.company_regulation_group_department);
			holder.icon = (ImageView) converview
					.findViewById(R.id.company_regulation_group_develop);
			holder.downloadLL = (LinearLayout) converview
					.findViewById(R.id.company_regulation_download_ll);
			holder.download = (ImageView) converview
					.findViewById(R.id.company_regulation_download_img);
			holder.progressBar = (ProgressBar) converview
					.findViewById(R.id.company_regulation_download_progressBar);
			holder.fileSize = (TextView) converview
					.findViewById(R.id.company_regulation_filesize);
			converview.setTag(holder);
		} else {
			holder = (ViewHolder) converview.getTag();
		}
		holder.downloadLL.setId((position + 1) * 234);
		holder.download.setId((position + 1) * 123456);
		holder.progressBar.setId((position + 1) * 678);
		Node mNode = allShow.get(position);
		if (mNode != null) {
			// 叶节点则不显示为文件布局
			if (mNode.isLeaf()) {
				// TODO 显示下载布局,隐藏展开图标
				holder.icon.setVisibility(View.GONE);
				holder.downloadLL.setVisibility(View.VISIBLE);
				
				holder.fileSize.setVisibility(View.VISIBLE);
				CompanyRegulationVO item = (CompanyRegulationVO) allShow.get(position)
						.getItem();
				holder.fileSize.setText(DocumentsUtils.fileSize(item
						.getFileSize()));
				// TODO 判断 文件是否存在
				if (FileUtils.openFile(Constant.DOC_PATH + mNode.getTitle()) != null) {
					downloadState.put(mNode.getTitle(), 2);
				} else {
					if (!downloadState.containsKey(mNode.getTitle()))
						downloadState.put(mNode.getTitle(), 0);
				}
				switch (downloadState.get(mNode.getTitle())) {
				case 0:
					holder.downloadLL.setVisibility(View.VISIBLE);
					holder.download.setVisibility(View.VISIBLE);
					holder.progressBar.setVisibility(View.GONE);
					break;
				case 1:
					holder.downloadLL.setVisibility(View.VISIBLE);
					holder.download.setVisibility(View.GONE);
					holder.progressBar.setVisibility(View.VISIBLE);
					break;
				case 2:
					holder.downloadLL.setVisibility(View.GONE);

					break;

				default:
					break;
				}
			} else {
				// TODO 显示展开布局,隐藏下载布局
				holder.icon.setVisibility(View.VISIBLE);
				holder.downloadLL.setVisibility(View.GONE);
				holder.fileSize.setVisibility(View.GONE);
				if (mNode.isExplaned()) {
					holder.icon
							.setBackgroundResource(R.drawable.addrbook_btn_close);
				} else {
					holder.icon
							.setBackgroundResource(R.drawable.addrbook_btn_open);

				}
			}
			// TODO 判断 文件是否存在
			// if (FileUtils.openFile(Constant.DOC_PATH + mNode.getTitle()) !=
			// null) {
			// holder.downloadLL.setVisibility(View.GONE);
			// }

			// 显示文本
			holder.department.setText(mNode.getTitle());
			// 控制缩进
			converview.setPadding(72 * mNode.getLevel(), 3, 3, 3);
		}
		return converview;
	}
final LinearLayout download_ll = (LinearLayout) listView
						.findViewById((position + 1) * 234);
				download = (ImageView) listView
						.findViewById((position + 1) * 123456);
				progressBar = (ProgressBar) listView
						.findViewById((position + 1) * 678);


 

注意观察,这里面的id值是有严格的一个要求,就是必须保证唯一性,这个我们要为三个控件的引用设置id值

有一个问题是,为什么position要使用+1再乘于某个值,而不是直接用position

原因是

第一:如果直接用position,则起不到唯一性的作用,三个控件的id就会重复,所以让分别乘于一个特定的值,这个值尽可能差异较大

第二:为什么position要+1,因为如果不没有+1,那么当position=0的时候,三个控件的id依然会重复,因为计算出来的id全部是0

 

 


 

帮我看下这段代码做了什么/* * The MIT License * * Copyright 2020 Intuit Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.intuit.karate.core; import com.intuit.karate.FileUtils; import java.io.File; import org.w3c.dom.Element; /** * * @author pthomas3 */ public class HtmlSummaryReport extends HtmlReport { private final Element tbody; private final HtmlTagsReport tagsReport; private int passCount; private int failCount; public HtmlSummaryReport() { tagsReport = new HtmlTagsReport(); set("/html/head/title", "Karate Summary Report"); setById("nav-type", "Features"); contentContainer.appendChild(div("page-heading alert alert-primary", tagsLink())); Element table = node("table", "features-table table table-sm"); contentContainer.appendChild(table); Element thead = node("thead", null); table.appendChild(thead); Element tr = node("tr", null); thead.appendChild(tr); tr.appendChild(th("Feature", null)); tr.appendChild(th("Title", null)); tr.appendChild(th("Passed", "num")); tr.appendChild(th("Failed", "num")); tr.appendChild(th("Scenarios", "num")); tr.appendChild(th("Time (ms)", "num")); tbody = node("tbody", null); table.appendChild(tbody); } public void addFeatureResult(FeatureResult result) { String rowClass = result.isFailed() ? "failed-lt" : "passed-lt"; Element tr = node("tr", rowClass); tbody.appendChild(tr); String featureUri = result.getDisplayUri(); String featurePath = getHtmlFileName(result); Element tdFeature = node("td", null); tr.appendChild(tdFeature); Element featureLink = node("a", null); tdFeature.appendChild(featureLink); featureLink.setTextContent(featureUri); featureLink.setAttribute("href", featurePath); tr.appendChild(td(result.getFeature().getNameAndDescription(), null)); tr.appendChild(td(result.getPassedCount() + "", "num")); tr.appendChild(td(result.getFailedCount() + "", "num")); tr.appendChild(td(result.getScenarioCount() + "", "num")); String duration = formatter.format(result.getDurationMillis()); tr.appendChild(td(duration, "num")); if (result.isFailed()) { failCount++; Element featureNav = div("nav-item failed"); Element failedLink = node("a", null); featureNav.appendChild(failedLink); failedLink.setTextContent(result.getFeature().getNameForReport()); failedLink.setAttribute("href", featurePath); navContainer.appendChild(featureNav); } else { passCount++; } tagsReport.addFeatureResult(result); } public File save(String targetDir) { tagsReport.save(targetDir); setById("nav-pass", passCount + ""); setById("nav-fail", failCount + ""); File file = saveHtmlToFile(targetDir, "karate-summary.html"); System.out.println("\nHTML report: (paste into browser to view) | Karate version: " + FileUtils.getKarateVersion() + "\n" + file.toURI() + "\n===================================================================\n"); return file; } }
最新发布
12-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值