1
我正在嘗試從主活動中呈現的自定義複合微調器中獲取項目值。在輸出中可能選擇微調器的下拉列表,但項目捕捉不發生請給我下面的解決方案是代碼片段。如何從複合控件android spinner類獲取微調項目到主要活動?
定製/複合控制類
public class CompounControlSpinner extends Spinner {
private String[] items = {
"Google", "IBM", "Microsoft", "Accenture", "Cisco", "Dell", "Oracle", "Facebook"
};
public ComContSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
this.setAdapter(new ArrayAdapter(context, android.R.layout.simple_spinner_item, items));
this.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
}
主要XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mysmax.compouncontrolspinner.MainActivity" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Select Your Company" />
<com.mysmax.compouncontrolspinner.ComContSpinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/text1"
android:layout_below="@+id/text1"
android:layout_marginTop="31dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/text1"
android:layout_below="@+id/spinner1"
android:layout_marginRight="27dp"
android:layout_marginTop="40dp"
android:text="Done" />
</RelativeLayout>
主要活動類
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new CompounControlSpinner(this, null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
請任何一個建議我如何從主要活動獲取數據微調時,我選擇它應該在主要活動中動態更新的項目。提前謝謝。
在複合帶param的onselection類拋出空指針異常,但字符串不爲空。 – user2627142 2014-09-28 17:42:55
@ user2627142在我的代碼中用單獨的行分行,看看哪一個出現異常(如https://gist.github.com/luksprog/54dbdc7c2c87cf23d476)。如果你遵循我的代碼,這個例外不應該發生。 – Luksprog 2014-09-28 17:53:40
09-30 14:27:36.654:E/AndroidRuntime(1188):致命例外:主 14:27:36.654:E/AndroidRuntime(1188):java.lang.NullPointerException 09-30 14:27:36.654: E/AndroidRuntime(1188):\t at com.mysmax.compouncontrolspinner.ComContSpinner $ 1.onItemSelected(ComContSpinner.java:30) 14:27:36.654:E/AndroidRuntime(1188):在widget.AdapterView.fireOnSelected(AdapterView.java :893) 09-30 14:27:36.654:E/AndroidRuntime(1188):\t at android.widget.AdapterView.access $ 200(AdapterView.java:48) 09-30 14:27:36.654:E/AndroidRuntime (1188):\t at android.widget.AdapterView $ SelectionNotifier.run(AdapterView.java:861) – user2627142 2014-09-30 18:51:22