我有一个 DialogFragment,在它的布局中我有一个 EditText 和一个 ListView。 ListView 主要显示联系人列表(最初此列表有 0 个项目)。当 edittext 的值更新时,我用在 EditText 中输入了 text 的联系人填充列表。
在 EditText 上,我使用 addTextChangedListener 在用户键入联系人姓名或电子邮件地址时使用所需联系人更新列表。
我面临的奇怪问题是列表(或者可能是布局)只有在我按下后退按钮以在键入后隐藏键盘时才会更新。只要软键盘显示列表就不会更新(除了第一次将项目添加到空列表时)。
为了更好的理解,下面是一些代码。
(在 onCreateView 中):
// Set list adapter for contacts list
contactsList = (ListView) shareView.findViewById(R.id.contactList);
emailContactAdapter = new EmailContactAdapter(getActivity(), emailContacts, shareFragment);
contactsList.setAdapter(emailContactAdapter);
// Implement Phone-book contact share
sendToInput = (EditText) shareView.findViewById(R.id.contact_name);
sendToInput.addTextChangedListener(onContactNameSearch);
在 onContactNameSearch (TextWatcher) 中:
public TextWatcher onContactNameSearch = new TextWatcher() {
private generics commonMethods = new generics();
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
emailContacts.clear();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
Log.d("DEBUG::REACH", "After Text Changed called");
String textValue = s.toString();
// Show or hide the share apps list based on user input
// and whether or not the list is already showing or not
if (textValue.equals("")) {
Log.d("DEBUG::REACH", "TEXT value is empty");
showAppList();
emailContacts.clear();
} else {
Log.d("DEBUG::REACH", "TEXT has value");
// Hide app list if visible
if (isAppListShowing()) hideAppList();
// Get the email contacts list based on the user query
emailContacts.addAll(commonMethods.getEmailContacts(appContext, textValue));
}
adapter.notifyDataSetChanged();
}
我的假设是列表适配器的列表已正确更新,但由于某种原因,直到隐藏软键盘布局才反射(reflect)新的更改。
问题:
PS:afterTextChanged 方法中的代码以前在onTextChanged 方法中,我遇到了同样的问题。
更新(添加屏幕截图以便更好地理解)
以下是显示对话框 fragment 且未在编辑文本中键入任何文本的情况。
现在,当我输入“A”时,列表就会出现。
我又添加了几个字母,但列表没有更新。我添加了字母“mit”,所以现在查询变为“Amit”,但列表中没有变化。
现在,当我按下设备上的硬件后退按钮以隐藏键盘时。键盘被隐藏,列表被更新。
(请不要介意重叠的联系人姓名和电子邮件,仍然需要修复布局 :P)
UPDATE2(添加 EmailContactAdapter 代码)
下面是EmailContactAdapter类
public class EmailContactAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<EmailContact> contacts;
private ProductShareFragment fragment;
private LayoutInflater inflater;
/**
* Constructor
*/
public EmailContactAdapter(Activity activity, ArrayList<EmailContact> contacts, ProductShareFragment fragment) {
this.activity = activity;
this.contacts = contacts;
this.fragment = fragment;
}
@Override
public int getCount() {
return contacts.size();
}
@Override
public Object getItem(int position) {
return contacts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null) {
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null) {
convertView = inflater.inflate(R.layout.email_contact_list_row, null);
}
EmailContact contact = contacts.get(position);
ImageView contactImage = (ImageView) convertView.findViewById(R.id.email_contact_image);
TextView contactName = (TextView) convertView.findViewById(R.id.email_contact_name);
TextView contactEmail = (TextView) convertView.findViewById(R.id.email_contact_email);
// contactImage.setImageBitmap(contact.getImage());
contactName.setText(contact.getName());
contactEmail.setText(contact.getEmail());
return convertView;
}
}
最佳答案
您正在尝试通过更改 emailContacts 来更改可见列表,但适配器仍包含旧列表数据。
解决方案:在 EditText 中的每个新文本之后创建新的适配器(这是一个非常糟糕的方法),或者在 EmailContactAdapter 中创建方法来替换项目 - 在你的情况下 联系人字段。
关于android - ListView 在隐藏键盘之前不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30217317/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
我正在尝试在Ruby中制作一个cli应用程序,它接受一个给定的数组,然后将其显示为一个列表,我可以使用箭头键浏览它。我觉得我已经在Ruby中看到一个库已经这样做了,但我记不起它的名字了。我正在尝试对soundcloud2000中的代码进行逆向工程做类似的事情,但他的代码与SoundcloudAPI的使用紧密耦合。我知道cursesgem,我正在考虑更抽象的东西。广告有没有人见过可以做到这一点的库或一些概念证明的Ruby代码可以做到这一点? 最佳答案 我不知道这是否是您正在寻找的,但也许您可以使用我的想法。由于我没有关于您要完成的工作
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
我正在尝试为我的iOS应用程序设置cocoapods但是当我执行命令时:sudogemupdate--system我收到错误消息:当前已安装最新版本。中止。当我进入cocoapods的下一步时:sudogeminstallcocoapods我在MacOS10.8.5上遇到错误:ERROR:Errorinstallingcocoapods:cocoapods-trunkrequiresRubyversion>=2.0.0.我在MacOS10.9.4上尝试了同样的操作,但出现错误:ERROR:Couldnotfindavalidgem'cocoapods'(>=0),hereiswhy:U
我正在我的Rails项目中安装Grape以构建RESTfulAPI。现在一些端点的操作需要身份验证,而另一些则不需要身份验证。例如,我有users端点,看起来像这样:moduleBackendmoduleV1classUsers现在如您所见,除了password/forget之外的所有操作都需要用户登录/验证。创建一个新的端点也没有意义,比如passwords并且只是删除password/forget从逻辑上讲,这个端点应该与用户资源。问题是Grapebefore过滤器没有像except,only这样的选项,我可以在其中说对某些操作应用过滤器。您通常如何干净利落地处理这种情况?
在我做的一些网络开发中,我有多个操作开始,比如对外部API的GET请求,我希望它们同时开始,因为一个不依赖另一个的结果。我希望事情能够在后台运行。我找到了concurrent-rubylibrary这似乎运作良好。通过将其混合到您创建的类中,该类的方法具有在后台线程上运行的异步版本。这导致我编写如下代码,其中FirstAsyncWorker和SecondAsyncWorker是我编写的类,我在其中混合了Concurrent::Async模块,并编写了一个名为“work”的方法来发送HTTP请求:defindexop1_result=FirstAsyncWorker.new.async.