1

この質問はおそらくこれまでに 100 回尋ねられていることはわかっていますが、まだ問題を解決できていません。2 つのフラグメントで構成される 1 つのアクティビティがあります。1 つのフラグメントは、データベースに情報を追加するフォームです。

**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>
...

そして、単なるリストビューである別のフラグメント。

<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>

そのフラグメント、リストビュー、データベースにあるものをすべて入力し、新しいものが追加されたときに自動更新したい(まだその段階に達していない)。以下の次のコードを使用している場合、データだけではなく、リストビューにフラグメント全体を入力することができます (つまり、リストボックス、ボタンなどが表示され、それらと対話することもできます。Inception.)。

@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 フィールドで何か間違ったことをしていると思います。データベース内のデータだけでなく、フラグメントビュー全体を出力として取得する理由は何ですか? 他の提案はありますか?

4

2 に答える 2

0

simplecursoradapterのレイアウトを変更することで、私が抱えていた問題を解決しました。それ以外の

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 リスト レイアウトに変更するとうまくいきました。:)

于 2013-02-27T22:59:55.330 に答える
0

問題は、リスト ビュー アイテム レイアウトの代わりにフラグメント レイアウトを渡す場合です。リスト ビュー アイテムがレイア​​ウト内に含まれていることがわかりますfragment_add_server.xml( ...xml スニペットの前後に配置したため)。スニペットを独自のファイルに移動しmy_list_view_item.xmlSimpleCursorAdapter.

mCurAdapter = new SimpleCursorAdapter(view.getContext(),R.layout.my_list_view_item,mNotesCursor,from,to,0);
于 2013-02-25T22:42:13.220 に答える