1

まず、はい、私は多くの可能な解決策を閲覧しましたが、それらは私を近づけましたが、私の人生では見つけることができない小さな重要なステップが欠けています. すべてが正常に読み込まれます。最初のスピナー (Category) は本来あるべきように設定されています。2 番目のスピナー (Color) は create メソッドによって設定されていることがわかりますが、ユーザーがカテゴリを選択したときにそれ自体を再作成するように指示する場所を見逃していました。これが現在の私のすべてのコードです。

package yarn.pack.name;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.AdapterView.OnItemSelectedListener;

public class YarnDatabaseActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Spinner Category_Add_Spinner = Create_Category_Add_Spinner();
        Spinner Color_Add_Spinner = Create_Color_Add_Spinner(null);
    } catch (Exception e) {
        Log.e("ERROR", e.toString());
        e.printStackTrace();
    }
}

// Creates the Category Spinner for the Add section sends the choice made to
// a method for populating the color spinner
public Spinner Create_Category_Add_Spinner() {
    Spinner Category_Add_Spinner = (Spinner) findViewById(R.id.categoryAddID);
    String[] Category_Add_Spinner_Array = getResources().getStringArray(
            R.array.categoryNames);
    SpinnerAdapter Category_Add_Spinner_Adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_dropdown_item,
            Category_Add_Spinner_Array);
    Category_Add_Spinner.setAdapter(Category_Add_Spinner_Adapter);
    Category_Add_Spinner
            .setOnItemSelectedListener(new Category_Add_Spinner_Listener());
    return Category_Add_Spinner;
}

// The listener for the Category Spinner that sends whatever choice made to
// the method that populates the second spinner.
public class Category_Add_Spinner_Listener implements
        OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> Category_Add_Adapter_View,
            View v, int position, long row) {
        String Category_Add_Choice = Category_Add_Adapter_View
                .getItemAtPosition(position).toString();
        Create_Color_Add_Spinner(Category_Add_Choice);
    }

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

    }

}

// Creates the Color Spinner for the Add section
public Spinner Create_Color_Add_Spinner(String category_Add_Choice) {
    String[] Color_Add_Spinner_Array = null;
    Spinner Color_Add_Spinner = (Spinner) findViewById(R.id.colorAddID);
    if (category_Add_Choice == "Red") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.RedColors);
    } else if (category_Add_Choice == "Pink") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.PinkColors);
    } else if (category_Add_Choice == "Orange") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.OrangeColors);
    } else if (category_Add_Choice == "Yellow") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.YellowColors);
    } else if (category_Add_Choice == "Green") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.GreenColors);
    } else if (category_Add_Choice == "Blue") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.BlueColors);
    } else if (category_Add_Choice == "Purple") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.PurpleColors);
    } else if (category_Add_Choice == "Neutral") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.NeutralColors);
    } else if (category_Add_Choice == "Mix") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.MixColors);
    } else if (category_Add_Choice == "Fleck") {
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.FleckColors);
    } else
        Color_Add_Spinner_Array = getResources().getStringArray(
                R.array.MixColors);     
    SpinnerAdapter Color_Add_Spinner_Adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_dropdown_item,
            Color_Add_Spinner_Array);
    Color_Add_Spinner.setAdapter(Color_Add_Spinner_Adapter);
    Color_Add_Spinner
            .setOnItemSelectedListener(new Color_Add_Spinner_Listener());
    return Color_Add_Spinner;
}
}

私が欠けているものは何でもばかばかしいほど明白なものであると確信していますが、私はこれに非常に長い間取り組んできており、私の時間は短いので、ここに手を差し伸べて、何かを手に入れることができないかどうかを確認しようと思っていました.被写体を照らす光。どうぞよろしくお願いいたします。


命名規則について申し訳ありません。私は一般的に、自分にとって最も意味のある名前を付けています。とにかく、問題は私が .equals の代わりに == を使用したことであることがわかりました。その変更を行い、問題なく動作しました! 皆様、ご協力ありがとうございました。

4

2 に答える 2

1

あなたのコードはほとんど機能しており、完璧ではありませんが、すぐに機能する可能性があります:

  1. Java で文字列を比較するために == を使用しないでください。常に stringA.equals( stringB ) を使用してください。これがあなたの唯一の問題だと思います。
  2. Java の命名規則を尊重すると、読みやすくなります。

@Frankenstein が指摘したように、ウィジェット データ メンバーを作成することは、多くの場合、findViewById を使用して常にレイアウトから再取得するよりも明確で効率的です。

于 2012-04-27T05:37:43.777 に答える
0

これを活動レベルで宣言し、

SpinnerAdapter Category_Add_Spinner_Adapter;

2 番目のステップで、最後に onItemSelected にこの行を追加します。

Category_Add_Spinner_Adapter.notifyDataSetChanged();

変更は実行時に反映されないため、 Array の値のみを変更しますが、アダプターはかなり前に配列を割り当てています。したがって、配列/配列リストの値を変更するたびに、それらの変更をアダプターにも反映します。基になる配列の値が変更されたことをアダプターに通知する必要があります。自分自身を更新してください。

于 2012-04-27T05:37:02.553 に答える