我完全在试图根据相邻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在spinner之外。
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
}
});
有人可以解释我之前写过的代码有什么问题。
答案 0 :(得分: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);
到onCreate of Activity
的方法答案 1 :(得分: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);
答案 2 :(得分: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 CODE
<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>
答案 3 :(得分: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
);