【问题标题】:How to create listview in fragment that is part of navigation drawer如何在作为导航抽屉一部分的片段中创建列表视图
【发布时间】:2015-10-03 01:59:36
【问题描述】:

下面列出的是一个位于导航抽屉中的片段,我正在尝试在其中创建一个列表视图。但是,这 2 行有一个错误,即以 setlistadapter 和 listview 开头的那一行。它说这是一个错误。我将如何解决它。

Error:(29, 9) error: 找不到符号方法 setListAdapter(ArrayAdapter)

Error:(30, 25) error: 找不到符号方法getListView()

注意:某些输入文件使用或覆盖已弃用的 API。

注意:使用 -Xlint:deprecation 重新编译以了解详细信息。

错误:任务 ':app:compileDebugJavaWithJavac' 执行失败。

编译失败;有关详细信息,请参阅编译器错误输出。

public class First extends Fragment { 

View myView;

@Nullable

String[] courses = {"1", "2", "3"};

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, courses));
    ListView list = getListView();
    list.setTextFilterEnabled(true);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int arg1, long arg2) {
            switch (arg1) {
                case 0:
                    Intent myIntent0 = new Intent(view.getContext(), Test.class);
                    startActivityForResult(myIntent0, 0);
                    break;
                case 1:
                    Intent myIntent1 = new Intent(view.getContext(), Test.class);
                    startActivityForResult(myIntent1, 0);
                    break;
                case 2:
                    Intent myIntent2 = new Intent(view.getContext(), Test.class);
                    startActivityForResult(myIntent2, 0);
                    break;
            }
        }
    });
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.math, container, false);
    return myView;

}
}

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

【问题讨论】:

  • 错误是什么。粘贴 logcat。
  • 错误:(29, 9) 错误: 找不到符号方法 setListAdapter(ArrayAdapter) 错误:(30, 25) 错误: 找不到符号方法 getListView() 注意:有些输入文件使用或覆盖已弃用的 API。注意:使用 -Xlint:deprecation 重新编译以获取详细信息。错误:任务“:app:compileDebugJavaWithJavac”执行失败。 > 编译失败;有关详细信息,请参阅编译器错误输出。
  • @Tauqir 我现在把它放在上面

标签: android listview android-fragments android-intent navigation-drawer


【解决方案1】:

setListAdaptergetListView 都是 ListFragment 类中的方法,但您只是在扩展 Fragment

您应该阅读layoutscreating a navigation drawer。在 xml 布局中,您很可能希望将 ListView 包装在 nav-drawer 中。

【讨论】:

  • 我该怎么做
  • 请阅读链接。第二个实际上有一个嵌套在 DrawerLayout 内的 ListView。
  • 一定会的。谢谢
  • 如果这解决了您的问题,请将我的回答标记为解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多