【问题标题】:Use ListFragment in DialogFragment?在 DialogFragment 中使用 ListFragment?
【发布时间】:2015-06-11 13:52:02
【问题描述】:

我有一个按钮应该打开一个对话框,用户可以在其中在搜索字段中输入一些文本,并且应用程序将在每次输入键时异步搜索数据库(使用 ContentProvider?)。结果显示在EditText下方的ListView中。

我发现的 CursorLoader 示例和大多数其他示例从不使用 DialogFragment,而是使用 Activity、FragmentActivity 或 ListFragment。此示例使用 DialogFragment 中不可用的方法 setListAdapter()。

我可以直接使用 DialogFragment 来满足我的需要,还是需要在 DialogFragment 中使用 ListFragment?

此外,我执行以下操作:

    editTextSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) { }

        @Override
        public void afterTextChanged(Editable s) {
            String search = new String(s.toString());

            getLoaderManager().restartLoader(LOADER_ID, null, this);
        }
    });

但是,this 不是有效的 LoaderCallback。如何访问 DialogFragment?

【问题讨论】:

  • 你可以直接使用DialogFragment。
  • 使用 getActivity() 代替 this。
  • 是实现LoaderCallbacks的DialogFragment。
  • 然后使用 your-dialog-fragment-classname.this 而不是这个。

标签: android


【解决方案1】:

您可以在 DialogFragment 中设置包含 ListView 的自定义视图。这将允许您添加 EditText 并操作 ListView

public class ExampleFragment extends DialogFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.list, null); //inflate some view that contains a ListView
}

@Override
public void onStart() {
    super.onStart();
    ListView listView = (ListView) getView().findViewById(R.id.listView); //get a reference to your ListView

    listView.setAdapter(...); //you can use the same methods as ListFragment to manipulate your list
}}

【讨论】:

    【解决方案2】:

    使用 getActivity() 代替 this 关键字

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 2015-02-18
      • 2012-06-14
      相关资源
      最近更新 更多