Android-使用片段和简单游标适配器填充列表视图 [英] Android - Populating a listview using fragments and a Simple cursor adapter

查看:96
本文介绍了Android-使用片段和简单游标适配器填充列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道到目前为止,这个问题可能被问了一百遍了,但是我仍然没有设法解决我的问题.我有一个由2个片段组成的活动.一种片段是一种向数据库添加信息的形式:

I know that this question is probably asked a hundred times so far, but i still haven't manage to solve my problem. I have one activity that's comprised of 2 fragments. One fragment is a form that adds information to the database:

**R.layout.fragment_add_server:**
<?xml version="1.0" encoding="utf-8"?>
...
<LinearLayout
    android:id="@+id/nameLinear"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="5dp" >

    <TextView
        android:id="@+id/nameText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="70"
        android:text="@string/strName"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="30"
        android:hint="@string/editName" />
</LinearLayout>
...

还有另一个只是列表视图的片段.

And another fragment that's just a listview.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/georgeback" 
android:orientation="vertical" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="60" >
</ListView>

<Button
    android:id="@+id/connect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="@string/connectBTN" />

</LinearLayout>

该片段,即列表视图,我想用数据库中的任何内容填充它,并在添加新内容时自动刷新(尚未到达该阶段).当我使用下面的代码时,我设法用整个片段填充列表视图(即我看到列表框,按钮等,甚至可以与它们进行交互.初始).

That fragment, the listview, i want to populate it with whatever there is in the database, and auto refresh when something new is added(haven't got to that stage yet). When i'm using the following code below i manage to populate the listview with the entire fragment(i.e i see the listboxes,buttons etc and i can even interact with them. Inception.) instead of just the data.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_select_server,
            container, false);
    Cursor mNotesCursor = mDbHelper.fetchAllNotes();
    String[] from = new String[]{mDbHelper.KEY_TITLE};
    int[] to = new int[]{R.id.editName};
    mCurAdapter = new SimpleCursorAdapter(view.getContext(),R.layout.fragment_add_server,mNotesCursor,from,to,0);

    mMyListView=(ListView)view.findViewById(R.id.listView1);
    mMyListView.setAdapter(mCurAdapter);

    return view;
}

我相信我在构造函数的from和to字段中做错了.知道为什么我要获得整个片段视图而不只是数据库中数据的输出吗?还有其他建议吗?

I believe that i'm doing something wrong with the from and to fields of the constructor. Any idea why i'm getting as an output the entire fragment view and not just the data in the database? Any other suggestions?

推荐答案

我通过更改simplecursoradapter中的布局解决了我遇到的问题.代替

I solved the problem i had by changing the layout in the simplecursoradapter. Instead of

mCurAdapter = new SimpleCursorAdapter(view.getContext(),R.layout.fragment_add_server,mNotesCursor,from,to,0);

我做到了:

mCurAdapter = new SimpleCursorAdapter(view.getContext(),android.R.layout.simple_list_item_1,mNotesCursor,from,to,0);

基本上将布局更改为通用的android list布局就可以了! :)

Essentially changing the layout to the generic android list layout did the trick! :)

这篇关于Android-使用片段和简单游标适配器填充列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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