我在其中,當用戶選擇一個值的微調,在onItemSelectedListener打開一個對話框,把更多的細節查找動態創建的佈局,其他的孩子瀏覽
private AdapterView.OnItemSelectedListener spinnerListeners=new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()){
case R.id.puttSpinner:
// open dialog to enter Additional putt Stats
if(position > 0) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());
LinearLayout layout = new LinearLayout(v.getContext());
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);layout.setOrientation(LinearLayout.VERTICAL);layout.setLayoutParams(parms);
layout.setGravity(Gravity.CLIP_VERTICAL);
layout.setPadding(2, 2, 2, 2);
for (int i=1;i<position+1;i++){
LinearLayout ll = new LinearLayout(v.getContext());
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setLayoutParams(llp);
TextView tv = new TextView(v.getContext());
tv.setText("Putt "+String.valueOf(i)+":");
tv.setPadding(40, 40, 40, 40);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(20);
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
tvParams.bottomMargin = 5;
//ll.addView(tv,tvParams);
ll.addView(tv);
Spinner s = new Spinner(v.getContext());
String[] puttDistList = getResources().getStringArray(R.array.putt_dist_list);
s.setAdapter(new ArrayAdapter<String>(v.getContext(),R.layout.putting_practice, puttDistList));
s.setOnItemSelectedListener(puttDistSelectListener);
LinearLayout.LayoutParams sParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.addView(s,sParams);
layout.addView(ll);
}
alertDialog.setView(layout);
alertDialog.setTitle("Putting Stats - Distance to Pin");
alertDialog.setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
現在,當用戶拿起從價值對話微調s
,它監聽更新與用戶輸入數據庫,但如何獲取與此微調
兩個TextView的和微調相關的包含在動態創建的線性佈局「LL」,也就是在TextView tv
的textvalue父佈局「佈局」。兩者都是動態創建
在此先感謝
final AdapterView.OnItemSelectedListener puttDistSelectListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//add the putt# and distance to temp_putt_stats table
DbAdapter adapter = new DbAdapter(view.getContext());
String distValue = parent.getItemAtPosition(position).toString();
switch (position){
case 0: //Inches
break;
case 1: //<3ft
break;
case 2: //<6 ft
break;
case 3: //<10 ft
break;
case 4: //10-20
break;
case 5: //20-30
break;
case 6: //30-40
break;
case 7: //> 40
break;
}
Toast.makeText(view.getContext(), "Position :"+position+" ListItem : " +distValue ,Toast.LENGTH_SHORT).show();
}
我越來越'NPE'與解決方案提供 – archiver
您還可以使用onBindView的微調項自定義適配器(不arrayadapter),並在這個新的適配器,那麼您可以指定標籤的textviews – ramden