【问题标题】:Show Sqlite results to a ListView in a ViewPager Fragment将 Sqlite 结果显示到 ViewPager 片段中的 ListView
【发布时间】:2014-03-02 16:09:06
【问题描述】:

我是 android 新手,我刚刚创建了一个 sqlite 数据库!我在其中添加值(希望如此),我想在 ListView 中显示这些值。

现在更详细...我有一个 ViewPager 并且在第一个片段中我有一个按钮,可以触发我的 sqlite 数据库的 setmethod 。在 ViewPager 的第二个片段中,我想要一个 ListView ,其中 Sqlite 的结果将在我按下按钮后显示。问题是即使我创建了 Listview 它也没有显示在我的片段中。此外,当我在我的设备中运行我的代码时,应用程序崩溃并返回我在下面发布的错误。如果有人可以帮助我正确实现 ListView 并使其工作,我将不胜感激!

OnCreateView :

public class PeopleManager extends ListFragment {

    private PeopleHelper getHelper;


 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  {
    final View v = inflater.inflate(R.layout.fragment_people_manager,null);

     getHelper = new PeopleHelper(getActivity());
     ListView list = (ListView) v.findViewById(R.id.list);

      List<PeopleModel> values = new ArrayList<PeopleModel>();
     ArrayAdapter<PeopleModel> adapter = new ArrayAdapter<PeopleModel>(getActivity(),
            android.R.id.list, values);
         setListAdapter(adapter);

     return v;
 }

我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:orientation="vertical" >

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

    <Button
        android:id="@+id/peopleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="People List" />

</LinearLayout>

日志猫:

03-02 17:37:10.449: E/AndroidRuntime(5555): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

【问题讨论】:

    标签: android sqlite listview android-fragments android-viewpager


    【解决方案1】:

    首先修复异常:替换

    android:id="@+id/list"
    

    android:id="@android:id/list"
    

    在您的布局 XML 中。 id 必须来自android.R,而不是您应用的R

    之后,在这里修复代码:

     ListView list = (ListView) v.findViewById(R.id.list);
    

    更改为android.R.id.list

     ArrayAdapter<PeopleModel> adapter = new ArrayAdapter<PeopleModel>(getActivity(),
            android.R.id.list, values);
    

    ArrayAdapter 构造函数需要每个列表行的 layout 资源 ID。传入 android.R.id.list 将不起作用 - 它甚至不是 layout 标识符。

    然后考虑将一些数据添加到您传递给适配器的ArrayList

    【讨论】:

    • 我做到了!错误消失了,但我的 ListView 中什么也没有出现!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 2015-10-16
    • 2016-03-02
    相关资源
    最近更新 更多