选择微调项值 [英] Select Spinner item value

查看:189
本文介绍了选择微调项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这样的微调,我想找到选择的项目是什么。我得到的东西的地方,记住这是选择的项目。现在有一个按钮,相同的布局,其中的微调器中单击,这将弹出一个对话框,告诉您选择哪个项目。但我得到的错误,在我的日志猫它说:显示java.lang.NullPointerException

So I have this spinner where I want to find what the selected item is. I got something in place to remember what the item which is selected. Now there is a button to click within the same layout where the spinner is, this will bring up a dialog box which tells which item you have selected. But I am getting error , on my log cat it says "java.lang.NullPointerException"

这是我的对话框XML文件;

This is my XML file for the Dialog Box;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffc0c0c0">

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Saving&apos;s Account"
            android:id="@+id/spinnerSelectedText"
            android:textColor="#ff000000"
            android:drawableLeft="@drawable/transferaccount_icon"
            android:drawablePadding="10dp"
            android:paddingLeft="10dp"
            android:layout_marginTop="10dp"
            android:textSize="16sp" />


</RelativeLayout>

这是我的java文件,该文件是为对话;

This is my java file which is for the dialog ;

public class examplefile extends DialogFragment {

public Dialog onCreateDialog(Bundle savedInstanceState) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inf = getActivity().getLayoutInflater();
    View theDIalog = inf.inflate(R.layout.makea_transfer_confirm, null);
    builder.setView(theDIalog);

    builder.setCancelable(true);

return dialog;

}

}

现在这是当你开始你得到的屏幕 - 的主要活动下课很好,但我不希望共享的主要活动的code,因为我认为这是没有必要的;

Now this is the screen that you get when you start off - well after the main activity class but I don't want to share the code from main activity since i believe it's not necessary;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View v = inflater.inflate(R.layout.makea_transfer, container, false);


    spinner = (Spinner)v.findViewById(R.id.spinnermakeatransfer);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.accounts,R.layout.spinner_item);
    adapter.setDropDownViewResource(R.layout.spinner_dropdown_items);
    spinner.setAdapter(adapter);
    spinner.setPrompt("Select an item");



Spinner spinner1 = (Spinner)v.findViewById(R.id.spinnermakeatransfer);
    String spinnerSelectedItem = spinner1.getSelectedItem().toString();


    TextView spinnerText = (TextView) v.findViewById(R.id.spinnerSelectedText);
    spinnerText.setText(spinnerSelectedItem);

return v;

}

现在,这是makea_transfer XML;

Now this is the makea_transfer XML ;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spinnermakeatransfer"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:paddingTop="5dp"
            android:spinnerMode="dialog"
            android:layout_marginTop="5dp"
            android:touchscreenBlocksFocus="false" />

</RelativeLayout>

请有人可以帮我解决这个问题,请评论,如果你不明白或需要从我这里更多的细节。

Please can someone help me fix this issue, please comment if you don't understand or need more details from me.

这是日志猫 点击此处

推荐答案

下面:

TextView spinnerText = (TextView) v.findViewById(R.id.spinnerSelectedText);

spinnerSelectedText 里面 makea_transfer_confirm 布局,但试图从 makea_transfer

spinnerSelectedText is inside makea_transfer_confirm layout but trying to get from makea_transfer :

获取TextView的在 onCreateDialog examplefile 的方法DialogFragment:

Get TextView in onCreateDialog method of examplefile DialogFragment :

View theDIalog = inf.inflate(R.layout.makea_transfer_confirm, null);
builder.setView(theDIalog);
// get TextView here
 TextView spinnerText = (TextView) theDIalog.findViewById(R.id.spinnerSelectedText);

和用于显示在 spinnerText spinnerSelectedItem 值用它传递 examplefile 使用setArguments类:

And for showing spinnerSelectedItem value in spinnerText pass it using examplefile class using setArguments :

v.findViewById(R.id.maketransferReviewButton).setOnClickListener(
                                                new View.OnClickListener() { 
@Override public void onClick(View v) { 
  SampleDialog fragment = new examplefile(); 
  Bundle args = new Bundle();
  args.putString("spinnerText", spinner1.getSelectedItem().toString());
  fragment.setArguments(args);
  examplefile.show(getFragmentManager(), "make a transfer dialog"); 
 } 

});

和内部onCreateDialog获得字符串使用getArguments为:

and inside onCreateDialog get String using getArguments as:

String strSelectedValueFromSpinner = getArguments().getString("spinnerText");
View theDIalog = inf.inflate(R.layout.makea_transfer_confirm, null);
builder.setView(theDIalog);
// get TextView here
TextView spinnerText = (TextView) theDIalog.findViewById(R.id.spinnerSelectedText);
spinnerText.setText(strSelectedValueFromSpinner);

这篇关于选择微调项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆