8

1 つのダイアログをトリガーするゲーム ビューがあり、ここから 2 番目のダイアログがトリガーされます。

この 2 番目のダイアログには、2 つのボタンと 1 つのスピナーがあります。私が抱えている問題は、動的データを使用して、ビュー内にスピナーを表示することです。

基本的に、ボタンがクリックされたときにスピナーをポップアップして、関連するすべてのデータを一覧表示する必要があります。3 つの異なる ArrayLists (1 つの文字列と 2 つの整数) があり、それぞれのスピナーの選択に動的に追加する必要がある個別の情報を保持します。

pt1 -   ArrayLists1
        ArrayLists2
        ArrayLists3
pt2 -   ArrayLists1
        ArrayLists2
        ArrayLists3

Spinner XML レイアウト コード:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal">
    <Button android:text="@string/attack" 
            android:id="@+id/Attack" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" />
    <Button android:text="@string/useitem" 
            android:id="@+id/UseItemBtn" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" />
    <Spinner android:text="@string/useitem"
            android:id="@+id/UseItemSpin"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:visibility="gone" />
    <Button android:text="@string/equipweapon" 
            android:id="@+id/EquipWeapon" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"/>
</LinearLayout>

スピナーのクリックをトリガーするために現在使用しているコードは次のとおりです(ここのログがトリガーされます):

import android.widget.AdapterView.OnItemSelectedListener;

@Override
public void onClick(View clicked)
{
    if(clicked.getId() == R.id.UseItemBtn)
    {
        Spinner spinner = (Spinner) findViewById(R.id.UseItemSpin);

        ArrayList<String> arrayList1 = new ArrayList<String>();
        arrayList1.add("test item the first one");
        arrayList1.add("really long list item - section 2 goes here - finally section 3");

        ArrayAdapter<String> adp = new ArrayAdapter<String> (context,android.R.layout.simple_spinner_dropdown_item,arrayList1);
        // APP CURRENTLY CRASHING HERE
        spinner.setAdapter(adp);
       //Set listener Called when the item is selected in spinner
       spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
       {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long arg3) 
            {
                String city = "The city is " + parent.getItemAtPosition(position).toString();
                Toast.makeText(parent.getContext(), city, Toast.LENGTH_LONG).show();
            }

            public void onNothingSelected(AdapterView<?> arg0) 
            {
                // TODO Auto-generated method stub
            }
        });
    }
    BattleRun.show();
}

このクリックで動的スピナーを追加するにはどうすればよいですか。これに加えて、スピナーがクリックされたときに、理想的には、実際のスピナー内に送信ボタンを配置して、選択する前に選択肢をフリックできるようにしたいと考えています (常に表示されている必要があります)。

最後に、項目がスピナーで選択されて送信されると、前のダイアログが新しいデータで自動的に更新されます (これは簡単に行うことができます)。

さらにコード スニペットが必要な場合はお知らせください。ヘルプやガイダンスをいただければ幸いです。

ありがとう、L & L パートナー

4

1 に答える 1

18

このコードを使用して、スピナーに文字列配列リストを設定できます -

    public void onClick(View clicked){
    if(clicked.getId() == R.id.Attack){
        Spinner spinner = (Spinner) findViewById(R.id.UseItem);

      //Sample String ArrayList
        ArrayList<String> arrayList1 = new ArrayList<String>();

        arrayList1.add("Bangalore");
        arrayList1.add("Delhi");
        arrayList1.add("Mumbai");
        ArrayAdapter<String> adp = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_dropdown_item,arrayList1);
        spinner.setAdapter(adp);

        spinner.setVisibility(View.VISIBLE);
       //Set listener Called when the item is selected in spinner
       spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
       {
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long arg3) 
            {
                String city = "The city is " + parent.getItemAtPosition(position).toString();
                Toast.makeText(parent.getContext(), city, Toast.LENGTH_LONG).show();

            }

            public void onNothingSelected(AdapterView<?> arg0) 
            {
                // TODO Auto-generated method stub
            }
        });

        //BattleRun.dismiss();
        Log.d("Item","Clicked");

    }
}

このコードは、レイアウト xml ファイル内に入ります -

<!-- Add onClick attribute on this button -->
<Button android:text="@string/attack" 
        android:id="@+id/Attack" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:onClick="onClick" />
<Spinner android:text="@string/useitem" 
        android:id="@+id/UseItem" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:visibility="gone" />
<Button android:text="@string/equipweapon" 
        android:id="@+id/EquipWeapon" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/>

xml ファイル内に onClick を追加していないことに注意してください。そのため、 onClick 関数が呼び出されていません。静的リストを使用するために、strings.xml ファイルで string-array を使用することもできます。

于 2012-07-20T09:10:11.230 に答える