我有一个这样的数据库表:
ID
NAME
COUNTRY_NAME
我想要这样的列表:
+ Italy
- Potato
- Tomato
+ France
- Fromage
- Baguette
我写了一个 CursorAdapter,每次调用重新查询时,读取表中的所有项目并将其映射到一个对象中,该对象用于映射每个项目(实际项目或标题)的位置。
private static class PersonEntry {
boolean isHeader;
String countryName;
int realPos;
int id;
}
代码是这样的:
/* cursor generated by querying whole table */
public void readHeaders(Cursor cursor ) {
Log.d(this.getClass().getSimpleName(),"readHeaders init");
items = new ArrayList<PersonEntry >();
this.mCursor = cursor;
cursor.moveToFirst();
int i = 0;
String previousCountry = "";
String currentCountry;
while(cursor.isAfterLast() == false) {
int id = cursor.getInt(cursor.getColumnIndexOrThrow(Match.ROW_ID));
currentCountry = cursor.getString(cursor.getColumnIndexOrThrow(Person.ROW_COUNTRY_NAME));
if (!currentCountry.equals(previousCountry)) {
// ho incontrato una nuova nazione
// rispetto alla precedente, aggiungiamola
items.add(new PersonEntry(... define isHeader=true ..));
}
// stiamo comunque scorrendo gli elementi, aggiungiamo quello appena trovato
items.add(new PersonEntry( ... defile isHeader = false ....);
previousCountry = currentCountry;
i++;
cursor.moveToNext();
}
cursor.close();
Log.d(this.getClass().getSimpleName(),"readHeaders end");
}
所以我重写了 getView、bindView 和 newView 以膨胀正确的布局并绑定(bind)基于 realPos-position Cursor 的 View 。
该方法有效,但它真的很昂贵:它需要详细说明整个表,而且我有很多记录。 我正在寻找的是一种在滚动 ListView 时映射 realPosition -> fakePosition 的简单方法,但我认为这些方法太复杂了,我认为如果 getView 不是线性的(快速滚动?)它们会中断。
解决方案: 1) 按 COUNTRY_NAME 查询排序。向下滚动时 real_cursor_position =(请求的位置 - “国家变化#(?)”)。如果在 real_position 中翻译的请求位置出现在具有不同 country_name 的项目之后,则它是一个标题。我认为,除非有棘手的解决方案,否则在向下滚动拳头和向上滚动时它会破裂。 ... 仅此而已
还有其他解决方案吗?
编辑:另一个问题是,如果不扫描整个表,我无法预测 adapter.getCount() 返回的 View 数。
最佳答案
我已经编写了简单适配器的 getView 方法来向您展示如何构建一个 ListView,其中的 header 将具有相同国家/地区的项目分组。这将假定标题 View 在每一行的布局中显示/隐藏它作为当前行的要求。同样的事情可以通过使用 getItemViewType 方法来完成,并在绑定(bind)适配器的各个部分方面做更多的工作。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(
R.layout.rowlayout, parent, false);
}
// for simplicity, the header it's just a TextView
TextView header = (TextView) convertView
.findViewById(R.id.headerPart);
// also for simplicity, the row content it's just a TextView
TextView rowText = (TextView) convertView
.findViewById(R.id.normalPart);
// set the data for the row
mCursor.moveToPosition(position);
rowText.setText(mCursor.getString(mCursor.getColumnIndex("name")));
// this is not the first position
if (position - 1 >= 0) {
// if there is a previous position see if it has the same
// country(in which case you already had set the header)
String currentCountry = mCursor.getString(mCursor
.getColumnIndex("country"));
mCursor.moveToPosition(position - 1);
String previousCountry = mCursor.getString(mCursor
.getColumnIndex("country"));
if (currentCountry.equalsIgnoreCase(previousCountry)) {
// the countries are the same so abort everything as we
// already set the header on one of previous rows
header.setVisibility(View.GONE);
} else {
// this is the first occurrence of this country so show the
// header
header.setVisibility(View.VISIBLE);
header.setText(currentCountry);
}
} else {
// this is position 0 and we need a header here
header.setVisibility(View.VISIBLE);
header.setText(mCursor.getString(mCursor
.getColumnIndex("country")));
}
return convertView;
}
关于android - ListView/CursorAdapter 动态生成列表标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14784186/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A
是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论
我有一张背景图片,我想在其中添加一个文本框。我想弄清楚如何将标题放置在其顶部的正确位置。(我使用标题是因为我需要自动换行功能)。现在,我只能让文本显示在左上角,但我需要能够手动定位它的开始位置。require'RMagick'require'Pry'includeMagicktext="Loremipsumdolorsitamet"img=ImageList.new('template001.jpg')img 最佳答案 这是使用convert的ImageMagick命令行的答案。如果你想在Rmagick中使用这个方法,你必须自己移植
我是Rails的新手,所以请原谅简单的问题。我正在为一家公司创建一个网站。那家公司想在网站上展示它的客户。我想让客户自己管理这个。我正在为“客户”生成一个表格,我想要的三列是:公司名称、公司描述和Logo。对于名称,我使用的是name:string但不确定如何在脚本/生成脚手架终端命令中最好地创建描述列(因为我打算将其设置为文本区域)和图片。我怀疑描述(我想成为一个文本区域)应该仍然是描述:字符串,然后以实际形式进行调整。不确定如何处理图片字段。那么……说来话长:我在脚手架命令中输入什么来生成描述和图片列? 最佳答案 对于“文本”数
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些
有这些railscast。http://railscasts.com/episodes/218-making-generators-in-rails-3有了这个,你就会知道如何创建样式表和脚手架生成器。http://railscasts.com/episodes/216-generators-in-rails-3通过这个,您可以了解如何添加一些文件来修改脚手架View。我想把两者结合起来。我想创建一个生成器,它也可以创建脚手架View。有点像RyanBates漂亮的生成器或web_app_themegem(https://github.com/pilu/web-app-theme)。我
导读语言模型给我们的生产生活带来了极大便利,但同时不少人也利用他们从事作弊工作。如何规避这些难辨真伪的文字所产生的负面影响也成为一大难题。在3月9日智源Live第33期活动「DetectGPT:判断文本是否为机器生成的工具」中,主讲人Eric为我们讲解了DetectGPT工作背后的思路——一种基于概率曲率检测的用于检测模型生成文本的工具,它可以帮助我们更好地分辨文章的来源和可信度,对保护信息真实、防止欺诈等方面具有重要意义。本次报告主要围绕其功能,实现和效果等展开。(文末点击“阅读原文”,查看活动回放。)Ericmitchell斯坦福大学计算机系四年级博士生,由ChelseaFinn和Chri
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路