0

私は周りにいて、レイアウトの例を繰り返し見てきました。私もいくつか試してみましたが、役に立ちませんでした。ほとんどは、私が以下で達成しようとしていることを実際に表したものではありません。親レイアウトに .add を使用しようとしていて、アンドロイドが気に入らなかったため、動作しているように見えたがエラーが発生したものに遭遇しました。前のアイテムを上書きすることなく、繰り返される各アイテムに異なる詳細を使用できるように、レイアウトを動的にする必要があります。

以下は、繰り返しレイアウトの XML です。これは、アクティビティとは異なるレイアウトにあります。私はそれで満足していません。より良い方法があれば知りたいです。私の構造は少し過負荷になっていると確信していますが、現時点ではそれについて心配していません.

<?xml version="1.0" encoding="utf-8"?>

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="5dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/shop_item_banner"
                android:orientation="horizontal"
                android:padding="10dp" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >

                    <ImageView
                        android:id="@+id/ImageView01"
                        android:layout_width="55dp"
                        android:layout_height="55dp"
                        android:background="@drawable/shop_back_missing_image" />


                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.83"
                        android:orientation="vertical"
                        android:paddingLeft="5dp" >

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" >

                            <TextView
                                android:id="@+id/textView1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="TextView" />
                            <TextView
                                android:id="@+id/textView3"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="TextView" />

                        </LinearLayout>

                        <TextView
                            android:id="@+id/textView2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="TextView" />
                    </LinearLayout>

                    <ImageView
                        android:id="@+id/imageView1"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:src="@drawable/shop_divider_section" />

                    <Button
                        android:id="@+id/button2"
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:background="@drawable/shop_big_button"
                        android:text="BUY"
                        android:typeface="normal" />


                </LinearLayout>

            </LinearLayout>

以下は、ページの構造を更新するコードです。ユーザーがアイテムをスクロールできるように、ScrollView 内にある LinearLayout を更新しています。問題をデバッグできるように、コードを縮小し始めました。これまでのところ、レイアウトを 3 回印刷するための while ループに取り掛かりました。一度は正常に動作しますが、複数回実行すると失敗し、アプリが強制終了します。選択したレイアウトにアイテムを追加できないという警告が表示されます。

    public class LayoutStream extends AsyncTask<String, String, String>{

    String data = null;
    LinearLayout layoutList = (LinearLayout)findViewById(R.id.ScrollItem);

    protected void onPreExecute(){

    }

    @Override
    protected String doInBackground(String... params) {         
        return data;
    }

    protected void onPostExecute(String tpresult){
        int run = 5;
        View storeitems = getLayoutInflater().inflate(R.layout.shop_item, null);
        while (run > 3){

            layoutList.addView(storeitems);
            run--;
        }   
    }
}

これは大きなハードルであり、なぜこれがこのように機能するのかを理解するのに多くの時間を費やしました。

エラーコードは前です。エラーは、postexecute() 中に要素を追加しようとしたときです。

03-21 21:27:44.245: W/dalvikvm(30172): threadid=1: キャッチされない例外で終了するスレッド (group=0x40020ac0) 03-21 21:27:44.255: E/AndroidRuntime(30172): 致命的な例外: メイン03-21 21:27:44.255: E/AndroidRuntime(30172): java.lang.IllegalStateException: 指定された子には既に親があります。最初に子の親で removeView() を呼び出す必要があります。03-21 21:27:44.255: E/AndroidRuntime(30172): android.view.ViewGroup.addViewInner(ViewGroup.java:1970) 03-21 21:27:44.255: E/AndroidRuntime(30172): Android で。 view.ViewGroup.addView(ViewGroup.java:1865) 03-21 21:27:44.255: E/AndroidRuntime(30172): android.view.ViewGroup.addView(ViewGroup.java:1822) 03-21 21:27: 44.255: E/AndroidRuntime(30172): android.view.ViewGroup.addView(ViewGroup.java:1802) 03-21 21:27:44.255: E/AndroidRuntime(30172): com.example.myapp.app.

ありがとう

4

1 に答える 1

1

だから私は私の問題に対する答えを見つけました。私が最初に始めたとき、私は解決に近づいていました。これにより、作成中の各アイテムをターゲットにして、作成時に更新することもできます。

public class LayoutStream extends AsyncTask<String, String, String>{

    String data = null;
    LinearLayout layoutList = (LinearLayout)findViewById(R.id.ScrollItem);

    protected void onPreExecute(){

    }

    @Override
    protected String doInBackground(String... params) {         
        return data;
    }

    protected void onPostExecute(String tpresult){
        int run = 5;
        LinearLayout layoutList = (LinearLayout)findViewById(R.id.ScrollItem);
        LayoutInflater IInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        while (run >= 1){

            View storeitems = IInflater.inflate(R.layout.shop_item, null);
            layoutList.addView(storeitems, 0);
            run--;
        }   
    }
}
于 2013-03-25T02:40:49.580 に答える