1

この質問に対する答えを見つけようとしましたが、自分のコードは思い通りに動作すると思っていましたが、そうではありません。

問題: 「親」LinearLayout があり、これにいくつかのネストされた膨張した「子」LinearLayout を追加します。これは機能します。各 CHILD レイアウトには、カスタム ChipView と TextView の 2 つのビューがあります。各子を膨らませた後、アクティビティ中に「いつでも」各子の ChipView と TextView を変更できるようにしたいと考えています。

簡単なプロジェクトを作成して遊んでみましたが、FIRST INFLATED 子レイアウトの ChipView と TextView にしかアクセスできませんでした。後続のものはすべて親に正しく挿入されていますが、それらを参照する変数を取得できないようです。

実行時にChipViewを作成することでこれを以前に行っていましたが、問題なく機能しましたが、個別に制御できるXMLを使用したよりエレガントなアプローチが必要でした.

私のアクティビティには、子を作成するボタンと、現在の ChipView のメソッドを呼び出すボタンがあります (つまり、最後に膨張したもの、またはクリックしたもの)。

アクティビティ:

public class SandboxActivity extends Activity {
    private Button okbtn;
    private Button add;
    private EditText count;
    private ChipsView chips;
    private LinearLayout pots;
    private TextView amount;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

       okbtn = (Button) findViewById(R.id.ok); //calls the method in customviews
       add = (Button) findViewById(R.id.add);  //adds a child
       count = (EditText) findViewById(R.id.count); //the value to call methods with
       pots = (LinearLayout) findViewById(R.id.pots); //the PARENT layout

       add.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View v) {
            LinearLayout ll = (LinearLayout) getLayoutInflater().inflate(R.layout.pottemplate, pots);
            chips = (ChipsView) ll.findViewById(R.id.chips);
            amount = (TextView) ll.findViewById(R.id.amount);

               //this should allow me to set the activity.chips variable to the last clicked custom view
            chips.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    chips = (ChipsView) v;
                }
            });         
            }

        });


        okbtn.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View v) {
                chips.setCount(Double.parseDouble(count.getText().toString()));         
            }

        });

    }
}

膨張した 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="fill_parent"
    android:orientation="vertical" >
    <com.ded.sandbox.ChipsView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="100dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:id="@+id/chips">    

    </com.ded.sandbox.ChipsView>

    <TextView
        android:id="@+id/amount"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="TextView" android:gravity="center_horizontal" android:textSize="12dp" android:textStyle="bold"/>

</LinearLayout>
4

1 に答える 1

1
View oneHappyRow = mInflater.inflate(R.layout.one_happy_hour_row,null);
TextView dayOfWeekTextView = (TextView) oneHappyRow.findViewById(R.id.dayOfWeekTextView);
TextView hoursOfDayTextView = (TextView) oneHappyRow.findViewById(R.id.hoursOfDayTextView);

膨張したレイアウトごとにここで参照を取得し、テキストなどを設定します。次に:

this.addView(oneHappyRow);

私の場合のクラスは LinearLayout を拡張します。

各参照は現在機能しており、常に最後の参照を取得できるとは限りません。

于 2015-10-16T16:23:40.447 に答える