Android ListView标题(Android ListView headers)
我有ListView有一些类型的事件。 事件按天排序,我希望每天都有标题和日期,然后在下面听事件。
这是我如何填充该列表:
ArrayList<TwoText> crs = new ArrayList<TwoText>(); crs.add(new TwoText("This will be header", event.getDate())); for (Event event : events) { crs.add(new TwoText(event.getStartString() + "-" + event.getEndString(), event.getSubject())); } arrayAdapter = new TwoTextArrayAdapter(this, R.layout.my_list_item, crs); lv1.setAdapter(arrayAdapter);
这就是我的Class TwoText的样子:
public class TwoText { public String classID; public String state; public TwoText(String classID, String state) { this.classID = classID; this.state = state; } }
这就是我的TwoTextArrayAdapter类的外观:
import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; public class TwoTextArrayAdapter extends ArrayAdapter<TwoText> { private ArrayList<TwoText> classes; private Activity con; TextView seperator; public TwoTextArrayAdapter(Activity context, int textViewResourceId, ArrayList<TwoText> classes) { super(context, textViewResourceId, classes); this.con = context; this.classes = classes; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.my_list_item, null); } TwoText user = classes.get(position); if (user != null) { TextView content1 = (TextView) v.findViewById(R.id.list_content1); TextView content2 = (TextView) v.findViewById(R.id.list_content2); if (content1 != null) { content1.setText(user.classID); } if(content2 != null) { content2.setText(user.state); } } return v; } }
这是my_list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView style="?android:attr/listSeparatorTextViewStyle" android:id="@+id/separator" android:text="Header" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#757678" android:textColor="#f5c227" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/list_content1" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_margin="5dip" android:clickable="false" android:gravity="center" android:longClickable="false" android:paddingBottom="1dip" android:paddingTop="1dip" android:text="sample" android:textColor="#ff7f1d" android:textSize="17dip" android:textStyle="bold" /> <TextView android:id="@+id/list_content2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_margin="5dip" android:clickable="false" android:gravity="center" android:linksClickable="false" android:longClickable="false" android:paddingBottom="1dip" android:paddingTop="1dip" android:text="sample" android:textColor="#6d6d6d" android:textSize="17dip" /> </LinearLayout> </LinearLayout>
我现在做的是我正在添加标题,就像普通的列表对象一样,但是我喜欢它作为标题,在我的情况下有一个日期。
我在我的xml中有这个代码标题:
<TextView style="?android:attr/listSeparatorTextViewStyle" android:id="@+id/separator" android:text="Header" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#757678" android:textColor="#f5c227" />
我尝试隐藏它,当它是不必要的,显示它是必要的,但我只是搞砸了我的代码的其余部分。 我尝试了更多的教程,但也有相同的效果。
有人可以指导我如何做这个简单的方法吗?
I have ListView that has some kind of events on it. Events are sorted by day, and I would like to have header with date on it for every day, and then events listen below.
Here is how I populate that list:
ArrayList<TwoText> crs = new ArrayList<TwoText>(); crs.add(new TwoText("This will be header", event.getDate())); for (Event event : events) { crs.add(new TwoText(event.getStartString() + "-" + event.getEndString(), event.getSubject())); } arrayAdapter = new TwoTextArrayAdapter(this, R.layout.my_list_item, crs); lv1.setAdapter(arrayAdapter);
and this is how my class TwoText looks:
public class TwoText { public String classID; public String state; public TwoText(String classID, String state) { this.classID = classID; this.state = state; } }
and this is how my TwoTextArrayAdapter class looks:
import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; public class TwoTextArrayAdapter extends ArrayAdapter<TwoText> { private ArrayList<TwoText> classes; private Activity con; TextView seperator; public TwoTextArrayAdapter(Activity context, int textViewResourceId, ArrayList<TwoText> classes) { super(context, textViewResourceId, classes); this.con = context; this.classes = classes; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.my_list_item, null); } TwoText user = classes.get(position); if (user != null) { TextView content1 = (TextView) v.findViewById(R.id.list_content1); TextView content2 = (TextView) v.findViewById(R.id.list_content2); if (content1 != null) { content1.setText(user.classID); } if(content2 != null) { content2.setText(user.state); } } return v; } }
and this is my_list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView style="?android:attr/listSeparatorTextViewStyle" android:id="@+id/separator" android:text="Header" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#757678" android:textColor="#f5c227" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/list_content1" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_margin="5dip" android:clickable="false" android:gravity="center" android:longClickable="false" android:paddingBottom="1dip" android:paddingTop="1dip" android:text="sample" android:textColor="#ff7f1d" android:textSize="17dip" android:textStyle="bold" /> <TextView android:id="@+id/list_content2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_margin="5dip" android:clickable="false" android:gravity="center" android:linksClickable="false" android:longClickable="false" android:paddingBottom="1dip" android:paddingTop="1dip" android:text="sample" android:textColor="#6d6d6d" android:textSize="17dip" /> </LinearLayout> </LinearLayout>
what I do at the moment is that I am adding header just as regular list object, but Id like it to be as header and in my case have a date on it.
I have this code in my xml for header:
<TextView style="?android:attr/listSeparatorTextViewStyle" android:id="@+id/separator" android:text="Header" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#757678" android:textColor="#f5c227" />
and I tried hiding it when it is unneccessary and showing it when neccessary but I just messed up rest of my code. I tried few more tutorials but they also had same effect.
Could anyone guide me on how to do that easy way?
原文:https://stackoverflow.com/questions/13590627
最满意答案
没有其他答案解决了问题的根本原因 。
问题是当Postgres引发异常时,它会在同一个连接上对未来的事务进行毒害。
该修复程序是回滚违规交易:
begin ActiveRecord...do something... rescue Exception => e puts "SQL error in #{ __method__ }" ActiveRecord::Base.connection.execute 'ROLLBACK' raise e end
参见参考 。
None of the other answers fix the root cause of the issue.
The problem is that when Postgres raises an exception, it poisons future transactions on the same connection.
The fix is to rollback the offending transaction:
begin ActiveRecord...do something... rescue Exception => e puts "SQL error in #{ __method__ }" ActiveRecord::Base.connection.execute 'ROLLBACK' raise e end
See reference.
相关问答
更多-
ActiveRecord :: StatementInvalid at / category / 5(ActiveRecord::StatementInvalid at /category/5)[2023-08-26]
这个: @recipes = @category.recipes 暗示你有 has_many :recipes 在您的Category模型中。 has_many将在recipes表中查找category_id列(即Recipe模型中的category_id属性)。 但是你没有recipes.category_id列,你有一个recipes.cat_id列: create_table "recipes", force: :cascade do |t| #... t.integer "cat_id ... -
没有其他答案解决了问题的根本原因 。 问题是当Postgres引发异常时,它会在同一个连接上对未来的事务进行毒害。 该修复程序是回滚违规交易: begin ActiveRecord...do something... rescue Exception => e puts "SQL error in #{ __method__ }" ActiveRecord::Base.connection.execute 'ROLLBACK' raise e end 参见参考 。 None of the ...
-
因此,看起来好像url_name存在于模式中,但另一个人删除了迁移文件。 为了解决我运行删除列迁移,然后添加列迁移,它解决了问题。 So it looks as though url_name was present in the schema but another person removed the migration file. To resolve I ran a remove column migration then did an add column migration and it res ...
-
出现此问题的原因是生产数据库上的公寓表缺少列床。 您需要在生产方面进行迁移。 heroku rake db:migrate 查看新列是否出现。 确保你也重启你的heroku。 heroku restart This problem occurs because the Apartment table on production database is missing the column Bed. You need to migrate on production side. heroku rake db: ...
-
您的迁移似乎存在问题。 在本地运行以查看它们是否正常运行: rake db:drop && rake db:create && rake db:migrate PS:我不告诉你运行rake db:reset因为它会加载模式而不是运行迁移。 Finally, I just had to destroy my Heroku app and recreate it. That solved this problem. I didn't have much data in my app. So, I could d ...
-
使用rails,关联has_one和belongs_to需要“trainers”表中“events”表的id,因此它可以在您的trainers表中定义您引用的事件,因此您需要将event_id列添加到trainers表中。 为此,您可以生成迁移 rails g migration add_event_id_to_trainer event_id:integer 然后运行 rake db:migrate With rails, the association has_one and belongs_to ...
-
我不确定“DB的组”是什么意思,但这似乎是一个简单的尝试插入一个对varchar(255)的列定义来说太大的值的情况。 如果您需要更多帮助,我认为您需要发布更多信息(例如您删除的内容以及您认为与此错误相关的原因)。 I'm not sure what you mean by "groups of the DB", but this seems like a straightforward case of trying to insert a value that is too large for the c ...
-
您不需要在heroku上再次运行这些命令。 只需将spree生成的文件添加到git即可。 另外请确保向git添加spree迁移,然后您可以将其部署到heroku上。 其次是 heroku run rake db:migrate You don't need to run these commands again on heroku. Just add the files generated by spree to git. Also make sure to add spree migrations to ...
-
问题是我没有任何迁移(只有schema.rb)。 如果需要,可以删除所有迁移,但如果需要,则需要运行rake db:setup以使用schema.rb来设置数据库(rake db:reset只会查找迁移)。 The issue was that I didn’t have any migrations (only schema.rb). You can remove all migrations if you want, but if you do, you need to run rake db:setu ...
-
我相信你使用的是错误的引号: SELECT COUNT(*) ....... (`question_categories`.`category_id` IN (87,1)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 使用" "代替`` 更新: 是的,我一直在您的测验模型中使用错误的引号: def build_parts category_ids = self.categories.map(&:id) question_ ...