1

隣接する Spinner で選択されている内容に基づいて TextView の値を変更しようとすることで、私は完全に機知に富んでいます。

public class SpinnerSelectItemListener implements OnItemSelectedListener {
private Context context;

public SpinnerSelectItemListener(Context c){
    this.context = c;
}

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    View view = null;
    TextView textView = null;

    LayoutInflater inflater = LayoutInflater.from(context);
    parent.getItemAtPosition(position);
    view = new View(context);
    view = inflater.inflate(R.layout.common_app_header, null);

    textView = (TextView)view.findViewById(R.id.customer_name_value);
    textView.setText("John");
}
public void onNothingSelected(AdapterView<?> parent) {
    // TODO Auto-generated method stub

}
}

これをデバッグモードで実行すると、すべてが期待どおりに行われますが、すべてが完了すると、デバッガーが新しい値を表示していても、エミュレーターで textView の値は変更されません。私が見逃している本当にばかげた何かが間違いなくあります。助けてください。

編集:状況は、スピナーから従業員の ID 番号を選択したようなもので、選択に応じて、従業員の名前を表示する TextView が変わります。変更したい TextView は、スピナーの外にあります。

EDIT2:リスナーをインラインで定義すると、これは正常に実行されます。つまり、次のように記述します

modelspinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
            TextView textView = null;

            textView = (TextView)findViewById(R.id.customer_segment_value);
            textView.setText("Commercial");

            textView = (TextView)findViewById(R.id.TIV_value);
            textView.setText(R.string.app1_name);
        }

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

        }
    });

私が以前に書いたコードの何が問題なのか、誰か説明してもらえますか?

4

4 に答える 4

1

こちらをご覧ください http://developer.android.com/guide/topics/ui/controls/spinner.html

このメソッドを変更します。

 public void onItemSelected(AdapterView<?> parent, View view, 
                int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
    }

public void onItemSelected(AdapterView<?> parent, View view, 
            int pos, long id) {
        String value = (String) parent.getItemAtPosition(pos)
        textView = (TextView)view.findViewById(R.id.customer_name_value);
        textView.setText(value );
}

でも引っ越すことをお勧めします

textView = (TextView)view.findViewById(R.id.customer_name_value);

メソッドonCreateofyour Activity

于 2012-12-08T17:51:09.290 に答える
0

このコードを試してください:

public class MainActivity extends Activity {


String[] text1 = { "SUNDAY", "MONDAY", "TUESDAY",
        "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
int[] val1 = { 0, 1, 2, 3, 4, 5, 6};


Spinner  spinner1;
TextView  textView1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView1 = (TextView)findViewById(R.id.text1);
    spinner1 = (Spinner)findViewById(R.id.spinner1);
    ArrayAdapter<String> adapter1 =
            new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_spinner_item, text1);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter1);
    spinner1.setOnItemSelectedListener(onItemSelectedListener1);
}

OnItemSelectedListener onItemSelectedListener1 =
        new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                String s1 = String.valueOf(val1[position]);
                textView1.setText(s1);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}

        };
}

XML コード

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>
于 2016-01-30T11:13:34.097 に答える
0

このコードを試してみてください。これの主なキーは " bResult.setText( spinner1csr.getString( spinner1csr.getColumnIndex(DatabaseHandler.KEY_ID1) ) );" です。

public class MainActivity extends AppCompatActivity {
  Spinner s1, s2, s3;
  TextView tex, tex1, bResult;
  Cursor spinner1csr, spinner2csr, spinner3csr, spinner4csr, search;
  SimpleCursorAdapter sca, sca2, sca3, sca4, sca6;
  long spinner1_selected = 0;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      s1 = (Spinner) findViewById(R.id.spinner1);
      s2 = (Spinner) findViewById(R.id.spinner2);
      s3 = (Spinner) findViewById(R.id.spinner5);
      final TextView bResult = (TextView)
      findViewById(R.id.barcodeResult);
      dbhndlr = new DatabaseHandler(this);
      // Get Cursors for Spinners
      spinner1csr = dbhndlr.getAllLabelsAsCursor();
      //Setup Adapter for Spinner 1
      sca = new SimpleCursorAdapter(this,
        android.R.layout.simple_list_item_1, spinner1csr,
        new String[] {
          DatabaseHandler.KEY_ID
        },
        new int[] {
          android.R.id.text1
        },
        0
      );
      s1.setAdapter(sca);
      s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView << ? > parent, View view, int position, long id) {
          //  bResult.setText(s1.getSelectedItem().toString());
          spinner1_selected = id;


        }
        @Override
        public void onNothingSelected(AdapterView << ? > parent) {}
      });
      spinner4csr = dbhndlr.getByRowid(spinner1_selected);
      sca4 = new SimpleCursorAdapter(this,
        android.R.layout.simple_list_item_1,
        spinner4csr,
        new String[] {
          DatabaseHandler.KEY_ID1
        },

        new int[] {
          android.R.id.text1
        },

        0

      );

于 2018-03-19T13:08:44.813 に答える
0

I had a similar issue. I fixed it by getting the textview before I got into the onItemSelected. In my case, the spinner was part of a dialog. Inside the onCreateDialog, that is where I fetched the textview.

protected Dialog onCreateDialog(int id) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(context);

    switch (id) {

    case DIALOG_ADD:

        builder.setTitle("Create New Action");
        final View textEntryView = getLayoutInflater().inflate(
                R.layout.addactionrow, null);

        builder.setView(textEntryView);         

        workingAmount = (TextView) textEntryView
                .findViewById(R.id.WorkingActionamount);

Then inside the OnItemSelected I just used the textview and things started working as expected. spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView parent, View view, int pos, long id) {

            spinnerSelectedAssetID = id;

            //Get the amount currently held here.....
            long x = pfdata.getActionCurrentTotalForAssetByID(spinnerSelectedAssetID);
            Log.d("X+", "X="+x);


            workingAmount.setText(Long.toString(x));
            workingAmount.setVisibility(View.VISIBLE);
于 2013-04-18T17:54:00.310 に答える