除非隐藏键盘,否则ListView不会更新 [英] ListView does not update until keyboard is hidden

查看:68
本文介绍了除非隐藏键盘,否则ListView不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DialogFragment,在布局中我有一个EditTextListView. Listview基本上显示联系人列表(此列表最初有0个项目).当edittext的值更新时,我用在EditText中键入text的联系人填充列表.

I have a DialogFragment and in it's layout I have a EditText and a ListView. The Listview basically shows the list of contacts (Initially this list has 0 items). When the edittext's value is updated I populate the list with contacts that have the text typed in the EditText.

EditText上,当用户键入联系人的姓名或电子邮件地址时,我已经使用addTextChangedListener用所需的联系人更新列表.

On the EditText I have used a addTextChangedListener to update the list with desired contacts as the user types in a name or email address of the contact.

我面临的怪异问题是,仅当我在键入后按下后退按钮以隐藏键盘时,列表(或布局)才会更新.只要显示软键盘,列表就不会更新(第一次将项目添加到空白列表时除外).

The weird problem I am facing is that the list (or maybe the layout) gets updated only when I press the back button to hide the keyboard after typing. As long as the soft keyboard is showing the the list does not get updated (Except for the very first time when items are added to the empty list).

以下是一些更好理解的代码.

Following is some of the code for better understanding.

(在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();
    }

我的假设是列表适配器的列表已正确更新,但是由于某种原因,直到隐藏软键盘后,布局才会反映新的更改.

My assumption is that the list adapter's list is correctly updated but due to some reason the layout does not reflect the new changes until the soft keyboard is hidden.

问题:

  • 是否有人之前遇到过类似的问题(在谷歌搜索时找不到任何资源:/)?
  • 为什么会这样?
  • 官方文档中有与此相关的任何内容吗?
  • 解决此问题的最佳方法是什么?

PS:afterTextChanged方法中的代码以前在onTextChanged方法中,而我也面临相同的问题.

PS: The code in the afterTextChanged method was previously in the onTextChanged method and I was facing the same issue.

更新(添加了屏幕截图以便更好地理解)

UPDATE (Added screenshots for better understanding)

  1. 以下是显示对话框片段且未在edittext中键入任何文本的情况.

  1. The following is when the dialog fragment is shown and no text has been typed in the edittext.

现在,当我键入"A"并填充列表时.

Now when I type in "A" and the list populates.

我再添加几个字母,但列表不会更新.我添加了字母"mit",因此查询现在变为"Amit",但列表中没有变化.

I add another few letters but the list does not update. I added letters "mit" so now the query becomes "Amit" but no change in the list.

现在,当我按下设备上的硬件后退按钮以隐藏键盘时.键盘被隐藏,列表被更新.

Now when I press the hardware back button on the device to hide the keyboard. The keyboard is hidden and the list is updated.

(请不要介意联系人姓名和电子邮件的重叠,仍然必须修改布局:P)

(Please do not mind the overlapping contact names and emails, still have to fix the layout :P)

UPDATE2 (添加EmailContactAdapter代码)

UPDATE2 (Adding EmailContactAdapter code)

以下是EmailContactAdapter类

Following is the EmailContactAdapter class

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.clear(); .然后在 emailContacts.addAll(commonMethods.getEmailContacts(appContext,textValue)); 之前添加 emailContacts.clear(); .您将在列表中附加键入的每个字母.

Try delete all emailContacts.clear();. Then add emailContacts.clear(); just before emailContacts.addAll(commonMethods.getEmailContacts(appContext, textValue));. You are appending your list for every letter your typed.

这篇关于除非隐藏键盘,否则ListView不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆