我正在尝试从主活动上呈现的自定义复合微调器获取项值。在输出中可以选择微调器的下拉列表,但项目捕获没有发生请在下面给我解决方案是代码片段。
自定义/复合控制类
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);
}
}
当我选择它应该在主要活动中动态更新的项目时,请任何人建议我如何从微调器获取主活动中的数据。提前谢谢。
答案 0 :(得分:0)
...当我选择项目时,它应该在main中动态更新 活性。
当你在自定义视图中获得Activity引用时,你可以简单地使用它作为一个接口,在选择事件发生时与它进行通信(并传递选定的值)。你可以这样:
public interface SelectionHappened {
void onSelection(String newSelectedItem);
}
活动需要实现此界面:
public class MainActivity extends ActionBarActivity implements SelectionHappened {
//... current code
@Override
public void onSelection(String newSelectedItem) {
// when this method is triggered the user selected something in the Spinner
}
}
最后一项是从自定义视图发送事件:
public class CompounControlSpinner extends Spinner {
private SelectionHappened mListener;
private String[] items = {
"Google", "IBM", "Microsoft", "Accenture", "Cisco", "Dell", "Oracle", "Facebook"
};
public ComContSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
mListener = (SelectionHappened) context;
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) {
mListener.onSelection((String)parent.getAdapter().getItem(position)); // send the event to the activity
}
//... rest of the current code