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);

到 Activity 的 onCreate 方法

于 2012-12-08T17:51:09.290 回答
0

我有一个类似的问题。在进入 onItemSelected 之前,我通过获取 textview 来修复它。就我而言,微调器是对话框的一部分。在 onCreateDialog 内部,我获取了 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);

然后在 OnItemSelected 中,我只使用了 textview 并且事情开始按预期工作。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 回答
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

试试这个代码:

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 回答