在隐藏键盘之前,ListView不会更新

Amy*_*yth 9 android android-listview android-dialogfragment

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

在用户键入联系人的姓名或电子邮件地址时,EditText我使用了a addTextChangedListener来更新列表和所需的联系人.

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

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

CustomDialogFragment.java

(在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);
Run Code Online (Sandbox Code Playgroud)

在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();
    }
Run Code Online (Sandbox Code Playgroud)

我的假设是列表适配器的列表已正确更新,但由于某些原因,布局在隐藏软键盘之前不会反映新的更改.

问题:

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

PS:afterTextChanged方法中的代码以前是在onTextChanged方法中,我遇到了同样的问题.

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

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

  2. 现在,当我输入"A"并填充列表时. 在此输入图像描述

  3. 我添加了另外几个字母,但列表没有更新.我添加了字母"mit"所以现在查询变为"Amit"但列表中没有变化. 在此输入图像描述

  4. 现在,当我按下设备上的硬件后退按钮以隐藏键盘时.键盘被隐藏,列表也会更新. 在此输入图像描述

(请不要介意重叠的联系人姓名和电子邮件,仍然需要修复布局: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;
}
}
Run Code Online (Sandbox Code Playgroud)

Irc*_*ver 0

您正在尝试使用更改来更改可见列表emailContacts,但适配器仍然包含旧列表数据。

解决方案:在EditText创建新适配器中的每个新文本之后(这是一种非常糟糕的方法),或者EmailContactAdapter在您的案例字段中创建方法来替换项目contacts